TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

C# Named and Optional Arguments

C# Named and Optional Arguments for beginners and professionals with examples on overloading, method overriding, inheritance, aggregation, base, polymorphism, sealed, abstract, interface, namespaces, exception handling, file io, collections, multithreading, reflection etc.

<< Back to C-SHARP

C# Named and Optional Arguments


C# Named Arguments

This feature allows us to associate argument name with its value at the time of function calling.

When we make named arguments, the arguments are evaluated in the order in which they are passed.

It helps us, not to remember the order of parameters. If we know the parameters names, we can pass that in any order. Let's see an example.

C# Named Arguments Example

using System;
namespace CSharpFeatures
{
    public class NamedArgumentsExample
    {
        static string GetFullName(string firstName, string lastName)
        {
            return firstName + " " + lastName;
        }
        public static void Main(string[] args)
        {
            string fullName1 = GetFullName("Rahul", "Kumar"); // Without named arguments
            string fullName2 = GetFullName(firstName:"Rahul", lastName:"Kumar"); // Named arguments
            string fullName3 = GetFullName(lastName:"Rahul", firstName:"Kumar"); // Changing order 
            Console.WriteLine(fullName1);
            Console.WriteLine(fullName2);
            Console.WriteLine(fullName3);
        }
    }
}

Output

Rahul Kumar
Rahul Kumar
Kumar Rahul

C# Optional Arguments

In C#, a method may contain required or optional parameters. A method that contains optional parameters does not force to pass arguments at calling time.

It means we call method without passing the arguments.

The optional parameter contains a default value in function definition. If we do not pass optional argument value at calling time, the default value is used.

Note: We must define optional parameters at the end of parameter list.

C# Optional Arguments Example 1

using System;
namespace CSharpFeatures
{
    public class OptionalArgumentsExample
    {
        public static void Main(string[] args)
        {
            add(12,12); // Passing both arguments
            add(10);    // Passing only required argument
        }
        static void add(int a, int b = 10) // second parameter is optional
        {
            Console.WriteLine(a+b);
        }
    }
}

Output

24
20

We can make any number of optional parameter s. let's see an example.

C# Optional Arguments Example 2

using System;
namespace CSharpFeatures
{
    public class OptionalArgumentsExample
    {
        public static void Main(string[] args)
        {
            add(12,12); // Passing both arguments
            add(12);    // Passing one argument
            add();      // Passing No argument
        }
        static void add(int a = 12, int b = 12) // Making all parameters optional
        {
            Console.WriteLine(a+b);
        }
    }
}

Output

24
24
24

Make sure, all the optional parameters are placed at the end of the parameter list. Otherwise, compiler throws a compile-time error. Let's see an example.

C# Optional Arguments Example 3

using System;
namespace CSharpFeatures
{
    public class OptionalArgumentsExample
    {
        public static void Main(string[] args)
        {
            add(12,12); // Passing both arguments
            add(12);    // Passing one argument
            add();      // Passing No argument
        }
        static void add(int a = 12, int b) // Making first parameter as optional
        {
            Console.WriteLine(a+b);
        }
    }
}

Output

error CS1737: Optional parameters must appear after all required parameters





Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf