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

下面的示例说明 GetEnumerator 方法的用法。包括在枚举数为活动的情况下从基础 DataTable 中删除行时枚举数的行为。

view plaincopy to clipboardprint?
public static void Main() 

 try 
 
 DataTable userTable = new DataTable("peopleTable"); 
 
 userTable.Columns.Add("Id", typeof(int)); 
 userTable.Columns.Add("Name", typeof(string)); 
 
 // Note that even if you create the DataTableReader 
 
// before adding the rows, the enumerator can still 
 
// visit all the rows. 
 DataTableReader reader = userTable.CreateDataReader(); 
 userTable.Rows.Add(new object[] { 1, "Peter" }); 
 userTable.Rows.Add(new object[] { 2, "Mary" }); 
 userTable.Rows.Add(new object[] { 3, "Andy" }); 
 userTable.Rows.Add(new object[] { 4, "Russ" }); 
 
 IEnumerator enumerator = reader.GetEnumerator(); 
 // Keep track of whether the row to be deleted 
 
// has actually been deleted yet. This allows 
 
// this sample to demonstrate that the enumerator 
 
// is able to survive row deletion. 
 bool isRowDeleted = false
 while (enumerator.MoveNext()) 
 
 DbDataRecord dataRecord = (DbDataRecord)enumerator.Current; 
 
 // While the enumerator is active, delete a row. 
 
// This doesn't affect the behavior of the enumerator. 
 if (!isRowDeleted) 
 
 isRowDeleted = true
 userTable.Rows[2].Delete(); 
 }
 
 Console.WriteLine(dataRecord.GetString(1)); 
 }
 
 }
 
 catch (Exception ex) 
 
 Console.WriteLine(ex); 
 }
 
 Console.ReadLine();  

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

2.2第二种用法

     const  int  times =1000;
 
     public  static  void  Test2()
    
       Stopwatch watch = new  Stopwatch();
       Hashtable hastable = new  Hashtable();
       
         for  ( int  i = 0; i < 10000; i++)
         {
             hastable.Add(i, i.ToString() + "值" );
         }
         //测试GetEnumerator
         watch.Start();
         for  ( int  i = 0; i < times; i++)
         {
             IDictionaryEnumerator enumerator = hastable.GetEnumerator();
             while  (enumerator.MoveNext())
             {
                 string  key = enumerator.Key.ToString();
                 string  value = enumerator.Value.ToString();
             }
         }
         watch.Stop();
         Console.WriteLine( "Hashtable GetEnumerator耗时"  + watch.ElapsedMilliseconds);
         Console.WriteLine( "---------------" );
 
         watch.Reset();
         //测试ForEach
         watch.Start();
         for  ( int  i = 0; i < times; i++)
         {
             foreach  ( object  item in  hastable.Keys)
             {
                 string  key = item.ToString();
                 string  value = hastable[item].ToString();
             }
         }
         watch.Stop();
         Console.WriteLine( "Hashtable ForEach耗时"  + watch.ElapsedMilliseconds);
         Console.WriteLine( "---------------" );
 
         watch.Reset();
         Dictionary< int , string > dictionary = new  Dictionary< int , string >();
         for  ( int  i = 0; i < 10000; i++)
         {
             dictionary.Add(i, i.ToString() + "值" );
         }
         watch.Start();
         for  ( int  i = 0; i < times; i++)
         {               
             Dictionary< int , string >.Enumerator enumerator = dictionary.GetEnumerator();
             while  (enumerator.MoveNext())
             {
                 int  key = enumerator.Current.Key;
                 string  value = enumerator.Current.Value;
             }
         }
         watch.Stop();
         Console.WriteLine( "Dictionary GetEnumerator耗时"  + watch.ElapsedMilliseconds);
         Console.WriteLine( "---------------" );
 
         watch.Reset();
         //测试ForEach
         watch.Start();
         for  ( int  i = 0; i < times; i++)
         {
             foreach  ( int  item in  dictionary.Keys)
             {
                 int  key = item;
                 string  value = dictionary[item];
             }
         }
         watch.Stop();
         Console.WriteLine( "Dictionary ForEach耗时"  + watch.ElapsedMilliseconds);
         Console.WriteLine( "---------------" );
         Console.ReadKey();
     }
}

  原文:http://www.cnblogs.com/scottckt/archive/2011/05/16/2048243.html

 三、获得Resources的字符。通过ResourceManager.GetString方法获得定义在Properties.Resources的String字符。WpfApplicationSomeMethodTest.Properties.Resources.ResourceManager.GetString("TestString");


四、获得Settings文件的字符。WpfApplicationSomeMethodTest.Properties.Settings.Default.DefaultFolder;

 


本文转自Work Hard Work Smart博客园博客,原文链接:http://www.cnblogs.com/linlf03/archive/2011/10/08/2201713.html,如需转载请自行联系原作者

目录
相关文章
|
数据采集 数据可视化 测试技术
C#生成Selenium测试报告:实用方法与技巧
在C#中使用Selenium进行自动化测试时,结合代理IP和ExtentReports能增强测试安全性和报告质量。安装必备工具如Selenium WebDriver、NUnit和ExtentReports。在测试设置中,配置代理(如亿牛云爬虫代理)以隐藏IP,通过ChromeOptions定制UserAgent,并添加Cookie。测试代码示例展示了如何打开网页、执行搜索并生成详细的测试报告。使用ExtentReports可创建可视化测试结果,便于团队分析。
185 5
C#生成Selenium测试报告:实用方法与技巧
|
12月前
|
开发框架 .NET 程序员
C# 去掉字符串最后一个字符的 4 种方法
在实际业务中,我们经常会遇到在循环中拼接字符串的场景,循环结束之后拼接得到的字符串的最后一个字符往往需要去掉,看看 C# 提供了哪4种方法可以高效去掉字符串的最后一个字符
916 0
|
7月前
|
C#
C# Hashtable的用法
哈希表(HashTable)是一种通过键值对直接访问的数据结构。Add 方法用于添加成员,先检查成员是否已存在,若不存在则计算其 ASCII 码值作为散列值并添加到表中。Remove 方法用于移除成员,Size 方法返回集合成员数量。代码实现了这些功能,确保集合操作的高效性。
|
11月前
|
编译器 C#
C#多态概述:通过继承实现的不同对象调用相同的方法,表现出不同的行为
C#多态概述:通过继承实现的不同对象调用相同的方法,表现出不同的行为
214 65
|
10月前
|
JSON 程序员 C#
使用 C# 比较两个对象是否相等的7个方法总结
比较对象是编程中的一项基本技能,在实际业务中经常碰到,比如在ERP系统中,企业的信息非常重要,每一次更新,都需要比较记录更新前后企业的信息,直接比较通常只能告诉我们它们是否指向同一个内存地址,那我们应该怎么办呢?分享 7 个方法给你!
339 2
|
10月前
|
C# UED SEO
C# 异步方法async / await任务超时处理
通过使用 `Task.WhenAny`和 `Task.Delay`方法,您可以在C#中有效地实现异步任务的超时处理机制。这种方法允许您在指定时间内等待任务完成,并在任务超时时采取适当的措施,如抛出异常或执行备用操作。希望本文提供的详细解释和代码示例能帮助您在实际项目中更好地处理异步任务超时问题,提升应用程序的可靠性和用户体验。
441 3
|
10月前
|
C#
c#中switch case语句的用法
C#中的 `switch case`语句提供了一种简洁而高效的方式来处理多个条件分支。通过了解其基本语法、注意事项和高级用法,可以在实际开发中灵活运用 `switch case`,提高代码的可读性和维护性。希望本文能帮助你更好地理解和使用C#中的 `switch case`语句。
598 0
|
11月前
|
存储 C#
【C#】大批量判断文件是否存在的两种方法效率对比
【C#】大批量判断文件是否存在的两种方法效率对比
284 1
|
C#
C#一分钟浅谈:Lambda 表达式和匿名方法
本文详细介绍了C#编程中的Lambda表达式与匿名方法,两者均可用于定义无名函数,使代码更简洁易维护。文章通过基础概念讲解和示例对比,展示了各自语法特点,如Lambda表达式的`(parameters) =&gt; expression`形式及匿名方法的`delegate(parameters)`结构。并通过实例演示了两者的应用差异,强调了在使用Lambda时应注意闭包问题及其解决策略,推荐优先使用Lambda表达式以增强代码可读性。
166 8
|
11月前
|
C#
C#的方法的参数传递
C#的方法的参数传递
112 0