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  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,如需转载请自行联系原作者

目录
相关文章
|
4天前
|
安全 C#
C# List基本用法
C# List基本用法
|
4天前
|
C#
C# Dev chartControl的用法
C# Dev chartControl的用法
|
4天前
|
Java 调度 C#
C#学习系列相关之多线程(一)----常用多线程方法总结
C#学习系列相关之多线程(一)----常用多线程方法总结
|
4天前
|
C#
C#学习相关系列之数组---常用方法使用(二)
C#学习相关系列之数组---常用方法使用(二)
|
4天前
|
安全 编译器 C#
C#学习相关系列之多线程---lock线程锁的用法
C#学习相关系列之多线程---lock线程锁的用法
|
4天前
|
开发框架 .NET C#
C#学习相关系列之Linq用法---where和select用法(二)
C#学习相关系列之Linq用法---where和select用法(二)
|
4天前
|
程序员 C#
C#抽象类和抽象方法详解
C#抽象类和抽象方法详解
10 0
|
4天前
|
存储 开发框架 .NET
C#中将DataTable转化成ListT的方法解析
C#中将DataTable转化成ListT的方法解析
9 0
|
4天前
|
数据采集 前端开发 数据挖掘
Fizzler库+C#:从微博抓取热点的最简单方法
本文介绍如何使用Fizzler库和C#构建微博热点信息爬虫。通过Fizzler的CSS选择器定位关键信息,提取热点标题和排名,实现微博内容的智能挖掘。示例代码展示单线程和多线程采集方法,并涉及代理IP使用。
Fizzler库+C#:从微博抓取热点的最简单方法
|
4天前
|
存储 数据采集 API
C# GetField 方法应用实例
C# GetField 方法应用实例