摘要:玩转C#可有趣了。在这篇C#编程实例中,我们将介绍如何用不同的方法创建对象列表。因此,场景是这样的,有一个MVC应用程序,我需要将5个空行(5个对象列表)绑定到grid表格,以便批量插入记录。所以无论何时我打开那个页面,grid在可编辑模式下呈现5个空行。
玩转C#可有趣了。在这篇C#编程实例中,我们将介绍如何用不同的方法创建对象列表。因此,场景是这样的,有一个MVC应用程序,我需要将5个空行(5个对象列表)绑定到grid表格,以便批量插入记录。所以无论何时我打开那个页面,grid在可编辑模式下呈现5个空行。
在这篇文章中,为了更好的说明,我使用了“Book”的例子。举个例子,假如我想添加多本书到一个图书馆管理软件。首先,让我们创建一个基本的POCO类——Book——它有一些属性,看起来就像这样:
public class Book
{
public string BookName { get; set; } = string.Empty;
public string Author { get; set; } = string.Empty;
public string ISBN { get; set; } = string.Empty;
}
好的,让我们从基本语法到高级语法开始探索吧。在C#3.0之前,要添加对象并初始化集合,我们经常是这样做的:
var bookList = new List<Book>();// Intiazize the object and add it to the collectionvar book1 = new Book();
bookList.Add(book1);var book2 = new Book();
bookList.Add(book2);
另一种方法是使用“for循环”,如下所示:
var bookList = new List<Book>();// Another one is using for loopfor(int i = 0; i < 2; i++)
{
bookList.Add(new Book());
}
另一种方法是使用AddRange()方法,它将对象添加到指定的集合中。
var bookList = new List<Book>();// Using AddRange method.
bookList.AddRange(new[] {
new Book(),
new Book()
});
C#3.0带来了很多增强功能。其中之一就是Collection Initializers。这是一个用于创建集合的缩短语法。
// using collection initializervar bookList = new List<Book>()
{
new Book(),
new Book(),
new Book()
};
在.NET框架中,有一个类——Enumerable——居于“System.Linq”命名空间下。这个类包含一些静态方法,我们可以使用这些静态方法创建对象列表。例如,使用Enumerable.Repeat()方法:
// using Enumerable.Repeatvar bookList = Enumerable.Repeat(new Book(), 2).ToList();
在上面的方法中,第一个参数是我们想要创建或重复的对象。第二个参数是我们需要重复对象的次数。
使用Enumerable.Range()方法的另一个示例:
// and another one is Enumerable.Repeatvar bookList = Enumerable.Range(1, 2).Select(i => new Book()).ToList();
Range()方法会在指定范围内生成一个集合。请注意,这种方法有很多很多用例。
好吧,但我想在另一个应用程序中使用它,所以我决定做一个扩展方法,并使得该扩展方法可以通用。所以,在这里,我创建了两个扩展方法。第一个将添加“N”个对象到列表中。第二个将返回“N”个对象的集合。
public static class UtilityExt
{
///<summary>
/// Add "N" number of objects to the source list.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="emptySource"></param>
/// <param name="number">Number of elements to add</param>
public static void AddNObjectsToCollection<T>(this List<T> emptySource, int number)
where T : new()
{
emptySource.AddRange(Enumerable.Repeat(new T(), number));
}
///<summary>
/// Returns the collection which contains "N" numbers of elements of type T
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="emptySource"></param>
/// <param name="number">Number of elements to return</param>
/// <returns></returns>
public static IList<T> GenerateSpecificNumberOfCollection<T>(this IEnumerable<T> emptySource, int number)
where T : new()
{
return Enumerable.Repeat(new T(), number).ToList();
}
}
你可以像这样调用上面的方法:
// Calling first method - AddNObjectsToCollectionvar bookList = new List<Book>();
bookList.AddNObjectsToCollection(2);// ========== OR ==========// Calling second method - GenerateSpecificNumberOfCollectionvar bookList = new List<Book>().GenerateSpecificNumberOfCollection(3);
好的,上面提到的所有语法都会影响代码的可读性和可重复性。如果你有任何其他可以实现相同结果的方法,欢迎随时分享到评论部分。最后,希望你喜欢这篇文章。
以上,关于C#编程实例的全部内容讲解完毕啦,欢迎大家继续关注!更多关于C#的干货请关注职坐标C#频道!
您输入的评论内容中包含违禁敏感词
我知道了
请输入正确的手机号码
请输入正确的验证码
您今天的短信下发次数太多了,明天再试试吧!
我们会在第一时间安排职业规划师联系您!
您也可以联系我们的职业规划师咨询:
版权所有 职坐标-一站式IT培训就业服务领导者 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
沪公网安备 31011502005948号