Tuesday, August 24, 2021

What is Params in C#?

 In C#, params is a keyword which is used to specify a parameter that takes variable number of arguments. It is useful when we don't know the number of arguments prior. Only one params keyword is allowed and no additional parameter is permitted after params keyword in a function declaration.

C# Params Example 1

  1. using System;  
  2. namespace AccessSpecifiers  
  3. {  
  4.     class Program  
  5.     {  
  6.         // User defined function  
  7.         public void Show(params int[] val) // Params Paramater  
  8.         {  
  9.             for (int i=0; i<val.Length; i++)  
  10.             {  
  11.                 Console.WriteLine(val[i]);  
  12.             }  
  13.         }  
  14.         // Main function, execution entry point of the program  
  15.         static void Main(string[] args)  
  16.         {  
  17.             Program program = new Program(); // Creating Object  
  18.             program.Show(2,4,6,8,10,12,14); // Passing arguments of variable length  
  19.         }  
  20.     }  
  21. }  

Output:

2
4
6
8
10
12
14

C# Params Example 2

In this example, we are using object type params that allow entering any number of inputs of any type.

  1. using System;  
  2. namespace AccessSpecifiers  
  3. {  
  4.     class Program  
  5.     {  
  6.         // User defined function  
  7.         public void Show(params object[] items) // Params Paramater  
  8.         {  
  9.             for (int i = 0; i < items.Length; i++)  
  10.             {  
  11.                 Console.WriteLine(items[i]);  
  12.             }     
  13.         }  
  14.         // Main function, execution entry point of the program  
  15.         static void Main(string[] args)  
  16.         {  
  17.             Program program = new Program(); // Creating Object  
  18.             program.Show("Santosh Kumar Singh","Tanu",101, 20.50,"Biveka"'A'); // Passing arguments of variable length  
  19.         }     
  20.     }  
  21. }  

Output:

Santosh Kumar Singh
Tanu
101
20.5
Biveka
A


C# Command Line Arguments

Arguments that are passed by command line known as command line arguments. We can send arguments to the Main method while executing the code. The string args variable contains all the values passed from the command line.

In the following example, we are passing command line arguments during execution of program.

C# Command Line Arguments Example

  1. using System;  
  2. namespace CSharpProgram  
  3. {  
  4.     class Program  
  5.     {  
  6.         // Main function, execution entry point of the program  
  7.         static void Main(string[] args) // string type parameters  
  8.         {  
  9.             // Command line arguments  
  10.             Console.WriteLine("Argument length: "+args.Length);  
  11.             Console.WriteLine("Supplied Arguments are:");  
  12.             foreach (Object obj in args)  
  13.             {  
  14.                 Console.WriteLine(obj);       
  15.             }  
  16.         }  
  17.     }  
  18. }  

Compile and execute this program by using following commands.

Compile: csc Program.cs

Execute: Program.exe Hi there, how are you?

After executing the code, it produces the following output to the console.

Output:

Argument length: 5
Supplied Arguments are:
Hi
there,
how
are
you?

Lab 09: Publish and subscribe to Event Grid events

  Microsoft Azure user interface Given the dynamic nature of Microsoft cloud tools, you might experience Azure UI changes that occur after t...