我偶然会使用这种结构:
Dictionary<string, List<string>> Foo = new Dictionary<string, List<string>>();
Run Code Online (Sandbox Code Playgroud)
这导致了这种代码:
foreach (DataRow dr in ds.Tables[0].Rows)
{
List<string> bar;
if (!Foo.TryGetValue(dr["Key"].ToString(), out desks))
{
bar= new List<string>();
Foo.Add(dr["Key"].ToString(), bar);
}
bar.Add(dr["Value"].ToString());
}
Run Code Online (Sandbox Code Playgroud)
你认为编写自定义的DictionaryOfList类是否值得自动处理这类事情?
还有另一种懒惰地初始化这些列表的方法吗?
我有一个声明如下的属性:
public decimal? MyProperty { get; set; }
Run Code Online (Sandbox Code Playgroud)
我需要将此值作为字符串传递给另一个方法,因此我看到这样做的唯一方法如下:
MyProperty == null ? null : MyProperty.ToString()
Run Code Online (Sandbox Code Playgroud)
当您将许多类似的属性传递给方法时,这看起来非常混乱.
有谁知道是否有更好,更简洁的写作方式?
哦,如果有人能想到这个问题的更合适的标题,请随时改变它...
我知道可以在类中引发一个实现声明发生的事件,但我希望在基类级别引发事件并引发派生类的事件:
public interface IFoo
{
event EventHandler<FooEventArgs> FooValueChanged;
void RaiseFooValueChanged(IFooView sender, FooEventArgs e);
}
[TypeDescriptionProvider(typeof(FooBaseImplementor))]
public abstract class FooBase : Control, IFoo
{
public virtual event EventHandler<FooEventArgs> FooValueChanged;
public void RaiseFooValueChanged(IFooView sender, FooEventArgs e)
{
FooValueChanged(sender, e);
}
}
Run Code Online (Sandbox Code Playgroud)
我不能拥有FooValueChanged事件摘要,因为基类不能引发事件.当前代码运行,但调用FooValueChanged(sender,e)抛出NullReferenceException,因为它不调用派生类的事件,只调用基类的事件.
我哪里错了?
我可以将事件和提升者都抽象化,但是我需要记住在每个派生类中调用FooValueChanged(sender,e).我试图避免这种情况,同时能够使用Visual Studio设计器进行派生控件.
我知道这是一个广泛的话题,但我对.NET的所谓最佳实践感兴趣,尽管我正在寻找不那么明显的主题,不像"用作代替转换".
让我们看看我可以从Stack Overflow用户那里学到什么有趣的东西.
这是一个解决方案,使C#Windows窗体中的图像没有任何dll并以有效,快速的方式产生负面影响?
我一直在搜索.net 3.0和3.5框架的内容列表,因为我一直在使用旧技术编程,例如哈希表而不是字典(更新的技术).
我一直在打扰,想知道在哪里可以找到C#和.Net框架的所有最新功能的列表,这样我就可以开始了解如何使用一些东西.
非常感谢帮助!
我知道我可以像这样扩展字符串类:
public static class EMClass
{
public static int ToInt32Ext(this string s)
{
return Int32.Parse(s);
}
public static int ToInt32Static(string s)
{
return Int32.Parse(s);
}
}
Run Code Online (Sandbox Code Playgroud)
并像这样使用它:
string x = "12";
x.ToInt32Ext()
Run Code Online (Sandbox Code Playgroud)
但是我怎么能把它带到我可以这样使用的地方:
int x = string.ToInt32("15");
Run Code Online (Sandbox Code Playgroud) 我正在寻找一种写这样的东西的方法:
if (product.Category.PCATID != 10 && product.Category.PCATID != 11 && product.Category.PCATID != 16) { }
Run Code Online (Sandbox Code Playgroud)
以下面的简写方式,当然不起作用:
if (product.Category.PCATID != 10 | 11 | 16) { }
Run Code Online (Sandbox Code Playgroud)
那么有什么简便方法可以做类似的事情吗?
Stopwatch stopwatch1 = new Stopwatch();
Stopwatch stopwatch2 = new Stopwatch();
string l = "my test";
string u = "MY TEST";
for (int i = 0; i < 25; i++)
{
l += l;
u += u;
}
stopwatch1.Start();
l=l.ToUpper();
stopwatch1.Stop();
stopwatch2.Start();
u=u.ToLower();
stopwatch2.Stop();
// Write result.
Console.WriteLine("Time elapsed: \nUPPER : {0}\n LOWER : {1}",
stopwatch1.Elapsed, stopwatch2.Elapsed);
Run Code Online (Sandbox Code Playgroud)
我跑了很多次:
UPPER : 00:00:01.3386287
LOWER : 00:00:01.4546552
UPPER : 00:00:01.1614189
LOWER : 00:00:01.1970368
UPPER : 00:00:01.2697430
LOWER : 00:00:01.3460950
UPPER : 00:00:01.2256813
LOWER : …Run Code Online (Sandbox Code Playgroud) c# ×8
.net ×6
.net-3.5 ×2
c#-3.0 ×2
winforms ×2
c#-4.0 ×1
collections ×1
events ×1
gdi+ ×1
if-statement ×1
imaging ×1
inheritance ×1
nullable ×1
properties ×1