简体   繁体   English

如何将类作为参数传递给函数

[英]How To Pass Class As Params to Function

How can I pass the Parameter to a function. 如何将参数传递给函数。 for example 例如

 public void GridViewColumns(params ClassName[] 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 . params意味着该方法可以接受ClassName类型的任意数量的参数。 Example of calling it with two instances of ClassName: 用两个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(). 首先,您必须在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实例作为Array传递:

ClassName[] arr = new ClassName[]{new ClassName(), new ClassName()};
GridViewColumns(arr);

More details here . 更多细节在这里

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM