简体   繁体   English

如何设置哪些功能输入是必需的,哪些不需要(C#)

[英]How to set which of function inputs are required and which are not (C#)

So I have a function 所以我有一个功能

public int Sum(var valueA, var valueB, var valueC) { 
  var summ = valueA + valueB; 
  return summ; 
} 

I want to add to summ valueC if it was given. 我想添加到summ valueC(如果已给出)。 And let user not specify it if he does not want to. 如果不想,则不要让用户指定它。 How to do such thing? 怎么做这样的事情?

You can do this in C# 4.0 with optional arguments . 您可以在C#4.0中使用可选参数来执行此操作。 If you're using a version prior to C# 4.0, you could create an overloaded function that calls your function with a default value. 如果使用的是C#4.0之前的版本,则可以创建一个重载函数,该函数以默认值调用您的函数。

If you are using .NET 3.5 or earlier you can use Nullable Types . 如果使用的是.NET 3.5或更早版本,则可以使用Nullable Types

public int Sum(int valueA, int valueB, int? valueC)
{
    int sum = valueA + valueB;
    if (valueC.HasValue)
    {
        sum += valueC.Value;
    }

    return sum;
}

The calls would be: 这些电话将是:

int answer1 = Sum(1, 2, 3); // = 6

int answer2 = Sum(1, 2, null); // = 3

Of course the classic way to do this is to use method overloads: 当然,执行此操作的经典方法是使用方法重载:

public int Sum(int valueA, int valueB)
{
    int sum = valueA + valueB;
    return sum;
}

public int Sum(int valueA, int valueB, int valueC)
{
    int sum = valueA + valueB + valueC;
    return sum;
}

int answer1 = Sum(1, 2);
int answer2 = Sum(1, 2, 3);

If you want to be able to use string as well as int either move to .NET4 or create another pair of overloaded methods: 如果您希望既可以使用string可以使用int ,请移至.NET4或创建另一对重载方法:

public int Sum(string valueA, string valueB)
{
    // Convert the strings to int and call the previous code
    // You will need to cope with the case when the strings don't represent numbers
}

public int Sum(string valueA, string valueB, string valueC)
{
    ...
}

If you want to cope with mixed string and int then you'll need even more overloads - which is probably overkill. 如果您想处理混合的stringint那么您将需要更多的重载-这可能是过大了。

In addition to the options that Shane Fulmer provided, you can also use the params keyword to have a function that takes a variable number of parameters: 除了Shane Fulmer提供的选项之外,您还可以使用params关键字来具有接受可变数量参数的函数:

public int Sum(params int[] values)
{
    int sum = 0;
    for(int i = 0; i < values.Length; i++){
        sum+=values[i];
    }
    return sum;
}

int answer2Parameters = Sum(1, 5);
int answer3Parameters = Sum(1, 2, 3);
int answer4Parameters = Sum(1, 3, 5, 6);

Of course if you want to limit them to exactly two or three then you probably want to look at optional parameters in C#4.0 or overload the Sum function - by this I mean create two Sum functions, one that takes two parameters and another that takes 3 parameters. 当然,如果您要将它们限制为恰好两个或三个,那么您可能想查看C#4.0中的可选参数或重载Sum函数-我的意思是创建两个Sum函数,一个带有两个参数,另一个带有3个参数。

public int Sum(int valueA, int valueB) { 
  int summ = valueA + valueB; 
  return summ; 
} 

public int Sum(int valueA, int valueB, int valueC) { 
  int summ = valueA + valueB + valueC; 
  return summ; 
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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