60 likes | 226 Views
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.
E N D
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 • void DoEveryThing(){ } //void or parameterless • void DoEveryThing(int iv, double dv){ } //parameterized • void DoEveryThing(double dv, int iv){ } //overloaded
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);
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. “
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