ALIAS And Command Line Argument in C#

This article will familiarize you with alias concepts and command line arguments in C#.

Command Line Argument

Alias for Namespaces Classes

In C# programming we can avoid the prefix System to the console class using "using System;" but we cannot avoid prefixing the System.Console to the method WriteLine().

Alias | Description

System is a namespace and Console is a class. The using directive can be applied only to the namespace and cannot be applied to classes.

Therefore the statement:

using System.Console;

Is illegal. However we can overcome this problem using an aliases for namespace classes.
Aliases can be used as in the following:

Using alias-name = class-name;

Alias | Ref Example

Using A = System.Console;

// A is the alias for System.Console

class Sample

{

    public static void Main()

    {

        A.WriteLine(“hello C# corner”);

    }

} 

CLA | Command Line Arguments

There are many occasions when we may want to make our program work differently depending on the input or we can say that we may want our program to behave in a specific way depending upon the input provided at the time of execution.

Command Line Arguments | Description

This functionality can be done in C# using Command Line Arguments (CLAs).
Command Line Arguments are parameters that are forwarded to the Main method at the time of invoking it for execution.

Command Line Arguments | Use

This example will show you, how to use the Command Line Argument in your code project, here we go:

Using System;

 

class Command

{

    public static void Main()

    {

        Console.Write(“welcome to”);

        Console.Write(“ ” + args[0]);

        Console.WriteLine(“ ” +args[1]);

    }

}

Command Line Arguments | Ref. Example

Using System;

 

class Command

{

    public static void Main()

    {

        double argValue = 0.0;

        double.sqrtValue = 0.0;

        if(args.Length == 0 )

        {

             Console.WriteLine(“No specified argument”);

             Console.Read.Line();

             return;

        }

        argValue = double.Parse(args[0].ToString());

        sqrtValue = Math.sqrt(argValue);

        Console.WriteLine(“Demonstrating CLA-- \n”);

        Console.WriteLine(“square root of argument is: {0}\n”, sqrtValue);

     }

}