I always used (a) Nullable<>.HasValue because I liked the semantics. However, recently I was working on someone else's existing code base where they used (b) Nullable<> != null exclusively instead. Is there a reason to use one over the other, or is it purely preference? (a) int? a; if (a.HasValue) ... (b) int? b; if (b != null) ... The compiler replaces null compar
我总是使用(a) Nullable<>.HasValue因为我喜欢语义。 但是,最近我正在使用其他人现有的代码库(b)仅使用Nullable<> != null 。 是有理由相互使用一个,还是纯粹偏好? (一个) int? a; if (a.HasValue) ... (b)中 int? b; if (b != null) ... 编译器用一个HasValue调用代替空比较,所以没有真正的区别。 只要做到可读性更高或对您和您的同事更有意义。 我更喜欢(a != null)以便语法与引用类
What is the proper ForAll method for the Dictionary object? I am learning the Linq type iteration (method 1 below), but do not getting it to work. The old foreach loop works (method 2 below). Despite many answers on similar questions, eg this question, I do not see a Linq iteration solution. The Dictionary seems to have methods to get all Keys or all Values, usable in the Linq iteration, but
什么是Dictionary对象的正确的ForAll方法? 我正在学习Linq类型的迭代(下面的方法1),但没有得到它的工作。 旧的foreach循环有效(下面的方法2)。 尽管在类似问题上有很多答案,例如这个问题,但我没有看到Linq迭代解决方案。 该词典似乎有方法来获取所有的键或所有值,在Linq迭代中可用,但不能获得所有KeyValuePairs。 奇怪! 我做了一些GetEnumeration() ,但没有多少运气。 AsParallel()方法似乎是解决方案,在
//mDIco is a Dictionnary with string as keys and homemade class (cAsso) as values IEnumerator iterdico = mDico.GetEnumerator(); iterdico.Reset(); while (iterdico.MoveNext()) { var asso = iterdico.Current as cAsso; if (asso != null) { //Code } } I thought this would work, but obviously it doesnt. So how I do i get access to the class which is contained into the value of
//mDIco is a Dictionnary with string as keys and homemade class (cAsso) as values IEnumerator iterdico = mDico.GetEnumerator(); iterdico.Reset(); while (iterdico.MoveNext()) { var asso = iterdico.Current as cAsso; if (asso != null) { //Code } } 我认为这会起作用,但显然它没有。 那么,我如何才能访问包含在我的词典的价值中的类? 问题在于你依赖的是非泛型的IEnumerator接口
How would I iterate through the keys and values of an IDictionary if I don't know the concrete types of the keys and values within, therefore want to just treat them as object s? If I do something like: foreach(var x in myIDictionary) { ... } I think x is an object . But how would I get the Key and Value out of it (both typed as object s), as specified in the IDictionaryEnumerator ? T
如果我不知道关键字和值中的具体类型,我将如何遍历一个IDictionary的键和值,因此只想将它们当作object ? 如果我做了这样的事情: foreach(var x in myIDictionary) { ... } 我认为x是一个object 。 但是,如何按照IDictionaryEnumerator规定获取Key和Value (都作为object的类型)? 没有通用参数的IKeyValuePair没有吗? 我想我可以通过使用MoveNext等手动循环访问枚举器,但是我觉得必须有一种方法可以用foreach
I just want get a 2 dimension array of List in c#. In my thought, below should works but it didn't, the 1 dimension array of List works. I use Unity3D with Mono, however I think it's language related problem. List<MyType>[,] twoDArrayofList;//don't work, it's type is List<MyType> List<MyType>[] oneDArrayofList;//it's works, the type is List<MyType> Anyone know
我只想在c#中获得一个2维的List数组。 在我的想法中,下面应该有效,但它没有,List的1维数组工作。 我使用Unity3D与Mono,但我认为这是与语言相关的问题。 List<MyType>[,] twoDArrayofList;//don't work, it's type is List<MyType> List<MyType>[] oneDArrayofList;//it's works, the type is List<MyType> 任何人都知道什么是错的? 谢谢。 你的意思是“不起作用”? 你遇到了什么错误?
I have a Dictionary whose elements I need to iterate through and make changes. I cannot use foreach statement, since it sometimes throws InvalidOperationException, saying that the collection cannot be modified during an enumaration. I can use a for loop, in conjunction with Dictionary.ElementAt method, and I successfully used it in other classes, but in this particular class, the method Elemen
我有一个字典,其元素需要迭代并进行更改。 我不能使用foreach语句,因为它有时会抛出InvalidOperationException异常,说在集成期间无法修改集合。 我可以使用for循环,并结合Dictionary.ElementAt方法,并成功地将它用在其他类中,但在此特定类中,找不到方法ElementAt! 有任何想法吗? ElementAt是System.Linq.Enumerable中定义的扩展方法。 您需要添加一个使用子句以使其可见: using System.Linq; 请注意,字典&l
I like lists, because they are very easy to use and manage. However, I have a need for a list of multiple elements, like records. I am new to C#, and appreciate all assistance! (Stackoverflow rocks!) Consider this straightforward, working example of a single element list, it works great: public static List<string> GetCities() { List<string> cities = new List<string>();
我喜欢列表,因为它们非常易于使用和管理。 但是,我需要列出多个元素,例如记录。 我是C#的新手,并感谢所有帮助! (Stackoverflow的岩石!) 考虑这个直接的,单个元素列表的工作示例,它很好用: public static List<string> GetCities() { List<string> cities = new List<string>(); cities.Add("Istanbul"); cities.Add("Athens"); cities.Add("Sofia"); return cities; } 如果我希
var dict = new Dictionary<int, string>(); for (int i = 0; i < 200000; i++) dict[i] = "test " + i; I iterated this dictionary using the code below: foreach (var pair in dict) Console.WriteLine(pair.Value); Then, I iterated it using this: foreach (var key in dict.Keys) Console.WriteLine(dict[key]); And the second iteration took ~3 seconds less. I can get both keys and valu
var dict = new Dictionary<int, string>(); for (int i = 0; i < 200000; i++) dict[i] = "test " + i; 我使用下面的代码迭代这个字典: foreach (var pair in dict) Console.WriteLine(pair.Value); 然后,我使用这个迭代它: foreach (var key in dict.Keys) Console.WriteLine(dict[key]); 第二次迭代花费了大约3秒钟的时间。 我可以通过两种方法获得键和值。 我想知道的是,第二种方法是否有缺
I generally use a foreach loop to iterate through Dictionary. Dictionary<string, string> dictSummary = new Dictionary<string, string>(); In this case I want to trim the entries of white space and the foreach loop does however not allow for this. foreach (var kvp in dictSummary) { kvp.Value = kvp.Value.Trim(); } How can I do this with a for loop? for (int i = dictSummary.C
我通常使用foreach循环遍历Dictionary。 Dictionary<string, string> dictSummary = new Dictionary<string, string>(); 在这种情况下,我想修剪空白的条目,但foreach循环不允许这样做。 foreach (var kvp in dictSummary) { kvp.Value = kvp.Value.Trim(); } 我如何使用for循环做到这一点? for (int i = dictSummary.Count - 1; i >= 0; i--) { } KeyValuePair<TKey, TValue>不允许您设置
I am very new to .NET, used to working in PHP. I need to iterate via foreach through a dictionary of objects. My setup is an MVC4 app. The Model looks like this: public class TestModels { Dictionary<int, dynamic> sp = new Dictionary<int, dynamic> { {1, new {name="abc", age="1"}}, {2, new {name="def", age="2"}} } } Controller: public class TestControl
我对.NET很陌生,曾经在PHP中工作过。 我需要通过foreach通过对象字典进行迭代。 我的设置是一个MVC4应用程序。 该模型如下所示: public class TestModels { Dictionary<int, dynamic> sp = new Dictionary<int, dynamic> { {1, new {name="abc", age="1"}}, {2, new {name="def", age="2"}} } } 控制器: public class TestController : Controller { Models.TestModels obj