indexer

How do I overload the [] operator in C# [duplicate]

半城伤御伤魂 提交于 2019-11-26 10:06:37
问题 This question already has an answer here: How do I overload the square-bracket operator in C#? 8 answers I would like to add an operator to a class. I currently have a GetValue() method that I would like to replace with an [] operator. class A { private List<int> values = new List<int>(); public int GetValue(int index) => values[index]; } 回答1: public int this[int key] { get => GetValue(key); set => SetValue(key, value); } 回答2: I believe this is what you are looking for: Indexers (C#

“Strange” C# property syntax

半城伤御伤魂 提交于 2019-11-26 08:37:08
问题 I just saw this in a c# project: public char this[int index] I consider myself new to C#; any one can help what it\'s meaning? 回答1: It is an Indexer. Indexers allow instances of a class or struct to be indexed just like arrays. Indexers resemble properties except that their accessors take parameters. An indexer provides array-like syntax. It allows a type to be accessed the same way as an array. Properties such as indexers often access a backing store. We often accept a parameter of int type

Class with indexer and property named “Item”

六月ゝ 毕业季﹏ 提交于 2019-11-26 07:45:01
问题 Is it possible to create a class in .NET 4 with: an indexer, a property named \"Item\"? For example, this C# class will not compile for me: public class MyClass { public object Item { get; set; } public object this[string index] { get { return null; } set { } } } The compiler gives an error CS0102: The type \'MyClass\' already contains a definition for \'Item\' although I am only explicitly defining Item once. 回答1: Based on this site, it is possible to use an attribute to rename the Indexer

Static Indexers?

流过昼夜 提交于 2019-11-26 04:46:00
问题 Why are static indexers disallowed in C#? I see no reason why they should not be allowed and furthermore they could be very useful. For example: public static class ConfigurationManager { public object this[string name] { get => ConfigurationManager.getProperty(name); set => ConfigurationManager.editProperty(name, value); } /// <summary> /// This will write the value to the property. Will overwrite if the property is already there /// </summary> /// <param name=\"name\">Name of the property<

Eclipse CDT indexer does not know C++11 containers

这一生的挚爱 提交于 2019-11-26 03:45:45
I configured a C++11 project in Eclipse CDT to use gcc-4.7. It is not the default compiler on my system, which does not support C++11. In order for compilation to work, I need to pass the flag -std=c++11 and also include the following header path: /usr/local/Cellar/gcc/4.7.2/gcc/include/c++/4.7.2 Whenever I use C++11 container types like std::unordered_set or std::unordered_map , the CDT indexer complains: Symbol unordered_set could not be resolved . How can I tell the indexer to resolve these symbols correctly? This is how I have configured my indexer: As far as I understand the settings, the

Eclipse CDT indexer does not know C++11 containers

十年热恋 提交于 2019-11-26 01:58:01
问题 I configured a C++11 project in Eclipse CDT to use gcc-4.7. It is not the default compiler on my system, which does not support C++11. In order for compilation to work, I need to pass the flag -std=c++11 and also include the following header path: /usr/local/Cellar/gcc/4.7.2/gcc/include/c++/4.7.2 Whenever I use C++11 container types like std::unordered_set or std::unordered_map , the CDT indexer complains: Symbol unordered_set could not be resolved . How can I tell the indexer to resolve