I try to understand the Extension method concept in c#, So I added this interface :
public interface IExtension
{
public static int Multiple(this int a)
{
return a * 2;
}
}
In my Main method
public static void Main()
{
Console.WriteLine("Entrer un numero");
int a =int.Parse( Console.ReadLine());
a.Multiple();
Console.ReadKey();
}
I get an error in a.Multiple(); indicates that the method is not recognized.
Even I added a static class implementing the interface, I get this same error.
How can I fix this problem?
Talk1:
In my knowledge static member in an interface doesn't make any sense, and you have the implementation??
Talk2:
You can't implement any method on an interface. Interfaces contain no implementation by definition. Did you mean something else?
Talk3:
Interestingly, in Java 8 they added "default" methods to interfaces... So this is right in C# and .NET, but it isn't an absolute truth (considering how much similar C# and Java are)
Talk4:
I guess the OP is from Java background considering the fact that a method is being implemented within an interface. No harsh feelings lets fiddle dotnetfiddle.net/FidKGT
Solutions1
Extension methods must be in a static class, not in an interface
public static class MyExtension
{
public static int Multiple(this int a)
{
return a * 2;
}
}
And you should print the result:
Console.WriteLine(a.Multiple());
Note that your extension method can't change the value of a (because int is a value type), it can only return a new value!.
Check by adding
Console.WriteLine(a.Multiple());
Console.WriteLine(a);
Just as a curiousity, in Java 8 Oracle added default methods to interfaces: https://docs.oracle.com/javase/tutorial/java/IandI/defaultmethods.html They are a little like C# extension methods, but they are defined inside the interface. Types that implement the interface can override them, otherwise they "inherit" these default methods.
Talk1:
You can't declare it: compilation error. ideone.com/13sL89 Or if you prefer the only beta Roslyn goo.gl/3Q14Vc
Solutions2
This code should not compile. Please first read about extension methods in c# - https://msdn.microsoft.com/en-us//library/bb383977.aspx
Extension method should be declared in public static class and interface is redundant.
public static class Extensions
{
public static int Multiple(this int a)
{
return a * 2;
}
}
public static void Main()
{
Console.WriteLine("Entrer un numero");
int a = int.Parse(Console.ReadLine());
int doubledNumber = a.Multiple();
Console.ReadKey();
}
Talk1:
Extensions must be declared in static classes. Static classes cant implement interface by definition. So you cant declare interface for this class.
Solutions3
An extension method must be specified in a non-generic static class, as a static method. You cannot declare one in an interface.
Edit:
@LamloumiAfif must have a different and more magical compiler than I do.
Talk1:
No, you can implement it in interface, you can test my code to see this
Talk2:
I've tested your code, see my edit above.
Talk3:
So I made a mistake , I have no error before compilation , editor didn't indicate any error!!