Sibeesh Passion

Top Menu

  • Home
  • Search
  • About
  • Privacy Policy

Main Menu

  • Articles
    • Azure
    • .NET
    • IoT
    • JavaScript
    • Career Advice
    • Interview
    • Angular
    • Node JS
    • JQuery
    • Knockout JS
    • Jasmine Framework
    • SQL
    • MongoDB
    • MySQL
    • WordPress
  • Contributions
    • Medium
    • GitHub
    • Stack Overflow
    • Unsplash
    • ASP.NET Forum
    • C# Corner
    • Code Project
    • DZone
    • MSDN
  • Social Media
    • LinkedIn
    • Facebook
    • Instagram
    • Twitter
  • YouTube
    • Sibeesh Venu
    • Sibeesh Passion
  • Awards
  • Home
  • Search
  • About
  • Privacy Policy

logo

Sibeesh Passion

  • Articles
    • Azure
    • .NET
    • IoT
    • JavaScript
    • Career Advice
    • Interview
    • Angular
    • Node JS
    • JQuery
    • Knockout JS
    • Jasmine Framework
    • SQL
    • MongoDB
    • MySQL
    • WordPress
  • Contributions
    • Medium
    • GitHub
    • Stack Overflow
    • Unsplash
    • ASP.NET Forum
    • C# Corner
    • Code Project
    • DZone
    • MSDN
  • Social Media
    • LinkedIn
    • Facebook
    • Instagram
    • Twitter
  • YouTube
    • Sibeesh Venu
    • Sibeesh Passion
  • Awards
  • Linux Azure Function Isolated Dot Net 9 YAML Template Deployment

  • Build, Deploy, Configure CI &CD Your Static Website in 5 mins

  • Post Messages to Microsoft Teams Using Python

  • Get Azure Blob Storage Blob Metadata Using PowerShell

  • Deploy .net 6 App to Azure from Azure DevOps using Pipelines

.NETASP.NET
Home›.NET›Named and Optional Arguments In C#

Named and Optional Arguments In C#

By SibeeshVenu
July 2, 2015
1418
0
Share:

Introduction

In this article we will learn about two things that we must aware of being a programmer.

  • Named Arguments
  • Optional Arguments

Both of this has been introduced with Visual Studio 2010. Now we will go ahead in detail about those. I hope you will like it.

Download Source Code

NamedOptional.rar

Background

I am working in a project in which we are using Visual Studio 2012 and C# as the programming language, We do have so many functions in our logical layers. In those functions we used both Named and Optional Arguments. So I thought of sharing this with you. Please be noted that this article is for the one who have not tried these Named and Optional arguments yet.

Before going to the coding part, we will learn what named and optional argument is? What are all the features of these two?

Named Arguments

Ref: MSDN Named arguments enable you to specify an argument for a particular parameter by associating the argument with the parameter’s name rather than with the parameter’s position in the parameter list.

As said by MSDN, A named argument ,

  • Enables you to pass the argument to the function by associating the parameter’s name
  • No needs for remembering the parameters position that we are not aware of always.
  • No need to look the order of the parameters in the parameters list of called function.
  • We can specify parameter for each arguments by its name
  • No we will describe all about a named argument with a simple program. I hope we all know how to find out the area of a rectangle. Yes you are right it is A= wl (Where w is the width and l is length and A is area.) So we will be using this formula in our function. Consider following is our function call.

    [csharp]
    FindArea(120, 56);
    [/csharp]

    In this our first argument is length (ie 120) and second argument is width (ie 56). And we are calculating the area by that function. And following is the function definition.

    [csharp]

    private static double FindArea(int length, int width)
    {
    try
    {
    return (length* width);
    }
    catch (Exception)
    {
    throw new NotImplementedException();
    }
    }

    [/csharp]

    So in the first function call, we just passed the arguments by its position. Right?
    [csharp]
    double area;
    Console.WriteLine("Area with positioned argument is: ");
    area = FindArea(120, 56);
    Console.WriteLine(area);
    Console.Read();
    [/csharp]

    If you run this, you will get an output as follows.

    Now here it comes the features of a named arguments. Please see the preceding function call.

    [csharp]
    Console.WriteLine("Area with Named argument is: ");
    area = FindArea(length: 120, width: 56);
    Console.WriteLine(area);
    Console.Read();
    [/csharp]

    Here we are giving the named arguments in the method call.

    [csharp]
    area = FindArea(length: 120, width: 56);
    [/csharp]

    Now if you run this program, you will get the same result. Please see the below image.

    As I said above, we can give the names vice versa in the method call if we are using the named arguments right? Please see the preceding method call.

    [csharp]
    Console.WriteLine("Area with Named argument vice versa is: ");
    area = FindArea(width: 120, length: 56);
    Console.WriteLine(area);
    Console.Read();
    [/csharp]

    Please run the program and see the output as below.

    You get the same result right? I hope you said yes.

    One of the important use of a named argument is, when you use this in your program it improves the readability of your code. It simply says what your argument is meant to be, or what it is?.

    Now you can give the positional arguments too. That means, a combination of both positional argument and named argument. So shall we try that?

    [csharp]
    Console.WriteLine("Area with Named argument Positional Argument : ");
    area = FindArea(120, width: 56);
    Console.WriteLine(area);
    Console.Read();
    [/csharp]

    In the above example we passed 120 as the length and 56 as a named argument for the parameter width.

    I hope you enjoyed using named arguments, there are some limitations too. We will discuss the limitation of a named arguments now.

    Limitation of using a Named Argument

    Named argument specification must appear after all fixed arguments have been specified.

    If you use a named argument before a fixed argument you will get a compile time error as follows.
    Named argument specification must appear after all fixed arguments have been specified

    Optional Arguments

    Ref: MSDN The definition of a method, constructor, indexer, or delegate can specify that its parameters are required or that they are optional. Any call must provide arguments for all required parameters, but can omit arguments for optional parameters.

    As said by MSDN, a Optional Argument,

  • We can omit the argument in the call if that argument is an Optional Argument
  • Every Optional Argument has its own default value
  • It will take default value if we do not supply the value
  • A default value of a Optional Argument must be a
  • 1. Constant expression.
    2. Must be a value type such as enum or struct.
    3. Must be an expression of the form default(valueType)

  • It must be set at the end of parameter list
  • Now consider preceding is our function definition with optional arguments.
    [csharp]
    private static double FindAreaWithOptional(int length, int width=56)
    {
    try
    {
    return (length * width);
    }
    catch (Exception)
    {
    throw new NotImplementedException();
    }
    }
    [/csharp]

    Here we have set the value for width as optional and gave value as 56. right? Now we will try to call this function.

    If you note, the IntelliSense itself shows you the optional argument as shown in the below image.

    Now if you call the function as shown in the preceding code block. The function will be fired and give you the same output.

    [csharp]
    Console.WriteLine("Area with Optional Argument : ");
    area = FindAreaWithOptional(120);
    Console.WriteLine(area);
    Console.Read();
    [/csharp]

    Note that we did not get any error while compiling and it will give you an output as follows.

    Conclusion

    I hope you will like this article. Please share me your valuable thoughts and comments. Your feedback is always welcomed.

    Thanks in advance. Happy coding!

    Kindest Regards
    Sibeesh Venu

    TagsC#C# ArgumentsC# ParametersC# programmingNamed ArgumentsNamed ParametersOptional ArgumentUsing Named ArgumentUsing Optional Arguments
    Previous Article

    Jun 2015 Month Winner In C-Sharp Corner

    Next Article

    How to check my mobile is 3G ...

    0
    Shares
    • 0
    • +
    • 0
    • 0
    • 0

    SibeeshVenu

    I am Sibeesh Venu, an engineer by profession and writer by passion. Microsoft MVP, Author, Speaker, Content Creator, Youtuber, Programmer.

    Related articles More from author

    • .NETC#Code Snippets

      How to trim that last occurrence of a character in C#

      May 31, 2015
      By SibeeshVenu
    • Q&A

      CellSetGrid Implementation in VS 2012 or later

      July 14, 2015
      By SibeeshVenu
    • .NETASP.NETBlogC#

      Calling a Webmethod Using Jquery Ajax

      May 31, 2015
      By SibeeshVenu
    • jQuery Datatable With Server Side Data
      .NETAngularWeb API

      jQuery Datatable With Server Side Data

      February 25, 2016
      By SibeeshVenu
    • .NETASP.NETC#Interview

      C sharp Interview Questions And Answers

      October 24, 2015
      By SibeeshVenu
    • .NETASP.NETC#Code Snippets

      Split method with a string value in c#

      May 31, 2015
      By SibeeshVenu
    0

    My book

    Asp Net Core and Azure with Raspberry Pi Sibeesh Venu

    YouTube

    MICROSOFT MVP (2016-2022)

    profile for Sibeesh Venu - Microsoft MVP

    Recent Posts

    • Linux Azure Function Isolated Dot Net 9 YAML Template Deployment
    • Build, Deploy, Configure CI &CD Your Static Website in 5 mins
    • Easily move data from one COSMOS DB to another
    • .NET 8 New and Efficient Way to Check IP is in Given IP Range
    • Async Client IP safelist for Dot NET
    • Post Messages to Microsoft Teams Using Python
    • Get Azure Blob Storage Blob Metadata Using PowerShell
    • Deploy .net 6 App to Azure from Azure DevOps using Pipelines
    • Integrate Azure App Insights in 1 Minute to .Net6 Application
    • Azure DevOps Service Connection with Multiple Azure Resource Group

    Tags

    Achievements (35) Angular (14) Angular 5 (7) Angular JS (15) article (10) Article Of The Day (13) Asp.Net (14) Azure (65) Azure DevOps (10) Azure Function (10) Azure IoT (7) C# (17) c-sharp corner (13) Career Advice (11) chart (11) CSharp (7) CSS (7) CSS3 (6) HighChart (10) How To (9) HTML5 (10) HTML5 Chart (11) Interview (6) IoT (11) Javascript (10) JQuery (82) jquery functions (9) JQWidgets (15) JQX Grid (17) Json (7) Microsoft (8) MVC (20) MVP (9) MXChip (7) News (18) Office 365 (7) Products (10) SQL (20) SQL Server (15) Visual Studio (10) Visual Studio 2017 (7) VS2017 (7) Web API (12) Windows 10 (7) Wordpress (9)
    • .NET
    • Achievements
    • ADO.NET
    • Android
    • Angular
    • Arduino
    • Article Of The Day
    • ASP.NET
    • Asp.Net Core
    • Automobile
    • Awards
    • Azure
    • Azure CDN
    • azure devops
    • Blockchain
    • Blog
    • Browser
    • C-Sharp Corner
    • C#
    • Career Advice
    • Code Snippets
    • CodeProject
    • Cognitive Services
    • Cosmos DB
    • CSS
    • CSS3
    • Data Factory
    • Database
    • Docker
    • Drawings
    • Drill Down Chart
    • English
    • Excel Programming
    • Exporting
    • Facebook
    • Fun
    • Gadgets
    • GitHub
    • GoPro
    • High Map
    • HighChart
    • How to
    • HTML
    • HTML5
    • Ignite UI
    • IIS
    • Interview
    • IoT
    • JavaScript
    • JQuery
    • jQuery UI
    • JQWidgets
    • JQX Grid
    • Json
    • Knockout JS
    • Linux
    • Machine Learning
    • Malayalam
    • Malayalam Poems
    • MDX Query
    • Microsoft
    • Microsoft ADOMD
    • Microsoft MVP
    • Microsoft Office
    • Microsoft Technologies
    • Microsoft Windows
    • Microsoft Windows Server
    • Mobile
    • MongoDB
    • Monthly Winners
    • MVC
    • MVC Grid
    • MySQL
    • News
    • Node JS
    • npm
    • Number Conversions
    • October 2015
    • Office 365
    • Office Development
    • One Plus
    • Outlook
    • Page
    • PHP
    • Poems
    • PowerShell
    • Products
    • Q&A
    • Raspberry PI
    • React
    • SEO
    • SharePoint
    • Skype
    • Social Media
    • Software
    • Spire.Doc
    • Spire.PDF
    • Spire.XLS
    • SQL
    • SQL Server
    • SSAS
    • SSMS
    • Storage In HTML5
    • Stories
    • Third Party Software Apps
    • Tips
    • Tools
    • Translator Text
    • Uncategorized
    • Unit Testing
    • UWP
    • VB.Net
    • Videos
    • Virtual Machine
    • Visual Studio
    • Visual Studio 2017
    • Wamp Server
    • Web API
    • Web Platform Installer
    • Webinars
    • WebMatrix
    • Windows 10
    • Windows 7
    • Windows 8.1
    • Wordpress
    • Writing

    ABOUT ME

    I am Sibeesh Venu, an engineer by profession and writer by passion. Microsoft MVP, Author, Speaker, Content Creator, Youtuber, Programmer. If you would like to know more about me, you can read my story here.

    Contact Me

    • info@sibeeshpassion.com

    Pages

    • About
    • Search
    • Privacy Policy
    • About
    • Search
    • Privacy Policy
    © Copyright Sibeesh Passion 2014-2025. All Rights Reserved.
    Go to mobile version