Saturday, August 25, 2007

Params > Using Param Object

USING PARAMS OBJECT
• Use of params allows to pass an array of parameters of same type, params object allows to pass an array of objects as parameter, where every object can refer to a different type
• params object is an array of objects that are a type of System.Object
• params object can be used to declare a method that accepts any number of arguments of type System.Object where each element in the array is an object that may refer to a different type
• Declaration is as under:
class Black
{
public static void Hole(params object [] paramList)
}
• The method hole can be passed any number of objects, even no objects at all
• In case of no objects, a zero length object array is passed, shown as under:
Black.Hole(); // is interpreted as: Black.Hole(new object [0])
Black.Hole(null); // Being reference type, initializing to null is allowed
• Some sample code to call the Hole method as under:
object[] array = new object[2];
array[0] = “twenty three”;
array[1] = 23;
Black.Hole(array);
• Alternatively, Hole can be called passing parameters like any other method, as under:
Black.Hole(“forty two”, 42); // Black.Hole(new object[] {“forty two”, 42});

No comments:

Post a Comment