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

Download Visual Studio 2010 and .NET Framework 4 Training Kit – February Release

The Visual Studio 2010 and .NET Framework 4 Training Kit includes presentations, hands-on labs, and demos. This content is designed to help you learn how to utilize the Visual Studio 2010 features and a variety of framework technologies including:

  • C# 4.0
  • Visual Basic 10
  • F#
  • Parallel Extensions
  • Windows Communication Foundation
  • Windows Workflow
  • Windows Presentation Foundation
  • ASP.NET 4
  • Windows 7
  • Entity Framework
  • ADO.NET Data Services
  • Managed Extensibility Framework
  • Visual Studio Team System

This version of the Training Kit works with Visual Studio 2010 RC and .NET Framework 4 RC.

  • Supported Operating Systems: Windows 7; Windows Server 2008; Windows Vista

Visual Studio 2010 RC .NET Framework 4 RC.

Instructions

Download and launch the self-extracting package. The Training Kit will uncompress to the selected folder and launch a HTML browser for the content.

Download Visual Studio 2010 and .NET Framework 4 Training Kit – February Release