How To Pass Class As Params to Function
How can I pass the Parameter to a function. for example
public void GridViewColumns(params Clas开发者_运维问答sName[] pinputparamter)
{
}
and Class is as given below
public Class ClassName
{
public string Name{get;set;}
public int RecordID{get;set;}
}
can anyone has idea?
params
means that the method can accept any number of parameters of type ClassName
. Example of calling it with two instances of ClassName:
GridViewColumns(new ClassName(), new ClassName());
or
ClassName a = new ClassName();
ClassName b = new ClassName();
ClassName c = new ClassName();
GridViewColumns(a, b, c);
First thing first, you have to create an object of the class in your main().
ClassName myObject = new ClassName();
then you can pass it as a parameter in your function.
GridViewColumns(myObject);
Hope this helps..
Also you can pass instances of ClassName
as Array:
ClassName[] arr = new ClassName[]{new ClassName(), new ClassName()};
GridViewColumns(arr);
More details here.
精彩评论