Csharp/CSharp Tutorial/Language Basics/params

Материал из .Net Framework эксперт
Версия от 12:19, 26 мая 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

Array As Actual Param: params int[] inVals

using System;
class MainClass {
   public static void ListInts(params int[] inVals)
   {
      if (inVals != null)
         for (int i = 0; i < inVals.Length; i++)   
            inVals[i] = inVals[i] * 10;
   }
   static void Main()
   {
      int[] MyArr = new int[] { 5, 6, 7 };         
      ListInts(MyArr);                          
      foreach (int x in MyArr)
         Console.WriteLine("{0}", x);              
   }
}
50
60
70

Demonstrate params

using System; 
 
class Min { 
  public int minVal(params int[] nums) { 
    int m; 
 
    if(nums.Length == 0) { 
      Console.WriteLine("Error: no arguments."); 
      return 0; 
    } 
 
    m = nums[0]; 
    for(int i=1; i < nums.Length; i++)  
      if(nums[i] < m) m = nums[i]; 
 
    return m; 
  } 
} 
 
class MainClass { 
  public static void Main() { 
    Min ob = new Min(); 
    int min; 
    int a = 10, b = 20; 
 
    // call with two values 
    min = ob.minVal(a, b); 
    Console.WriteLine("Minimum is " + min); 
 
    // call with 3 values 
    min = ob.minVal(a, b, -1); 
    Console.WriteLine("Minimum is " + min); 
 
    // call with 5 values 
    min = ob.minVal(18, 23, 3, 14, 25); 
    Console.WriteLine("Minimum is " + min); 
 
    // can call with an int array, too 
    int[] args = { 45, 67, 34, 9, 112, 8 }; 
    min = ob.minVal(args); 
    Console.WriteLine("Minimum is " + min); 
  } 
}
Minimum is 10
Minimum is -1
Minimum is 3
Minimum is 8

Mix params array type with other data types

using System;
class MainClass
{
  public static void ArrayOfInts(string msg, params int[] list) 
  {
    Console.WriteLine(msg);
    for ( int i = 0 ; i < list.Length ; i++ )
      Console.WriteLine(list[i]);
    Console.WriteLine();
  }
  public static void Main() 
  {
    int[] intArray = new int[3] {10,11,12};
    ArrayOfInts("Here is an array of ints", intArray);
    ArrayOfInts("Enjoy these 3 ints", 1, 2, 3);
    ArrayOfInts("Take some more!", 55, 4, 983, 10432, 98, 33);
  }
}
Here is an array of ints
10
11
12
Enjoy these 3 ints
1
2
3
Take some more!
55
4
983
10432
98
33

Sending in args using params keyword: object, int and string

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

Use "params int[]"

using System;
class MainClass
{
   static void Main() {
      int first = 5, second = 6, third = 7;
      ListInts(first, second, third);
      Console.WriteLine("{0}, {1}, {2}", first, second, third);
   }
   public static void ListInts(params int[] inVals)
   {
      for (int i = 0; i < inVals.Length; i++)        
      {
         inVals[i] = inVals[i] * 10;
         Console.WriteLine("{0} ", inVals[i]);       
      }
   }
   
}
50
60
70
5, 6, 7

Use regular parameter with a params parameter.

using System; 
 
class MyClass { 
  public void showArgs(string msg, params int[] nums) { 
    Console.Write(msg + ": "); 
 
    foreach(int i in nums) 
      Console.Write(i + " "); 
 
    Console.WriteLine(); 
  } 
} 
 
class MainClass { 
  public static void Main() { 
    MyClass ob = new MyClass(); 
 
    ob.showArgs("Here are some integers",  
                1, 2, 3, 4, 5); 
 
    ob.showArgs("Here are two more",  
                17, 20); 
 
  } 
}
Here are some integers: 1 2 3 4 5
Here are two more: 17 20