如何在c#中返回一个列表? [英] how to return a list in c#?

查看:984
本文介绍了如何在c#中返回一个列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

列表< sfdclistinfo> IstsfdcInfo =  new 列表< sfdclistinfo>(); 





i需要返回列表,如果它包含数据并将它们绑定到datagridview



[edit]已添加代码块 - OriginalGriff [/ edit]

解决方案

第一:



列表是泛型类型。换句话说,它们必须是某物的列表。你需要告诉它 是什么:

  //  < span class =code-comment> List< t> list = new List< t>()其中T是Type。 

List< int> intList = new List< int>(); // a整数列表

intList.Add( 1 ); // 将整数添加到列表中
intList.Add( 256 );

if (intList.Any()) // linq方式说intList.Count()> 0
Console.WriteLine(intList.Count());

// out:> 2





列表类型可以是任何类型。我在这里使用了一个原语,但它们通常用于存储复杂和用户定义的类型。



List的替代方案包括但不限于:



  //  数组 
int [] intArray = new int [ 2 ]; // 创建一个int数组,但它有一个固定的大小(2)。
< span class =code-comment> // Dictionary
Dictionary< int,string> = new 字典< int,string>(); // 用法非常不同。

此类型存储一个键(int)用于访问它的值(字符串)。



所有这些集合都是通用的,如果没有用类型指定就无法实现(好的,所以Arrays有点不一样但仍然必须打字)。



有关泛型的更多信息,请看这个链接

在ASP.NET中使用C#的泛型示例 [ ^ ]


该列表不包含数据 - 它只是创建一个新的空列表。

如果你想向它添加项目(来自某些来源)那么使用DataGridView非常非常容易:

  public   class  ProdTotalSales 
{
public string 文字{ get ; set ; }
public int 值{ get ; set ; }
私有 字符串隐藏{获取; set ; }
public ProdTotalSales( string s, string h, int i)
{
Text = s;
隐藏= h;
价值= i;
}
}



 ProdTotalSales p1 =  new  ProdTotalSales(  P1  p1 hidden 1 ); 
ProdTotalSales p2 = new ProdTotalSales( P2 p2 hidden 2 );
List< prodtotalsales> list = new List< prodtotalsales>(){p1,p2};
myDataGridView.DataSource = list;



将为您提供一个包含两列的DataGridView(一个用于类中的每个公共属性)和两行(一个用于每个类的实例)列表)。


List<sfdclistinfo> IstsfdcInfo = new List<sfdclistinfo>();



i need to return the list if it contains data and bind them to datagridview

[edit]Code block added - OriginalGriff[/edit]

解决方案

First:

List are a generic type. In other words they must be a list of something. You need to tell it what that something is:

//List<t> list = new List<t>() where T is a Type.

List<int> intList = new List<int>(); //a List of integers

intList.Add(1);  // add integer to the list
intList.Add(256);

if(intList.Any()) // a linq way of saying intList.Count()>0
 Console.WriteLine(intList.Count());

//out:> 2



The list Type can be any type. I used a primitive here but they are often used for storing complex and user defined types.

Alternatives to List include, but are not limited to:

//Array
int[] intArray = new int[2]; // creates an array of ints but it has a fixed size (2).
//Dictionary
Dictionary<int,string> = new Dictionary<int,string>(); //very different usage.

This type stores a key(int) that is used to access its value(string).

All of these collections are generic and cannot be instanciated without being specified with a type (ok, so Arrays are a little different but the still must be typed).

For more information on generics please see this link
An Example of Generics Using C# in ASP.NET[^]


That list won't contain data - it just creates a new, empty list.
If you want to add items to it (from some source) then it's very, very easy to use it with a DataGridView:

public class ProdTotalSales
    {
    public string Text { get; set; }
    public int Value { get; set; }
    private string Hidden { get; set; }
    public ProdTotalSales(string s, string h, int i)
        {
        Text = s;
        Hidden = h;
        Value = i;
        }
    }


ProdTotalSales p1 = new ProdTotalSales("P1", "p1 hidden", 1);
ProdTotalSales p2 = new ProdTotalSales("P2", "p2 hidden", 2);
List<prodtotalsales> list = new List<prodtotalsales>() { p1, p2 };
myDataGridView.DataSource = list;


Will give you a DataGridView with two columns (one for each public property in the class) and two rows (one for each instance of the class in the List).


这篇关于如何在c#中返回一个列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆