1 / 6

Optional & Named Parameters

Optional & Named Parameters. CS3260 Version 1.0. Overview. Parameterless Methods C# 1.0 Paramerterized Methods C # 1.0 Overloaded Methods C # 1.0 Optional Parameters/Arguments Methods C# 4.0 Named Parameters/Arguments C# 4.0. Method Parameters.

galia
Télécharger la présentation

Optional & Named Parameters

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Optional & Named Parameters CS3260 Version 1.0

  2. Overview • Parameterless Methods C# 1.0 • Paramerterized Methods C# 1.0 • Overloaded Methods C# 1.0 • Optional Parameters/Arguments Methods C# 4.0 • Named Parameters/Arguments C# 4.0

  3. Method Parameters • void DoEveryThing(){ } //void or parameterless • void DoEveryThing(int iv, double dv){ } //parameterized • void DoEveryThing(double dv, int iv){ } //overloaded

  4. Optional Parameters • “Parameters in C# can now be specified as optional by providing a default value for them in a member declaration. When the member is invoked, optional arguments can be omitted. Furthermore, any argument can be passed by parameter name instead of position.” C# 4.0 Specification • void DoEveryThing(int iv=100, double dv = 5.5){ } • DoEveryThing(); • DoEveryThing(222); • DoEveryThing(333,75.55);

  5. Optional Arguments “Formal parameters of constructors, methods, indexers and delegate types can be declared optional: fixed-parameter:attributesoptparameter-modifieropt type identifier default-argumentopt default-argument:=expression • A fixed-parameter with a default-argument is an optional parameter, whereas a fixed-parameter without a default-argument is a required parameter. “

  6. Named Arguments • “Named and optional parameters are really two distinct features, but are often useful together. Optional parameters allow you to omit arguments to member invocations, whereas named arguments is a way to provide an argument using the name of the corresponding parameter instead of relying on its position in the parameter list.” C# 4.0 Specification • void DoEveryThing(dv:44.45); //passing dv by name • void DoEveryThing(iv:555); //passing iv by name • void DoEveryThing(dv:5.5,iv:66); //reverse order

More Related