Optional parameters - one of the coolest feature in C# 4.0. It is similar to the optional parameters in SQL Server stored procedure.
Since this feature is not in the earlier versions, we need to use method overloading. Now, in C# 4.0 instead of method overloading we can use optional parameters.
How to implement an optional parameters in C# 4.0? Here is a simple example.
public void GetSupplierAddress(int SuppNo, string city = null)
Please note the second parameter. It has the null value as default. It is not possible to assign a value like this in the earlier versions of C#. So while calling this method it is possible to ignore the second parameter.
SupplierData objSuppliers = new SupplierData();
objSuppliers.GetSupplierAddress(1);
objSuppliers.GetSupplierAddress(1, "London");
You can find the source code below.
0 comments:
Post a Comment