C# Named and Optional Arguments Tutorial with Examples

C# Named and Optional Arguments Tutorial with Examples

Today, We want to share with you C# Named and Optional Arguments Tutorial with Examples.
In this post we will show you Optional Arguments In C#, hear for Optional Parameters and Named Arguments in C# Example we will give you demo and example for implement.
In this post, we will learn about Named and optional parameters in C# with an example.

Introduction: Optional Arguments In C#

In this post, we will learn about Optional Arguments In C#. An optional parameter has a default value.

Read Also:  Reuse Code in jquery Ajax Methods

Now in this post, I will explain about Optional Arguments In C# with appropriate example.By using this new feature in C#.Net , we can add default values for formal parameters.There can be any number of Optional Arguments in a method.Optional Arguments are defined at the end of the parameter list in method.

Example:

Create Console Application in Visual Studio and write below lines of codes in it.

Read Also:  get_post_meta in WordPress Example

using System;
using System.Collections;

namespace ConsoleDemo
{   
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("pakainfo.Com");

            ///Call the method by omiting Optional Arguments  
            Program.GetDetails(1, "Raj");

            ///Call the method by omiting 'course' Optional Arguments  
            Program.GetDetails(2, "Mahesh", 11);

            ///Call the method with all the parameters that will override the default values  
            Program.GetDetails(3, "Pinak", 12, "Science");
            Console.ReadLine();  
        }

        public static void GetDetails(int roll_No, string name, int id = 10, string course = "Btech")
        {
            Console.WriteLine(string.Format("Roll No.: {0}, Name: {1}, id: {2}, Course: {3}", roll_No, name, id, course));
        }
    }   
}

Read :

Read Also:  5 best open source pdf library using PHP : Best PDF Library

Summary

You can also read about AngularJS, ASP.NET, VueJs, PHP.

I hope you get an idea about How can you use optional parameters in C#.
I would like to have feedback on my Pakainfo.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.