Can I specify a default value for parameters or additional parameters in C # functions?

Is it possible to specify default options in C #?

In C:

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

Can we give the parameters a default value? Is this possible in C #? If so, can we avoid function overloading?

It is always useful to use an optional parameter for an existing function. If you are working on a project that has to refer to a class that has a function, and we changed the parameter with an optional value, it may throw a runtime exception that the method was not found.

This is because we will take into account that if we add an additional optional value, no code change will be required if the function is used in many places.

 function Add(int a, int b); 

This will be invoked as follows:

 Add(10, 10); 

But if we add an optional parameter, for example,

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

then the compiler expects

 Add(10, 10, 0); 

In fact, we call this Add(10, 10) , and this function will not be available in this class and will throw a runtime exception.

This happens by adding a new parameter to the function called by many places, and I'm not sure if this happens every time. But I suggest you overload the function.

You always need to overload a method that has an optional parameter. Also, if you are working with functions that have more than one optional parameter, then it is useful to pass the value using the parameter name.

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

It is always useful to call this function as follows.

 Add(10, 20, c:30); 
+46
c #
Oct 12 2018-10-12
source share
6 answers

This is how you do it in C #, but this feature was first added in .NET 4.0

+54
Oct 12 '10 at 12:52
source share

This is only possible with C # 4.0

However, when you use a C # version prior to 4.0, you can get around this with overloaded methods:

 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).

+26
Oct 12 2018-10-12
source share

Yes. See Named and Optional Arguments . Note that the default value should be constant, so this is normal:

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

but this is not so:

 public void Bar(string myParam = Foo()) // not a constant, not OK { } 
+25
Oct. 12 '10 at 12:52
source share

Yes, but you need to use .NET 3.5 and C # 4.0 to get this functionality.

This MSDN page contains additional information.

+5
Oct 12 '10 at 12:52
source share

This functionality is available in C # 4.0 - it was introduced in Visual Studio 2010. And you can use it in a project for .NET 3.5. Therefore, there is no need to upgrade old projects in .NET 3.5 to .NET 4.0.

You just need to use Visual Studio 2010, but remember that it should compile the default language version (install it in the project Properties-> Buid-> Advanced ...)

This MSDN page contains additional information about advanced options in VS 2010.

+1
Jul 24 '14 at 19:52
source share

This is a C # 4.0 function, but was not possible without using function overloads prior to this version.

0
Oct 12 '10 at 12:52
source share



All Articles