Overloading the general list Add with extension

I would like to overload the general method of adding a list so that I can use collection initialization, for example:

var x = new List<Tuple<string>> { { "1", "2" }, { "1", "2" } }; 

(where Tuple is a simple custom binary tuple implementation). However, I created an extension method, put the using directive in the cs file and still get "No overload for the Add method, it takes 2 arguments" -error.
Is it not possible to do this (using the extension method)?

Extension Method Code:

 namespace ExtensionMethods { public static class Extensions{ public static void Add<T>(this List<Tuple<T>> self, T value1, T value2) { self.Add(new Tuple<T> { value1, value2 }); } } } 
+4
source share
4 answers

In C # 6.0 [0], Microsoft allows the use of extension methods in collection initializers. Hooray:)

And since this is not a change to the .NET Framework or CLR, but a change to the compiler, this function can be used with .NET 4.0.

So now the correct C # code is given. (Tested in Visual Studio 2015 RC)

 class Program { static void Main(string[] args) { var x = new List<Tuple<string,string>> { { "1", "2" }, { "1", "2" } }; } } public static class Extensions { public static void Add<T1,T2>(this List<Tuple<T1,T2>> self, T1 value1, T2 value2) { self.Add(Tuple.Create( value1, value2 )); } } 

C # 6 Functions [0]

+1
source

This is not possible using extension methods. For this syntax to work, you must create your own collection class, which will have the signature void Add(T value1, T value2) .

PS: What you did is not overload , and there is no way to overload anything in an existing class.

UPDATE: Looks like my first suggestion should be: "This is not possible using extension methods in C # "

+2
source

You cannot implement constructors using extension methods. An extension method is nothing more than a static method that accepts an instance of an object. Therefore, you must first have an instance so that it can go to it.

But you can just use AddRange() list to initialize your list.

+1
source

Extension methods cannot overload any method defined for their target type, simply because they are not members of the target type. Extension methods do not add anything to their target types. It is simply compiler magic that allows them to be called as if they were methods defined in their target type. In fact, they are defined in a separate static type, so they cannot overload any method in their target type.

Behind the scenes, the compiler replaces any calls to your extension method with an Extensions call.

It looks like you are trying to make C # behave like Ruby and initialize an array of objects by passing a set of values ​​or at least mimicking the behavior of dictionary initialization. Unfortunately, you cannot do this without overloading List <>.

Initializing a collection is another bit of compiler magic that tries to find the Add method with as many arguments as there are elements in the argument list. If it were otherwise, you could define a conversion operator to convert the list to your Tuple type.

Why don't you just use the built-in object initializers? You just need to write a little more code

0
source

Source: https://habr.com/ru/post/1345527/


All Articles