Archive for the ‘ C# ’ Category

Optional Parameters in C#

With the release of .NET 4.0 C# programmers are now able to created methods with optional parameters like VB programmers doing. Optional arguments are widely used in VBA for long time. Although they make life a little bit easier for programmers (you don’t have to repeat default values in your method calls).

Code Snippet
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace optionalParameters
  7. {
  8.     class Program
  9.     {
  10.         static void Main(string[] args)
  11.         {
  12.             OptionalParameterTest t = new OptionalParameterTest();
  13.             Console.WriteLine("Optional Parameter :  {0}", t.OptionalParameter("One"));
  14.             Console.WriteLine("Optional Parameter :  {0} ", t.OptionalParameter("One1", "Two1"));
  15.             Console.WriteLine("Optional Parameter :  {0} ", t.OptionalParameter("One1", "Two1","three1"));
  16.             Console.WriteLine("Optional Parameter :  {0} ", t.OptionalParameter("One1", "Two1","Three1","Four1"));
  17.             Console.ReadLine();
  18.  
  19.         }
  20.  
  21.     }
  22.     public class OptionalParameterTest
  23.     {
  24.         public string OptionalParameter(string  one, string two = "Two",string three="Three",string four="Four")
  25.         {
  26.  
  27.            
  28.             return one + "  " + two + "  " + three + "  " + four;
  29.         }
  30.     }
  31.  
  32. }

Optional Parameters

Understanding Method Overloading in C#

Like other Programming languages C# also supports method overloading. Method overloading is a feature found in various programming languages such as Ada, C#, C++, D and Java that allows the creation of several methods with the same name which differ from each other in terms of the type of the input and the type of the output of the function. [According to wikipedia]

Method Overloading Example

Suppose in our application we want to add to two integers, double, float and long. The first way is to create unique methods for each addition operation or call a single method name with distinct set of arguments.

The Visual Studio IDE will provide you assistance while calling overloaded method.

method overloading

Code Snippet
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace Method_Overloading
  7. {
  8.     class Program
  9.     {
  10.         static void Main(string[] args)
  11.         {
  12.             Console.WriteLine("**********************************Method Overloading Demo**********************************");
  13.             Console.WriteLine("Add two int :" + Add(1, 1));
  14.             Console.WriteLine("Add two Double :" + Add(1.0, 1.0));
  15.             Console.WriteLine("Add two float :" + Add(11.12, 1.123));
  16.             Console.WriteLine("Add two long :" + Add(234561, 11235678));
  17.             Console.ReadLine();
  18.  
  19.         }
  20.         private static  Int64 Add(int num1, int num2)
  21.         {
  22.             return num1 + num2;
  23.         }
  24.         private static double Add(double num1, double num2)
  25.         {
  26.             return num1 + num2;
  27.         }
  28.         private static float Add(float num1, float num2)
  29.         {
  30.             return num1 + num2;
  31.         }
  32.         private static long Add(long num1, long num2)
  33.         {
  34.             return num1 + num2;
  35.         }
  36.     }
  37. }

Method overloading does not depends upon the return type of method, if you are having two methods with unique name with equal number of arguments and of same type with different return type than these methods are not overloaded method instead they are same methods and compiler will give error at compile time.

method overloading error

Data Type Conversion in .Net

Some times it is required to convert data from one base data type to another base data type. The Convert Class converts a base data type to another base data type. Convert class throw a FormatException when the attempt was made to convert a String to other base data type and String value is in not proper format. To Handle FormatException we can use Convert class convert method inside try catch block but there is another better way of doing the same thing is to use TryParse to avoide runtime errors and eliminate the need of Try Catch block.

TryParse method is available for all .Net base data type including Boolean, Char, SByte, Byte, Int16, Int32, Int64, UInt16, UInt32, UInt64, Single, Double, Decimal, DateTime and String. There are two overloaded versions of TryParse method is available for each .Net base data type.

Boolean.Try Parse

Converts specified String value to equivalent Boolean Value. Return True if Conversion is successful otherwise return False.

Byte.TryParse

Converts Specified String value to equivalent Byte Value. Return True if conversion is successful otherwise return False.

TryParse works in the same way for other base type also.

Code Snippet
  1. Dim Result As Boolean
  2.         Dim ByteResult As Byte
  3.         Dim BooleanResult As Boolean
  4.         Dim IntegerResult As Integer
  5.         Dim DoubleResult As Double
  6.         Dim InputString As String = "1"
  7.  
  8.         Result = Byte.TryParse(InputString, ByteResult)
  9.         If Result = False Then
  10.             Console.WriteLine(InputString & " is not converted into Byte" & vbCrLf)
  11.         Else
  12.             Console.WriteLine(InputString & " is converted to Byte : " & ByteResult & vbCrLf)
  13.         End If
  14.  
  15.         InputString = "True"
  16.         Result = Boolean.TryParse(InputString, BooleanResult)
  17.         If Result = False Then
  18.             Console.WriteLine(InputString & " is not converted to Boolean : " & vbCrLf)
  19.         Else
  20.             Console.WriteLine(InputString & " is converted to Boolean : " & BooleanResult & vbCrLf)
  21.         End If
  22.  
  23.         InputString = "12345"
  24.         Result = Integer.TryParse(InputString, IntegerResult)
  25.         If Result = False Then
  26.             Console.WriteLine(InputString & " is not converted to Integer" & vbCrLf)
  27.         Else
  28.             Console.WriteLine(InputString & " is converted to Integer : " & IntegerResult & vbCrLf)
  29.         End If
  30.  
  31.         InputString = "12345.111"
  32.         Result = Double.TryParse(InputString, DoubleResult)
  33.         If Result = False Then
  34.             Console.WriteLine(InputString & " is not converted to Double : " & vbCrLf)
  35.         Else
  36.             Console.WriteLine(InputString & " is converted to Double : " & DoubleResult & vbCrLf)
  37.         End If