This article explains the alias statement and command line arguments in C#.
Alias for Namespaces Classes
In C# programming we can avoid providing the "System" qualifier for the "Console" class with "using System;" but we cannot avoid using "System.Console" or "Console" for the method WriteLine().
Alias | Description
"System" is a namespace and "Console" is a class. The using directive can be applied only to a namespace and cannot be applied to classes.
Therefore the statement:
using System.Console
is illegal. However we can overcome this problem using Aliases for namespace classes.
An Alias 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 like 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 provided to the Main method at the time of invoking it for execution by Windows.
Command Line Arguments | Use
This example will show you how to use command line arguments in your code project, here we go:
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;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Hello_Word
{
class Program
{
static void Main(string[] args)
{
double sqrt;
double argValue = 0.0d;
double sqrtValue = 0.0d;
// double.sqrtValue = 0.0d;
if (args.Length == 0)
{
Console.WriteLine("no specified argument");
Console.ReadLine();
return;
}
argValue = double.Parse(args[0].ToString());
// sqrtValue = Math.sqrt(argValue);
Console.WriteLine("square root of argument is: " + sqrtValue);
Console.WriteLine("");
}
}
}
The output of this snippet will be “no specified argument” because a command line argument is not reaching the second case.
Output Window
Now run this code and you will get what you need.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Hello_Word
{
class Program
{
static void Main(string[] args)
{
double sqrt;
double argValue = 0.0d;
double sqrtValue = 0.0d;
double.sqrtValue = 0.0d;
if (args.Length == 0)
{
Console.WriteLine("no specified argument");
Console.ReadLine();
return;
}
argValue = double.Parse(args[0].ToString());
sqrtValue = Math.sqrt(argValue);
Console.WriteLine("square root of argument is: " + sqrtValue);
Console.WriteLine("");
}
}
}