C# 方法使用汇总(GetEnumerator用法,??用法等)

目录:

1、??运算符使用

2、GetEnumerator方法

3、ResourceManager.GetString方法获得Resources的字符。

4、获得Settings文件的字符。

一、??可能是一个被遗忘的运算符,很少看到有人用它,它的用法很简单却很实用:
variable ?? defaultValue
相当于
variable == null ? defaultValue : variable
有了它,一行便能搞定Lazy Evaluation了:
使用??之前:

public UserAccess Users
{
get
{
if (_users ==  null)
{
_users = Proxy.GetQueryObject<UserAccess>();
}
return _users;
}
}


之后:

public UserAccess Users
{
get
{
return _users ?? (_users = Proxy.GetQueryObject<UserAccess>());
}
}


注:这个运算符只支持引用类型和Nullable类型。

int?就是Nullable<int>,Nullable类型也支持的。

原文:http://www.cnblogs.com/Dah/archive/2007/09/29/910479.html

 

二.GetEnumerator

None.gif下面的示例说明 GetEnumerator 方法的用法。包括在枚举数为活动的情况下从基础 DataTable 中删除行时枚举数的行为。
None.gif
None.gifview plaincopy to clipboardprint?
None.gifpublic static void Main() 
ExpandedBlockStart.gif
InBlock.gif try 
ExpandedSubBlockStart.gif { 
InBlock.gif DataTable userTable = new DataTable("peopleTable"); 
InBlock.gif 
InBlock.gif userTable.Columns.Add("Id", typeof(int)); 
InBlock.gif userTable.Columns.Add("Name", typeof(string)); 
InBlock.gif 
InBlock.gif // Note that even if you create the DataTableReader 
InBlock.gif 
// before adding the rows, the enumerator can still 
InBlock.gif 
// visit all the rows. 
InBlock.gif
 DataTableReader reader = userTable.CreateDataReader(); 
ExpandedSubBlockStart.gif userTable.Rows.Add(new object[] { 1, "Peter" }); 
ExpandedSubBlockStart.gif userTable.Rows.Add(new object[] { 2, "Mary" }); 
ExpandedSubBlockStart.gif userTable.Rows.Add(new object[] { 3, "Andy" }); 
ExpandedSubBlockStart.gif userTable.Rows.Add(new object[] { 4, "Russ" }); 
InBlock.gif 
InBlock.gif IEnumerator enumerator = reader.GetEnumerator(); 
InBlock.gif // Keep track of whether the row to be deleted 
InBlock.gif 
// has actually been deleted yet. This allows 
InBlock.gif 
// this sample to demonstrate that the enumerator 
InBlock.gif 
// is able to survive row deletion. 
InBlock.gif
 bool isRowDeleted = false
InBlock.gif while (enumerator.MoveNext()) 
ExpandedSubBlockStart.gif { 
InBlock.gif DbDataRecord dataRecord = (DbDataRecord)enumerator.Current; 
InBlock.gif 
InBlock.gif // While the enumerator is active, delete a row. 
InBlock.gif 
// This doesn't affect the behavior of the enumerator. 
InBlock.gif
 if (!isRowDeleted) 
ExpandedSubBlockStart.gif { 
InBlock.gif isRowDeleted = true
InBlock.gif userTable.Rows[2].Delete(); 
ExpandedSubBlockEnd.gif } 
InBlock.gif Console.WriteLine(dataRecord.GetString(1)); 
ExpandedSubBlockEnd.gif } 
ExpandedSubBlockEnd.gif } 
InBlock.gif catch (Exception ex) 
ExpandedSubBlockStart.gif { 
InBlock.gif Console.WriteLine(ex); 
ExpandedSubBlockEnd.gif } 
InBlock.gif Console.ReadLine();  

原文:http://www.cnblogs.com/suiqirui19872005/archive/2007/08/11/851752.html

2.2第二种用法

     const  int  times =1000;
 
     public  static 
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C#中,`IEnumerable`是一个接口,用于表示一组可枚举的元素。它定义了一个单一的方法`GetEnumerator()`,该方法返回一个`IEnumerator`对象,用于在集合中按顺序访问元素。 `IEnumerable`接口允许您在集合上执行迭代操作,例如使用foreach循环遍历集合中的元素。通过实现`IEnumerable`接口,您可以将自己的自定义集合类或数据结构与C#的迭代器模式一起使用,并以一种统一的方式处理它们。 以下是一个简单的示例,演示了如何实现和使用`IEnumerable`接口: ```csharp using System; using System.Collections; public class MyCollection : IEnumerable { private int[] data; public MyCollection(int[] array) { data = array; } public IEnumerator GetEnumerator() { return new MyEnumerator(data); } } public class MyEnumerator : IEnumerator { private int[] data; private int position = -1; public MyEnumerator(int[] array) { data = array; } public object Current => data[position]; public bool MoveNext() { position++; return position < data.Length; } public void Reset() { position = -1; } } class Program { static void Main(string[] args) { int[] numbers = { 1, 2, 3, 4, 5 }; MyCollection collection = new MyCollection(numbers); foreach (int number in collection) { Console.WriteLine(number); } } } ``` 在上面的示例中,我们创建了一个名为`MyCollection`的自定义集合类,它实现了`IEnumerable`接口。`MyCollection`类中的`GetEnumerator`方法返回了一个`MyEnumerator`对象,该对象实现了`IEnumerator`接口。 在`Main`方法中,我们创建了一个`MyCollection`实例,并使用foreach循环遍历集合中的元素。通过实现`IEnumerable`接口和`IEnumerator`接口,我们可以使用foreach循环来遍历自定义集合中的元素。 需要注意的是,C# 2.0引入了更方便的迭代器语法,可以简化上述示例中的代码。使用迭代器语法,您可以使用`yield return`语句轻松地定义一个迭代器方法,而无需显式实现`IEnumerable`和`IEnumerator`接口。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值