JumpStart # 1 – Using Optional And Named Parameters in C#

Before looking at the code, Read about – What is JumpStart?

<%@ Page Language="C#" %>
<%@ Import Namespace="System.Runtime.InteropServices" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">
    protected void Page_Load(object sender, EventArgs e)
    {
        // Call with only name 
        optionalParameterMethod("John");
        
        //call with name and state
        optionalParameterMethod("Scott", "AK");
        
        //call with name, state and city
        optionalParameterMethod("Chris", "FL", "Miami");
        
        //call with name and city
        optionalParameterMethod("Alan", city: "BayArea");

        //call with name, state and city with Named Parameters
        optionalParameterMethod("Bob", state: "Calif" ,city: "BayArea");

        /*
            cannot call method like the following with missing Required Parameter
            optionalParameterMethod(state:"Hi",city: "Hello");      
        */

        // Call with only name 
        optionalParameterAttMethod("John");

        //call with name and state
        optionalParameterAttMethod("Scott", "AK");

        //call with name, state and city
        optionalParameterAttMethod("Chris", "FL", "Miami");

        //call with name and city
        optionalParameterAttMethod("Alan", city: "BayArea");

        //call with name, state and city with Named Parameters
        optionalParameterAttMethod("Bob", state: "Calif", city: "BayArea");

        /*
            cannot call method like the following with missing Required Parameter 
            optionalParameterAttMethod(state:"Hi",city: "Hello");      
        */

    }
    
    protected void optionalParameterMethod(string name, string state = "CA", string city = "SFO")
    {
        /* Use the parameters here. This is a feature of C# 4.0
         * If not Values are supplied to parameters, then they contain default values i.e., CA , SFO
         */
        Response.Write(name + "---" + state + "---" + city);
        Response.Write("<br/>");
    }
    
    protected void optionalParameterAttMethod(string name, [Optional,DefaultParameterValue("CA")] string state, [Optional] string city)
    {
        /* To implement Optional Att - import System.Runtime.InteropServices namespace
         * If not Values are supplied to parameters, then they contain default values i.e., null
         */
        Response.Write(name + "---" + state + "---" + city);           
        Response.Write("<br/>");
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    </div>
    </form>
</body>
</html>

 

Appendix -

  1. state: "Calif", city: "BayArea" – These are called as Named Parameters. Those calls improve readability. Lessen burdens on the developers to memorize the order of parameters.
  2. Be careful in using Optional Parameters – as they are substituted with their default values. So the logic must be good enough in case of using optional parameters.
  3. [Optional,DefaultParameterValue(“CA”)] – also serves as optional parameters. But we got to import System.Runtime.InteropServices.

You may also like...