Default or optional Parameters in C#
-
Hi, We have default parameters in C++ like at the time of function definition we can give some default value to a parameter. Is there anyhting similar in C#. Because when I gave like in the below line it was giving me error in the C#, can I use this thing in the C#
public static void sample(int i=100, string s=string.Empty, bool b=true)
{
///Something
}Thanks & Regards, Md. Abdul Aleem NIIT technologies
visual studio 2010
First and the Foremost: FIGHT TO WIN
-
Hi, We have default parameters in C++ like at the time of function definition we can give some default value to a parameter. Is there anyhting similar in C#. Because when I gave like in the below line it was giving me error in the C#, can I use this thing in the C#
public static void sample(int i=100, string s=string.Empty, bool b=true)
{
///Something
}Thanks & Regards, Md. Abdul Aleem NIIT technologies
-
Hi, We have default parameters in C++ like at the time of function definition we can give some default value to a parameter. Is there anyhting similar in C#. Because when I gave like in the below line it was giving me error in the C#, can I use this thing in the C#
public static void sample(int i=100, string s=string.Empty, bool b=true)
{
///Something
}Thanks & Regards, Md. Abdul Aleem NIIT technologies
-
Hi, We have default parameters in C++ like at the time of function definition we can give some default value to a parameter. Is there anyhting similar in C#. Because when I gave like in the below line it was giving me error in the C#, can I use this thing in the C#
public static void sample(int i=100, string s=string.Empty, bool b=true)
{
///Something
}Thanks & Regards, Md. Abdul Aleem NIIT technologies
-
Hi, We have default parameters in C++ like at the time of function definition we can give some default value to a parameter. Is there anyhting similar in C#. Because when I gave like in the below line it was giving me error in the C#, can I use this thing in the C#
public static void sample(int i=100, string s=string.Empty, bool b=true)
{
///Something
}Thanks & Regards, Md. Abdul Aleem NIIT technologies
No, you can't set default values. But you can use an overload to accomplish something similar.
public static void sample(int i, string s, bool b)
{
///Something
}public static void sample()
{
sample(100, string.Empty, true);
}public static void sample(int i)
{
sample(i, string.Empty, true);
}///Etc...
-
Not exactly elegant but...
public static void sample(int i, string s, bool b)
{
if (i == default(int)) { i = 100; }
if (s == default(string)) { s = string.Empty; }
if (b == default(bool)) { b = true; }// The rest of your method...
}
Works in .NET Framework 3.5.
Kevin Rucker, Application Programmer QSS Group, Inc. United States Coast Guard OSC Kevin.D.Rucker@uscg.mil "Programming is an art form that fights back." -- Chad Hower
Wait, how does that even work? I've never even heard of something like that. I am intrigued. Is this what you're doing? http://msdn.microsoft.com/en-us/library/xwth0h0d%28v=VS.80%29.aspx[^]
-
Wait, how does that even work? I've never even heard of something like that. I am intrigued. Is this what you're doing? http://msdn.microsoft.com/en-us/library/xwth0h0d%28v=VS.80%29.aspx[^]
Yes, it just returns the framework's default value for the type specified.
Kevin Rucker, Application Programmer QSS Group, Inc. United States Coast Guard OSC Kevin.D.Rucker@uscg.mil "Programming is an art form that fights back." -- Chad Hower
-
C# 4 !
LOL, please say it again, just one more time :laugh:
-
As already pointed out, optional, default and named parameters are available in .NET 4.0 Interestingly, if you do use them the code analysis tool slaps your hand about it, requesting that you create overloads with the defaults set in method. G
grgran wrote:
.NET 4.0
C# 4 dagnabit!!!
-
LOL, please say it again, just one more time :laugh:
Done, but I've now run out of attributes -- how do you do blinking?
-
Not exactly elegant but...
public static void sample(int i, string s, bool b)
{
if (i == default(int)) { i = 100; }
if (s == default(string)) { s = string.Empty; }
if (b == default(bool)) { b = true; }// The rest of your method...
}
Works in .NET Framework 3.5.
Kevin Rucker, Application Programmer QSS Group, Inc. United States Coast Guard OSC Kevin.D.Rucker@uscg.mil "Programming is an art form that fights back." -- Chad Hower
So, then, how would I specify 0, null, and false (?) when I want to?
-
Not exactly elegant but...
public static void sample(int i, string s, bool b)
{
if (i == default(int)) { i = 100; }
if (s == default(string)) { s = string.Empty; }
if (b == default(bool)) { b = true; }// The rest of your method...
}
Works in .NET Framework 3.5.
Kevin Rucker, Application Programmer QSS Group, Inc. United States Coast Guard OSC Kevin.D.Rucker@uscg.mil "Programming is an art form that fights back." -- Chad Hower
Hmm, surely that misses the advantage of giving optional parameters, i.e. you can call the function without specifying all the input - using this form means you still need to give three values, or it doesn't match the function. Also, in this example, it's impossible to supply 'false' for the third value; false is the default value for a bool, so it would always become true.
-
Hi, We have default parameters in C++ like at the time of function definition we can give some default value to a parameter. Is there anyhting similar in C#. Because when I gave like in the below line it was giving me error in the C#, can I use this thing in the C#
public static void sample(int i=100, string s=string.Empty, bool b=true)
{
///Something
}Thanks & Regards, Md. Abdul Aleem NIIT technologies
Yes, the C# lost that feature from "primitive" C++. You get it back with interest in C# 4.0 - not only can you provide default parameter values, you can also name your parameters and provide them in any order you want. For example, in C++ you had to order your function parameters correctly, so that the ones with default values came at the end. void foo(int i = 1, int j = 0, int k) This didn't work, because what does the call foo(1, 2) mean? In C# 4.0, you can do that. foo(k: 0, j: 1) So add "named" parameters (arguments) to the feature, which was not available in C++.
-
Hmm, surely that misses the advantage of giving optional parameters, i.e. you can call the function without specifying all the input - using this form means you still need to give three values, or it doesn't match the function. Also, in this example, it's impossible to supply 'false' for the third value; false is the default value for a bool, so it would always become true.
You're absolutely correct, the only way to truly emulate optional parameters like in VB would be to use overloads of the method like this:
public static void sample(int i, string s, bool b)
{
// Do work here
}public static void sample(int i)
{
sample(i, string.Empty, true);
}public static void sample(int i, string s)
{
sample(i, s, true);
}public static void sample(int i, bool b)
{
sample(i, string.Empty, b);
}public static void sample(string s)
{
sample(100, s, true);
}public static void sample(string s, bool b)
{
sample(100, s, b);
}public static void sample (bool b)
{
sample(100, string.Empty, b);
}public static void sample()
{
sample(100, string.Empty, true);
}Which swiftly becomes unweildy once you get past three parameters.
Kevin Rucker, Application Programmer QSS Group, Inc. United States Coast Guard OSC Kevin.D.Rucker@uscg.mil "Programming is an art form that fights back." -- Chad Hower
-
So, then, how would I specify 0, null, and false (?) when I want to?
See the answer I posted to StephenPhillips.
Kevin Rucker, Application Programmer QSS Group, Inc. United States Coast Guard OSC Kevin.D.Rucker@uscg.mil "Programming is an art form that fights back." -- Chad Hower
-
I usually implement that pattern as follows in C# 3.5:
public static void sample(int i, string a)
{
_sample(i,false,true,a);
}
public static void sample(int i, bool b)
_sample(i,false,b,string.Empty);
}
public static void sample(string a, bool b)
{
_sample(100,false,b,a);
}
private static void _sample(int i,bool a,bool b,string a_string)
{
// implement 'sample' operation
}Software Zen:
delete this;
This is what I use, however I'll admit I feel it slopifies the code base.... :doh:
I'd blame it on the Brain farts.. But let's be honest, it really is more like a Methane factory between my ears some days then it is anything else...
-----
"The conversations he was having with himself were becoming ominous."-.. On the radio...