开发者

How to use Optional/Named parameters in C# 4.0

开发者 https://www.devze.com 2023-03-28 16:32 出处:网络
For the life of me I seemingly can not understand what Optional Par开发者_运维问答ameters are used for. By that, I mean, what kind of programs would they be used in, and how? The same thing applies to

For the life of me I seemingly can not understand what Optional Par开发者_运维问答ameters are used for. By that, I mean, what kind of programs would they be used in, and how? The same thing applies to Named Parameters, I just can't seem to fully grasp either thing. I know Optional Parameters help keep the amount of overloaded methods down. Thats about it. If someone could help me fully understand what these are used for/how to use them I would greatly appreciate it.


Optional Parameters are what their name is: for optional parameters.

The only thing that is very important is, that the default values of the parameters gets compiled into the calling code. This means, if you change the default value of a parameter you will have to recompile the calling assemblies too, otherwise they will still use the old default value.


I generally use optional parameters on a method that many different objects might call. For example, if you had an application that handles a search you might have the params

List<string> SearchStuff(string searchString, Date startDate, Date endDate);

Later on you might start thinking about pagination and want to default it to 25 results but still allow the caller to determine it and could use an optional parameter to help you out like so:

List<string> SearchStuff(string searchString, Date startDate, Date endDate, int pageSize = 25);

The pageSize you can see is optional by setting a value on it. The caller can pass a pageSize if it wants or can omit it all together and the default value will be used.

As for named parameters, I am not sure that I have used them in C# (or if you even can). In Objective-c however, they are used constantly:

- (NSString *) getThisFromInteger:(NSInteger)myInteger;

and when calling the method you actually are typing out the named parameters when calling it

[self getThisFromInteger: 24];

as opposed to C# which this would be more like

self.getThis(24);

Hope this helps some.


John covers the use of optional parameters well, but to add an explanation of named parameters: If you have a lot of optional parameters, you have two choices if you want to specify the 6th one: specify all earlier optional parameters, or use a named parameter to just set that one specific one.

Example:

public static void sendEmail(string to, string subject, string body, string[] attachmentFiles = null, bool isHTML = false, string onBehalfOf = "", MailPriority priority = MailPriority.Normal, string BCCAddress = null, bool isWeb = false)

If I want to send a high-priority email my choices are:

// If we didn't have optional parameters
sendEmail("me@me.com", "Important!", "Something broke - fix it!", null, false, "", MailPriority.High, null, false)
// If we didn't have named parameters
sendEmail("me@me.com", "Important!", "Something broke - fix it!", null, false, "", MailPriority.High)
// Using both optional and named parameters to full advantage.
sendEmail("me@me.com", "Important!", "Something broke - fix it!", priority: MailPriority.High)

The last of those three is much easier to use and read later.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号