This topic has been archived. It cannot be replied.
-
工作学习 / 学科技术讨论 / C#中的yield关键字,看了帮助也没明白, 有懂行的吗?
-tonyhao(tonyhao);
2008-12-4
(#4895878@0)
-
http://www.alteridem.net/2007/08/22/the-yield-statement-in-c/
-anthonylo2008(anthonylo);
2008-12-4
(#4895970@0)
-
see my two methodsIEnumerable<int> GetEnumerable(int I, int J){
{
for (int i = I; i < J; i++)
{
yield return i;
}
}
Above method gets the result similar to following:
IEnumerable<int> GetEnumerable(int I, int J){
{
List<int> list = new Lis<int>();
for (int i = I; i < J; i++)
{
List.Add(i);
}
return (IEnumerable<int>)list;
}
-deep_blue(BLUE);
2008-12-5
{386}
(#4897135@0)
-
不错不错,一目了然。想起俺以前有一个面试其中一道题就是写出与yield相等值的C#代码
-binghongcha76(一只大猫);
2008-12-5
(#4898270@0)
-
多谢. 再多问一个. 这个Singleton安全吗?Class Singleton
{
private Singleton(){}
public static readonly Singleton Instance = new Singleton();
}
-tonyhao(tonyhao);
2008-12-5
{110}
(#4898492@0)
-
Can you tell the difference between your code and following code?Class Singleton
{
private static Singleton instance;
private Singleton(){}
public static Singleton Instance
{
get
{
if (instance == null)
{
instance = new Singleton();
}
return instance;
}
}
}
-deep_blue(BLUE);
2008-12-5
{296}
(#4898607@0)
-
你的加上volatile就安全了.
-tonyhao(tonyhao);
2008-12-5
(#4899026@0)
-
Not really.If you are talking about thread-safe Singleton, you should lock on an addition object (this way it avoids dead locks) when initializing Singleton object itself.
Singleton itself should also be sealed (cannot be inherited).
See http://msdn.microsoft.com/en-us/library/ms998558.aspx.
-deep_blue(BLUE);
2008-12-6
{290}
(#4900143@0)
-
让人迷惑的是: singleton必须是sealed 的吗?
-tonyhao(tonyhao);
2008-12-6
(#4900185@0)
-
No. But in multi-threading environment, child class’s additional parameters initializing are not locked. It’ll cause thread-safe problem.
-deep_blue(BLUE);
2008-12-6
(#4900291@0)
-
你是指子类的构造函数?
-tonyhao(tonyhao);
2008-12-7
(#4901203@0)
-
I mean the whole initialization of the inherited object. Of course, when you call its constructor, it conducts the initialization.
-deep_blue(BLUE);
2008-12-8
(#4903405@0)