简体   繁体   English

我可以在C#函数中为参数或可选参数赋予默认值吗?

[英]Can I give a default value to parameters or optional parameters in C# functions?

Can I give default parameters in C#? 我可以在C#中提供默认参数吗?

In C: 在C:

void fun(int i = 1)
{
    printf("%d", i);
}

Can we give parameters a default value? 我们可以给参数一个默认值吗? Is it possible in C#? 在C#中有可能吗? If so, can we avoid overloading functions? 如果是这样,我们可以避免重载功能吗?

It's always a bad practice to add an optional parameter to an existing function. 向现有函数添加可选参数始终是一种不好的做法。 If you are working on a project which is having to refer the class having a function and we changed a parameter with an optional value, it may throw a run time exception that the method is not found. 如果您正在处理一个必须引用具有函数的类的项目,并且我们使用可选值更改了参数,则可能会抛出运行时异常,即找不到该方法。

This is because we will consider that the if we add an extra optional value, there is no code change required if the function is used in many places. 这是因为我们将考虑如果我们添加一个额外的可选值,如果在许多地方使用该函数,则不需要更改代码。

function Add(int a, int b);

This will be called using this way: 这将使用这种方式调用:

Add(10, 10);

But if we add an optional parameter like this, 但是如果我们添加这样的可选参数,

function Add(int a, int b, int c = 0);

then the compiler expects 然后编译器期望

Add(10, 10, 0);

Actually we are calling like this Add(10, 10) and this function won't be available in that class and causes a run time exception. 实际上我们这样调用Add(10, 10)并且该函数在该类中不可用并导致运行时异常。

This happens for while adding a new parameter to a function which called by a lot of places and I not sure this will happen every time. 这种情况发生在将一个新参数添加到由很多地方调用的函数时,我不确定每次都会发生这种情况。 But I suggest you to overload the function. 但我建议你重载这个功能。

Always we need to overload the method which has an optional parameter. 我们总是需要重载具有可选参数的方法。 Also if you are working with functions having more than one optional parameter, then it's good to pass the value using the name of the parameter. 此外,如果您正在使用具有多个可选参数的函数,则最好使用参数名称传递该值。

function Add(int a, int b, int c = 0);

It's always good to call this function using the following way. 使用以下方式调用此函数总是好的。

Add(10, 20, c:30);

这正是你在C#中的表现,但该功能最初是在.NET 4.0中添加的

Yes. 是。 See Named and Optional Arguments . 请参阅命名参数和可选参数 Note that the default value needs to be a constant, so this is OK: 请注意,默认值必须是常量,所以这没关系:

public string Foo(string myParam = "default value") // constant, OK
{
}

but this is not: 但这不是:

public void Bar(string myParam = Foo()) // not a constant, not OK
{
}

It is only possible as from C# 4.0 它只能从C#4.0开始

However, when you use a version of C#, prior to 4.0, you can work around this by using overloaded methods: 但是,当您使用4.0之前的C#版本时,可以使用重载方法解决此问题:

public void Func( int i, int j )
{
    Console.WriteLine (String.Format ("i = {0}, j = {1}", i, j));
}

public void Func( int i )
{
    Func (i, 4);
}

public void Func ()
{
    Func (5);
}

(Or, you can upgrade to C# 4.0 offcourse). (或者,您可以升级到C#4.0 offcourse)。

Yes, but you'll need to be using .NET 3.5 and C# 4.0 to get this functionality. 是的,但您需要使用.NET 3.5和C#4.0来获得此功能。

This MSDN page has more information. 此MSDN页面包含更多信息。

This functionality is available from C# 4.0 - it was introduced in Visual Studio 2010. And you can use it in project for .NET 3.5. 此功能可从C#4.0获得 - 它是在Visual Studio 2010中引入的。您可以在.NET 3.5的项目中使用它。 So there is no need to upgrade old projects in .NET 3.5 to .NET 4.0. 因此,无需将.NET 3.5中的旧项目升级到.NET 4.0。

You have to just use Visual Studio 2010, but remember that it should compile to default language version (set it in project Properties->Buid->Advanced...) 您必须使用Visual Studio 2010,但请记住它应该编译为默认语言版本(在项目Properties-> Buid-> Advanced ...中设置它)

This MSDN page has more information about optional parameters in VS 2010. 此MSDN页面包含有关VS 2010中可选参数的更多信息。

这是C#4.0的一个功能,但在该版本之前不使用函数重载是不可能的。

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

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