Params Keyword in C#

By
It is very good feature of  c# language.we can use params keyword only array type parameter.when we are using params keyword in any method then we can directly pass the parameter  to the method.
If we do not use params keyword in array type parameter then there are some extra work which are given below:
  • Declare of array first
  • Initialization of array value
  • after that parameter pass to the method
This is time consuming process and program efficiency also decreases.To avoid this concept we should use params keyword in your programs.But there are some rules to use params keyword in programs.
  • Method take array type parameter
  • Params keyword should be only one in any method.
  • params keyword will be last parameter in any method if method have more than one parameter
There are some reasons to use params keyword at the last in any method.
  • If we dot not put params keyword at last in method then program will give error,because if we use it to first and we pass some value to the method from main method .then method will confuse that which value store in param keyword variable and which value store to other variable.
  • To solve this kind of problem we always put params keyword  parameter at last in method.
Now i am going to implement params keyword concept using programming which are given below;
Step 1- First open your visual studio->File->New->Project->Press OK->and write the following program which is given below:
see it

using System;
namespace paramskeyword
{
    class Program
    {

        public class student
        {
            public void show(int a,params String[] str)
            {
                if (str.Length > 0)
                {
                for (int i = 0; i < str.Length; i++)
                 {
                Console.WriteLine("first integer value is" + " " +a);
                Console.WriteLine("array str value is" + " " + i + " " + str[i]);
                Console.ReadLine();
                 }
            }
        }
        static void Main(string[] args)
        {
           
            student obj = new student();
            obj.show(25,"ankit", "ramesh", "rohan","shayam");
            
            
        }
    }
}
Step 2- Now Run the program(press F5).
output:


Description:
Here i have created a student class ,Within the student ,i have created a show method with two parameter.
  • Integer type value
  • array type value(params keyword).
After that i have created the object(obj) of student class .then i have passed some values to the show function using class object(obj).
Ex.obj.show(25,"ankit", "ramesh", "rohan","shayam");
I hope this is helpful for you 
To Get the Latest  Free Updates Subscribe
Click below for download whole application
   Download

0 comments:

Post a Comment

Powered by Blogger.