Sending in args using params keyword: object, int and string : params « Language Basics « C# / CSharp Tutorial






using System;

class Person
{
  public string fullName;
  public int age;

  public Person(string n, int a)
  {
    fullName = n;
    age = a;
  }

  public void PrintInfo()
  {
    Console.WriteLine("{0} is {1} years old", fullName, age);
  }
}

class MainClass
{
  public static void ArrayOfObjects(params object[] list) 
  {
    for ( int i = 0 ; i < list.Length ; i++ )
    {   
      if(list[i] is Person)
      {
        ((Person)list[i]).PrintInfo();
      }
      else
        Console.WriteLine(list[i]);
    }
    Console.WriteLine();
  }

  public static void Main() 
  {
    Person p = new Person("Fred", 93);
    ArrayOfObjects(7, p, "System.String"); 
  }
}
7
Fred is 93 years old
System.String








1.16.params
1.16.1.Demonstrate params
1.16.2.Use regular parameter with a params parameter.
1.16.3.Mix params array type with other data types
1.16.4.Array As Actual Param: params int[] inVals
1.16.5.Use 'params int[]'
1.16.6.Sending in args using params keyword: object, int and string