Is there a way to avoid writing exactly the same type twice when initializing properties (or fields) in c#? (something similar to diamond operator in Java would do..)
For example, lets say I have this code:
private readonly Dictionary<string, ISomething> _dict =
new Dictionary<string, ISomething>();
Is there a way to avoid writing the type Dictionary<string, ISomething>()
or part of it twice?
Please note that the type is already defined as Dictionary
, not IDictionary
..
No, there's nothing like that in C# at the moment. The obvious potential fix would be to allow fields to be declared using var
, but that's more complicated than it may sound.
I'd be somewhat surprised to see anything like Java's "diamond operator" appear in C#, but I've been surprised before...
If you're really trying to avoid the typing then I'd expect Visual Studio to help offer to complete the assignment after you type new
. (Personally I don't view typing as the main bottleneck when coding anyway, but...)
See more on this question at Stackoverflow