Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to create constructor-extension-method ? how?

Is it possible to add a constructor extension method?

Sample Use Case

I want to add a List< T > constructor to receive specific amount of bytes out of a given partially filled buffer (without the overhead of copying only the relevant bytes and so on):

... public static List<T>(this List<T> l, T[] a, int n) {     for (int i = 0; i < n; i++)        l.Add(a[i]); } ... 

so the usage would be:

List<byte> some_list = new List<byte>(my_byte_array,number_of_bytes); 

I've already added an AddRange extension method:

public static void AddRange<T>(this List<T> l, T[] a, int n) {    for (int i = 0; i < n; i++)        l.Add(a[i]); } 

I want to do it as a constructor too. Is it possible? If yes - how?

like image 569
Tar Avatar asked Jan 24 '11 12:01

Tar


People also ask

Can we create extension method for interface?

You can use extension methods to extend a class or interface, but not to override them. An extension method with the same name and signature as an interface or class method will never be called. At compile time, extension methods always have lower priority than instance methods defined in the type itself.

Is it possible to create constructors in C# programming language?

C# Constructor Syntax As discussed, the constructor is a method, and it won't contain any return type. If you want to create a constructor in c#, you need to create a method with the class name. Following is the syntax of creating a constructor in the c# programming language.

Can we create extension methods for static class?

An extension method must be defined in a top-level static class. An extension method with the same name and signature as an instance method will not be called. Extension methods cannot be used to override existing methods. The concept of extension methods cannot be applied to fields, properties or events.

Can we create extension method for abstract class?

Sure, no problem.


2 Answers

No, but if you changed your AddRange signature to return the list instance, then you could at least do

var list = new List<int>().AddRange(array, n); 

which imho is probably clearer than overloading the constructor anyway.

like image 168
fearofawhackplanet Avatar answered Sep 21 '22 22:09

fearofawhackplanet


SWeko's answer is basically correct, though of course the article he links to is about extension properties rather than extension constructors.

We also did a rough design for extension constructors at the same time as we did extension properties; they would be a nice syntactic sugar for the factory pattern. However, they never got past the design stage; the feature, though nice, is not really necessary and does not enable any awesome new scenarios.

If you have a really awesome problem that extension constructors would solve, I'd be happy to hear more details. The more real-world feedback we get, the better we are able to evaluate the relative merits of the hundreds of different feature suggestions we get every year.

like image 26
Eric Lippert Avatar answered Sep 19 '22 22:09

Eric Lippert