class NewList<T> : List<T> Why can't I access it's internals like T[] _items , etc? Why aren't they protected, but private? Should I use composition for this? They're private because the author (Microsoft) did not intend for you to access them, probably for safety reasons. You should use composition instead of inheritance. Make your class expose IList<T>
class NewList<T> : List<T> 为什么我不能访问它的内部像T[] _items等? 为什么他们不受保护,但私人? 我应该使用这个组合吗? 它们是私人的,因为作者(微软)不打算让你访问它们,可能是出于安全原因。 你应该使用组合而不是继承。 让你的类直接暴露IList<T>而不是List<T> 。 List<T>不应该直接在公共API中公开(这实际上是在设计准则中指定的)。 它应该仅用作内部实现细节。 L
Recently I used a class that inherits from a collection instead of having the collection instantiated within the class, is this acceptable or does it create unseen problems further down the road? Examples below for the sake of clarity: public class Cars : List<aCar> instead of something like: public class Cars { List<aCar> CarList = new List<aCar>(); } Any thoughts? 问题
最近我使用了一个从集合继承而来的类,而不是在集合中实例化集合,这是可以接受的还是会在后续创建看不见的问题? 为了清楚起见,下面的例子: public class Cars : List<aCar> 而不是像这样的东西: public class Cars { List<aCar> CarList = new List<aCar>(); } 有什么想法吗? 问题在于你的Cars类仍然有从List继承的接口,这可能允许你不需要的操作。 这取决于你班级的最终目的。 如果它只是作
I have a class that inherits from Dictionary<string, string> . Within an instance method, I want to iterate over all KeyValuePair<string, string> 's. I've tried doing the following: foreach (KeyValuePair<string, string> pair in base) But this fails with the following error: Use of keyword 'base' is not valid in this context How can I iterate over the KeyV
我有一个继承自Dictionary<string, string> 。 在一个实例方法中,我想遍历所有KeyValuePair<string, string>的。 我尝试了以下操作: foreach (KeyValuePair<string, string> pair in base) 但是这会失败,并出现以下错误: 关键词'base'的使用在此上下文中无效 我怎样才能迭代在从Dictionary<string, string>派生类的实例方法中的KeyValuePair<string, string> Dictionary<s
I once read an article by Imaar Spaanjars on how to build 3 tier applications. (http://imar.spaanjaars.com/416/building-layered-web-applications-with-microsoft-aspnet-20-part-1) which has formed the basis of my coding for a while now. Thus I implement collections as he has done, by inheriting a List<T> . So if I have a class named Employee,to implement a collection I will also have a cl
我曾经阅读过Imaar Spaanjars关于如何构建3层应用程序的文章。 (http://imar.spaanjaars.com/416/building-layered-web-applications-with-microsoft-aspnet-20-part-1),它已经形成了我现在编码的基础。 因此,我通过继承List<T>实现集合。 所以,如果我有一个名为Employee的类,为了实现一个集合,我还将拥有一个类Employees,如下所示。 class Employee { int EmpID {get;set;} string EmpName {get;set;}
Let's assume this class in C#: public class LimitedList<T> : List<T> { private int _maxitems = 500; public void Add(T value) /* Adding a new Value to the buffer */ { base.Add(value); TrimData(); /* Delete old data if lenght too long */ } private void TrimData() { int num = Math.Max(0, base.Count - _maxitems); base.RemoveRa
我们假设在C#中使用这个类: public class LimitedList<T> : List<T> { private int _maxitems = 500; public void Add(T value) /* Adding a new Value to the buffer */ { base.Add(value); TrimData(); /* Delete old data if lenght too long */ } private void TrimData() { int num = Math.Max(0, base.Count - _maxitems); base.RemoveRange(0,
With C# 6.0 in the VS2015 preview we have a new operator, ?. , which can be used like this: public class A { string PropertyOfA { get; set; } } ... var a = new A(); var foo = "bar"; if(a?.PropertyOfA != foo) { //somecode } What exactly does it do? It's the null conditional operator. It basically means: "Evaluate the first operand; if that's null, stop, with a result o
在VS2015预览版的C#6.0中,我们有一个新的操作符, ?. ,可以这样使用: public class A { string PropertyOfA { get; set; } } ... var a = new A(); var foo = "bar"; if(a?.PropertyOfA != foo) { //somecode } 它究竟做了什么? 它是无效的条件运算符。 它基本上意味着: “评估第一个操作数;如果为null,则停止,结果为null;否则,评估第二个操作数(作为第一个操作数的成员访问)。” 在你的例子中,重点
What are the benefits and drawbacks of the ?: operator as opposed to the standard if-else statement. The obvious ones being: Conditional ?: Operator Shorter and more concise when dealing with direct value comparisons and assignments Doesn't seem to be as flexible as the if/else construct Standard If/Else Can be applied to more situations (such as function calls) Often are unneces
与标准的if-else语句相比,?:操作符有哪些优点和缺点? 显而易见的是: 有条件的:操作员 处理直接价值比较和分配时更简洁,更简洁 似乎没有像if / else结构那样灵活 标准如果/否 可以应用于更多情况(如函数调用) 通常是不必要的长 可读性似乎因每个语句而异。 在第一次接触到?:操作符之后的一段时间,我花了一些时间来消化它的工作原理。 你会推荐尽可能使用它,或者坚持if / else,因为我与许多非程序
Can someone please explain to me what does the question mark in the member access in the following code means? Is it part of standard C#? I get parse errors when trying to compile this file in Xamarin Studio. this.AnalyzerLoadFailed?.Invoke(this, new AnalyzerLoadFailureEventArgs(AnalyzerLoadFailureEventArgs.FailureErrorCode.NoAnalyzers, null, null)); AnalyzerFileReference.cs line 195 It is
有人可以向我解释下列代码中的成员访问中的问号是什么意思? 它是标准C#的一部分吗? 当试图在Xamarin Studio中编译这个文件时,我得到了解析错误。 this.AnalyzerLoadFailed?.Invoke(this, new AnalyzerLoadFailureEventArgs(AnalyzerLoadFailureEventArgs.FailureErrorCode.NoAnalyzers, null, null)); AnalyzerFileReference.cs第195行 它是在C#6中引入的Null Propagation运算符 ,只有在对象this.AnalyzerLoadFaile
Possible Duplicate: What's the use/meaning of the @ character in variable names in C#? I understand that the @ symbol can be used before a string literal to change how the compiler parses the string. But what does it mean when a variable name is prefixed with the @ symbol? The @ symbol allows you to use reserved word. For example: int @class = 15; The above works, when the below wou
可能重复: C#中变量名称中@字符的用法/含义是什么? 我知道@符号可以在字符串文字之前使用,以改变编译器解析字符串的方式。 但是,当变量名以@符号为前缀时,这意味着什么? @符号允许您使用保留字。 例如: int @class = 15; 以上的作品,当下面不会: int class = 15; @符号在C#中有两个用途: 首先,它允许您使用保留关键字作为像这样的变量: int @int = 15; 第二个选项可让您指定一个字符串而不必转义
I'm tempted to lie and say that English is my second language, but the truth is that I just have no idea what 'Coalescing' means. I know what ?? 'does' in C#, but the name doesn't make sense to me. I looked up the word and I understand it to be a synonym for 'join'. 'Null Join Operator' still doesn't make sense. Can someone enlighten me? I'
我很想说谎,并说英语是我的第二语言,但事实是,我不知道'凝聚'是什么意思。 我知道什么?? '在C#中做',但名字对我来说没有意义。 我查了一下这个词,我明白它是“连接”的同义词。 '空加入运营商'仍然没有意义。 有人能够启发我吗? 我很想说谎,并说英语是我的第二语言......但事实是,我不知道'凝聚'是什么意思。 我知道什么? '在C#中做',但名字对我来说没有意义。