C# named arguments vs fixed arguments

In C# we can specify argument name in function call. The name of the arguments should be exactly same to the name of the receiving arguments in function definition.
If named arguments are used along fixed arguments, the fixed arguments should appear first, and then named arguments will appear in function call and definition.

Here is an example.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication6
{   
    class Program
    {
   
        static void Main(string[] args)
        {
           Console.WriteLine( FullName(45, "Ali", "Khan") );

           Console.WriteLine( FullName(45, LastName: "Khan", FirstName: "Ali") );
     
        }

        static string FullName(int age, string FirstName, string LastName)
        {

            return FirstName + " " + LastName + " " + age;
        }

     
    }
}

 

Tags:

Add a Comment

Your email address will not be published. Required fields are marked *