c#中接口的使用方法图解_.net c#使用在接口中声明和实现的扩展方法

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!!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值