work hard work smart

专注于Java后端开发。 不断总结,举一反三。
随笔 - 1158, 文章 - 0, 评论 - 153, 阅读 - 186万
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

目录:

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;

 

 


 

 

编辑推荐:
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
阅读排行:
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【译】Visual Studio 中新的强大生产力特性
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 字符编码:从基础到乱码解决
点击右上角即可分享
微信分享提示