昨晚我梦见以下是不可能的.但是在同一个梦里,来自SO的人告诉我.因此,我想知道是否可以转换System.Array为List
Array ints = Array.CreateInstance(typeof(int), 5);
ints.SetValue(10, 0);
ints.SetValue(20, 1);
ints.SetValue(10, 2);
ints.SetValue(34, 3);
ints.SetValue(113, 4);
Run Code Online (Sandbox Code Playgroud)
至
List<int> lst = ints.OfType<int>(); // not working
Run Code Online (Sandbox Code Playgroud) 当我有一个代码块
static void Main()
{
foreach (int i in YieldDemo.SupplyIntegers())
{
Console.WriteLine("{0} is consumed by foreach iteration", i);
}
}
class YieldDemo
{
public static IEnumerable<int> SupplyIntegers()
{
yield return 1;
yield return 2;
yield return 3;
}
}
Run Code Online (Sandbox Code Playgroud)
我可以将收益率收益背后的原理解释为
|1| |2| |3| are stored in contiguous memory block.Pointer of "IEnumerator" Moves to |1|澄清:
(1)通常我们将在函数内部允许一个有效的return语句.当多个yield return,yield return,...语句出现时C#如何处理?
(2)一旦遇到回报,就无法再次控制回到SupplyIntegers(),如果允许则不会再从1开始收益?我的意思是收益率1?
当我说
Dictionary<int,string>
Run Code Online (Sandbox Code Playgroud)
它是否相当于两个不同的数组,例如:
int[] keys =new int[] { 1, 2, 3 };
string[] values=new string[]{"val1","val2","val3"};
Run Code Online (Sandbox Code Playgroud) 如何将Enum转换为Key,Value Pairs.我已经用C#3.0转换了它.
public enum Translation
{
English,
Russian,
French,
German
}
string[] trans = Enum.GetNames(typeof(Translation));
var v = trans.Select((value, key) =>
new { value, key }).ToDictionary(x => x.key + 1, x => x.value);
Run Code Online (Sandbox Code Playgroud)
在C#1.0中,这样做的方法是什么?
1)Action委托的真正定义是什么?一些定义描述它是多态条件映射,有人说它*应用决策表*.
(你可能会问你通过了解定义会得到什么,如果我知道它我能理解它的真正目的).
2)感谢Binary Worrier,Andrew Hare的stackoverflow给出了很好的例子.当我宣布
string[] words = "This is as easy as it looks".Split(' ');
`Array.ForEach(words, p => Console.WriteLine(p));`
Run Code Online (Sandbox Code Playgroud)
我可以理解它实际上做了什么.但是当我宣布时,C#如何在我宣布时解释
Dictionary<SomeEnum, Action<User>> methodList =
new Dictionary<SomeEnum, Action<User>>()
methodList.Add(SomeEnum.One, DoSomething);
methodList.Add(SomeEnum.Two, DoSomethingElse);
Run Code Online (Sandbox Code Playgroud)
它是否在字典中存储了Actions的集合?.不幸的是,由于示例不完整,我没有得到它.
3)Action , Function ,Predicate延迟之间的功能区别是什么?
我有一个愚蠢的怀疑.一般来说,"System.Object"实现了"Equals".当我实现IEquatable接口时,我可以给我的"Equals"提供自定义定义(我相信如此).
所以教授类的实现等于
class Professor:System.Object,IEquatable
Run Code Online (Sandbox Code Playgroud)
因为System.Equals和IEquatable的Equals有不同的定义,为什么C#报告错误?.因为我没有覆盖"Equals",甚至没有使用new关键字隐藏"Equals".
class Professor : IEquatable<Professor>
{
public string Name { get; set; }
public bool Equals(Professor cust)
{
if (cust == null) return false;
return cust.Name == this.Name;
}
}
Run Code Online (Sandbox Code Playgroud) 我访问了不同的网站,试图了解使用自定义枚举的实时示例,我正在放弃这一行.我有例子.但他们让我感到困惑.
例
拿1
class NumberArray
{
public int[] scores;
public NumberArray()
{
}
public NumberArray(int[] scores)
{
this.scores = scores;
}
public int[] Scores
{
get {return scores;}
}
}
Run Code Online (Sandbox Code Playgroud)
拿2
public class Enumerator : IEnumerator
{
int[] scores;
int cur;
public Enumerator(int[] scores)
{
this.scores = scores;
cur = -1;
}
public Object Current
{
get {
return scores[cur];
}
}
public void Reset()
{
cur = -1;
}
public bool MoveNext()
{
cur++;
if (cur < scores.Length)
return …Run Code Online (Sandbox Code Playgroud) 当我应用IEnumerator并执行MoverNext()时,它会像C风格一样遍历, 'a' 'p' 'p' 'l' 'e' '\o' 直到找到空字符吗?我认为它会返回整个字符串.枚举如何在这里工作?
string ar = "apple";
IEnumerator enu = ar.GetEnumerator();
while (enu.MoveNext())
{
Console.WriteLine(enu.Current);
}
Run Code Online (Sandbox Code Playgroud)
我输出为
a
p
p
l
e
Run Code Online (Sandbox Code Playgroud) 如果没有初始化,如何为数组赋值?
string[] s={"all","in","all"};
I mean why did not the compile show error?.Normally we need to
initialize ,before assign values.
Run Code Online (Sandbox Code Playgroud)