简体   繁体   中英

Can I make a function in C# take an optional parameter or do I need to overload it?

I have the following in an interface:

string GetTopic(string rk);

and this function:

public string GetTopic(string rk)
{
    return string.Format("{0}.{1}.{2}",
        rk.Substring(0, 2).TrimStart('0'),
        rk.Substring(2, 2).TrimStart('0').PadLeft(1, '0'),
        rk.Substring(4, 2).TrimStart('0').PadLeft(1, '0'));
}

I would like to add an optional second parameter, enabling the function to be called like this:

var a = GetTopic("010101") 

or

var b = GetTopic("010101","test")

In the first case I would like to get the output "1.1.1" and in the second case the output "1.1.1 - test".

Is this possible or do I need to make two functions and have one overload the other one? How can I specify the optional second parameter in my interface?

You can set a default:

public string GetTopic(string rk, string anotherParam = "")
{
    String append = (String.IsNullOrEmpty(anotherParam)) ? "" : " - " + anotherParam;
    return string.Format("{0}.{1}.{2}{3}",
        rk.Substring(0, 2).TrimStart('0'),
        rk.Substring(2, 2).TrimStart('0').PadLeft(1, '0'),
        rk.Substring(4, 2).TrimStart('0').PadLeft(1, '0'),
        append);
}

so "anotherParam" will be test if you call:

var a = GetTopic("010101");

And for your interace-definition:

public interface IUtilityService 
{ 
    string GetTopic(string rk, string suffix = ""); 
} 

MSDN: http://msdn.microsoft.com/de-de/library/dd264739.aspx

In C# 4.0, you can provide a default value for parameter, therefore making it optional:

public string GetTopic(string rk, string param2 = "5")
{
    return string.Format("{0}.{1}.{2}",
        rk.Substring(0, 2).TrimStart('0'),
        rk.Substring(2, 2).TrimStart('0').PadLeft(1, '0'),
        rk.Substring(4, 2).TrimStart('0').PadLeft(1, '0'));
}

You can use params keyword:

public string GetTopic(string rk, params options)
{
    return string.Format("{0}.{1}.{2}",
        rk.Substring(0, 2).TrimStart('0'),
        rk.Substring(2, 2).TrimStart('0').PadLeft(1, '0'),
        rk.Substring(4, 2).TrimStart('0').PadLeft(1, '0'));
}

This gives you an array of optional paramaters. Here is a reference link .

It is possible:

public string GetTopic(string rk,string lk = null)
{
    if (lk!=null)
    { //with param logic

    }
    else
    { //without param logic

    }
    return string.Format("{0}.{1}.{2}",
        rk.Substring(0, 2).TrimStart('0'),
        rk.Substring(2, 2).TrimStart('0').PadLeft(1, '0'),
        rk.Substring(4, 2).TrimStart('0').PadLeft(1, '0'));
}

You need to change your interface to make the second parameter optional:

public interface YourInterface
{
    string GetTopic(String rk, String suffix = "");
}

Then, implement your interface, again specifying the optional parameter:

public class YourClass : YourInterface
{
    public string GetTopic(String rk, String suffix = "")
    {
        return string.Format("{0}.{1}.{2}{3}{4}",
            rk.Substring(0, 2).TrimStart('0'),
            rk.Substring(2, 2).TrimStart('0').PadLeft(1, '0'),
            rk.Substring(4, 2).TrimStart('0').PadLeft(1, '0'),
            string.IsNullOrWhiteSpace(suffix) ? "" : " - ",
            suffix);
    }    
}    

A heads-up regarding the use of optional parameters in situations like these: be careful about the default values you specify because depending on how the method is called, the value defined in the interface or the value defined in the implementation will be used.

For instance, if the interface were to define suffix = "" and if the implementation were to define suffix = null :

  • YourInterface.GetTopic("010101") would pass and empty string as the second parameter, but
  • YourClass.GetTopic("010101") would pass null instead!

A good overview of the possible cases can be found in this blog post .

you need to add the optional param into the interface:

 interface IInterface
    {
        string GetTopic(string one, string t = null);
    }

    class a : IInterface
    {
        public string GetTopic(string one, string t = null)
        {
            throw new NotImplementedException();
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM