-
January 13th, 2011, 11:20 AM
#1
Regarding what to type in to the parameter of this function
Hello, I am just began working on the software development and have few questions about some function as I have not used C# for like a year. Here is my question, In the function at the bottom, I am just wondering what "([Optional, DefaultValue(Constants.SilentLoud.Loud)]" stands for. Is it kinda like a data type? and then, what should I type in to this parameter when I need to call this function? Could you please explain what it stands for and what it does?
The function is:
Public object Close([Optional, DefaultValue(Constants.SilentLoud.Loud)] Constants.SilentLoud silent)
Thank you very much!
-
January 14th, 2011, 02:25 PM
#2
Re: Regarding what to type in to the parameter of this function
It isn't the data type. The data type is actually Constants.SilentLoud .... what's in the brackets actually says that the parameter is optional... and if you don't explicitly pass in a value, then use the Constants.SilentLoud.Loud value.
as for what to pass it, you can pass nothing, or a valid Constants.SilentLoud value.
-tg
-
January 15th, 2011, 01:18 AM
#3
Re: Regarding what to type in to the parameter of this function
hhmmm...Didn't know optional values worked like that, or in that syntax anyways in C#. Can someone give an example of a code that will compile which uses optional param?
As a rule of thumb, for me atleast, I try not to use optional params (I work with vb6 also and have seen it in a lot of procedures). If you feel like you need to pass/return value(s), in most cases, it shouldn't be an optional param but a required param.
-
January 16th, 2011, 03:14 PM
#4
Re: Regarding what to type in to the parameter of this function
Well, up to C# 4 the language design team was against optional parameters, advocating the use of method overloading instead, but then they changed their mind, probably somewhat influenced by the whining of all the people doing COM interop and having to pass Type.Missing a dozen of times for unused parameters.
Now, "[Optional, DefaultValue(Constants.SilentLoud.Loud)]" are just 2 attributes combined (like the ones available in VB.NET), but it is somewhat unusual for a developer to write something like that, because there's an alternate syntax that's way more easier to read:
public object Close(Constants.SilentLoud silent = Constants.SilentLoud.Loud)
C# 4 now also supports named parameters (in Smaltalk fashion). All in all, it's not such a big deal, it just makes coding more convenient and/or more readable.
For example:
Code:
public static string SayHi(string firstName, string lastName = string.Empty)
{
if (lastName == string.Empty)
Console.WriteLine(Hello there, " + firstName + "!";
else
Console.WriteLine(Hello there, " + firstName + " " + lastName + "!";
}
// and when u call it in main()
SayHi("Duke");
// or
SayHi("Duke", "Nukem");
// or with named params
SayHi(
firstName: "Duke",
lastName: "Nukem" // optional
);
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|