ienumerable

Interesting use of the C# yield keyword in Nerd Dinner tutorial

廉价感情. 提交于 2019-12-20 09:58:01
问题 Working through a tutorial (Professional ASP.NET MVC - Nerd Dinner), I came across this snippet of code: public IEnumerable<RuleViolation> GetRuleViolations() { if (String.IsNullOrEmpty(Title)) yield return new RuleViolation("Title required", "Title"); if (String.IsNullOrEmpty(Description)) yield return new RuleViolation("Description required","Description"); if (String.IsNullOrEmpty(HostedBy)) yield return new RuleViolation("HostedBy required", "HostedBy"); if (String.IsNullOrEmpty(Address))

How to check if a variable is an IEnumerable of some sort

大憨熊 提交于 2019-12-20 09:56:12
问题 basically I'm building a very generic T4 template and one of the things I need it to do is say print variable.ToString() . However, I want it to evaluate lists and foreach through them and instead print ListItem.ToString() My T4 template does not know what type variable will be ahead of time, that is why this is so generic. But my current code that gets generated looks like this: if(variable!=null) if(variable is IEnumerable) //error here foreach(var item in variable) Write(item.ToString());

Why IList<T> inherits IEnumerable<T> and IEnumerable again

点点圈 提交于 2019-12-20 04:13:38
问题 I have taken a look on IList<T> and ICollection<T> on MSDN by chance, and see that the definition of these two interfaces are: public interface ICollection<T> : IEnumerable<T>, IEnumerable public interface IList<T> : ICollection<T>, IEnumerable<T>, IEnumerable Notice that ICollection<T> inherits from IEnumerable<T> and IEnumerable , that's okay. IList<T> inherits from ICollection<T> , but why IList<T> has to inherit IEnumerable<T> and IEnumerable again? Any there any reason for this? 回答1: The

细说 C# 中的 IEnumerable和IEnumerator接口

妖精的绣舞 提交于 2019-12-20 02:11:41
我们先思考几个问题: 为什么在foreach中不能修改item的值? 要实现foreach需要满足什么条件? 为什么Linq to Object中要返回IEnumerable? 接下来,先开始我们的正文。 自己实现迭代器 .net中迭代器是通过IEnumerable和IEnumerator接口来实现的,今天我们也来依葫芦画瓢。 首先来看看这两个接口的定义: 并没有想象的那么复杂。其中IEnumerable只有一个返回IEnumerator的GetEnumerator方法。而IEnumerator中有两个方法加一个属性。 接下来开发画瓢,我们继承IEnumerable接口并实现: 下面使用原始的方式调用: 有朋友开始说了,我们平时都是通过foreache来取值的,没有这样使用过啊。好吧,我们来使用foreach循环: 为什么说基本上是等效的呢?我们先看打印结果,在看反编译代码。 由此可见,两者有这么个关系: 我们可以回答第一个问题了“为什么在foreach中不能修改item的值?”: 我们还记得IEnumerator的定义吗 接口的定义就只有get没有set。所以我们在foreach中不能修改item的值。 我们再来回答第二个问题:“要实现foreach需要满足什么条件?”: 必须实现IEnumerable接口?NO

C# List Only Contains

梦想的初衷 提交于 2019-12-20 01:55:33
问题 Was hoping to find out if there is an easy way to check if a list only contains certain list values. For example if I have a list of int that could randomly contain distinct ints 1-10 (ie 1,3,7 or 2,3,4,6,8,9) and I want to check if the list only contains int 1 and/or 5. 1 or 5 or 1,5 would return true and anything else would return false. This is actually for an MVC project and is a list of strings. Based on conditions I build a string list and want to check if 1 or both of 2 certain strings

Enumerable giving unexpected output

六眼飞鱼酱① 提交于 2019-12-19 20:37:57
问题 class Foo { public static IEnumerable<int> Range(int start, int end) { return Enumerable.Range(start, end); } public static void PrintRange(IEnumerable<int> r) { foreach (var item in r) { Console.Write(" {0} ", item); } Console.WriteLine(); } } class Program { static void TestFoo() { Foo.PrintRange(Foo.Range(10, 20)); } static void Main() { TestFoo(); } } Expected Output: 10 11 12 13 14 15 16 17 18 19 20 Actual Output: 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 What is the

Multiple IEnumerable implementations paradox

岁酱吖の 提交于 2019-12-19 19:52:11
问题 I have a generic class A< T >, that implements IEnumerable< T[] >. I want to have a convenience wrapper B that inherits from A< char > and implements IEnumerable< string >. public class A<T> : IEnumerable<T[]> { public IEnumerator<T[]> GetEnumerator() { return Enumerate().GetEnumerator(); } IEnumerator IEnumerable.GetEnumerator() { return GetEnumerator(); } protected IEnumerable<T[]> Enumerate() { throw new System.NotImplementedException(); } } public class B : A<char>, IEnumerable<string> {

Reason for KeyNotFoundException in Dictionary initialization

倖福魔咒の 提交于 2019-12-19 17:45:51
问题 The following code new Dictionary<string, List<int>> { ["a"] = {1}, }; Throws a run-time KeyNotFoundException , albeit that {1} is a perfectly well-formed array (i.e. int[] a = {1,2,3,4} being valid code). Changing the TValue of the Dictionary to int[] , throws a compile-time CS1061 , but this does not (note the added new[] array-allocation): new Dictionary<string, IEnumerable<int>> { ["a"] = new[]{1}, }; Why does this happen? 回答1: Your first piece of code is using a collection initializer,

IEnumerable<T> and .Where Linq method behaviour?

陌路散爱 提交于 2019-12-19 15:02:05
问题 I thought I know everything about IEnumerable<T> but I just met a case that I cannot explain. When we call .Where linq method on a IEnumerable , the execution is deferred until the object is enumerated, isn't it? So how to explain the sample below : public class CTest { public CTest(int amount) { Amount = amount; } public int Amount { get; set; } public override string ToString() { return $"Amount:{Amount}"; } public static IEnumerable<CTest> GenerateEnumerableTest() { var tab = new List<int>

IEnumerable<T> and .Where Linq method behaviour?

自闭症网瘾萝莉.ら 提交于 2019-12-19 15:01:17
问题 I thought I know everything about IEnumerable<T> but I just met a case that I cannot explain. When we call .Where linq method on a IEnumerable , the execution is deferred until the object is enumerated, isn't it? So how to explain the sample below : public class CTest { public CTest(int amount) { Amount = amount; } public int Amount { get; set; } public override string ToString() { return $"Amount:{Amount}"; } public static IEnumerable<CTest> GenerateEnumerableTest() { var tab = new List<int>