Replies: 12 comments
-
Duplicate of #471? |
Beta Was this translation helpful? Give feedback.
-
Yes, thank you. |
Beta Was this translation helpful? Give feedback.
-
this could be also equivalent using methods, you don't really have to use indexer.
though this proposal is kinda cool for set part. it can improve syntax for setter indexers. |
Beta Was this translation helpful? Give feedback.
-
Yes, this can be done with two separate methods, 1 - Both methods are independent, when they should be together, Another thing, maybe the generated code could also be mapped to an interface, for instance interface IIndexAcessor<TKey, TValue> {
TValue this[TKey key] { get; set; }
} and I could pass obj.Metrics around, and it would use my acessor. Is something to think about I believe. |
Beta Was this translation helpful? Give feedback.
-
yes, I agree with concerns that you have. this may not seem useful to some people but they will know its valuable when writing API that everyone loves. 👍 better say it converts ugly syntax into good looking syntax 😃 |
Beta Was this translation helpful? Give feedback.
-
The argument against this feature has always been that in the C# language there is an expectation that the property and the indexer are two separate parts of the expression and that you should be able to assign what you're indexing to its own variable: var x = foo.Bar[index];
var bar = foo.Bar; // would not work with indexed properties That argument doesn't seem to have stopped any of the other major CLR languages, though. VB.NET, F# and C++/CLI all support named/custom indexer properties. |
Beta Was this translation helpful? Give feedback.
-
I like this idea. |
Beta Was this translation helpful? Give feedback.
-
This could sometimes be useful. Your workaround, however, can be simplyfied: class Indexer<TKey, TValue>
{
readonly Func<TKey, TValue> _Get;
readonly Action<TKey, TValue> _Set;
public Indexer(Func<TKey, TValue> get, Action<TKey, TValue> set)
{
_Get = get;
_Set = set;
}
public TValue this[TKey k]
{
get => _Get(k);
set => _Set(k, value);
}
}
static class Indexer
{
static public Indexer<TKey, TValue> Create<TKey, TValue>(Func<TKey, TValue> get, Action<TKey, TValue> set)
=> new Indexer<TKey, TValue>(get, set);
}
class BaseEvent
{
public Indexer<double, double> Meteric { get; }
public BaseEvent()
{
double c = 0;
Meteric = Indexer.Create(
get: (double k) => c + k,
set: (k, v) => c = v - k
);
}
} Unfortunately, you still have to separate the declaration and definition of the indexer. |
Beta Was this translation helpful? Give feedback.
-
@quinmars One issue with the Potentially this could be solved using weak references and LINQ |
Beta Was this translation helpful? Give feedback.
-
Later I was thinking about another Idea, that could be more powerful than this one, it was something like: public class TestClass
{
string suffix = "_suffix";
public property class Attribute
{
public string this[string key]
{
get => return key + suffix;
set => throw new System.NotImplementedException();
}
}
}
static void Test()
{
var test = new TestClass();
var res = test.Attribute["abc"];
} It would basically be a nested class, with access to its outer class, (yes, like Java). It would be equivalent of public class TestClass
{
string suffix = "_suffix";
public AttributeClass Attribute = new AttributeClass(this); // I know this is an error, but you get the idea.
public class AttributeClass
{
readonly TestClass outer;
public AttributeClass(TestClass outer)
{
this.outer = outer;
}
public string this[string key]
{
get => return key + outer.suffix;
set => throw new System.NotImplementedException();
}
}
} This way, it would be possible to add fields inside AttributeClass and it would still be easy. |
Beta Was this translation helpful? Give feedback.
-
Would it work like this:
This is how it could work in C#
|
Beta Was this translation helpful? Give feedback.
-
Was looking to implement this also. I would definitely love to see this feature implemented. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Context
csharp allows you to overload the array indexing operator
Limitation
But if you want more than one acessor, for example:
this is not allowed, because
would be ambiguous.
Even if I only want setters, I am not allowed.
Proposal
It would be great, if I could do this instead:
And access it like:
Today
This can be done today, by creating something like this:
But is unnecessarily complex for something that could be simple.
Beta Was this translation helpful? Give feedback.
All reactions