Expilicit接口方法实现: [英] Expilicit Interface Method Implementations:

查看:186
本文介绍了Expilicit接口方法实现:的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hello先生,



我读到了一个事实,即使用了显式接口方法,以避免装箱并提供编译时安全性。



我尝试了一个代码:



情景1:

内部struct somevaluetype:IComparable

{



private int32 m_x;

public somevaluetype(Int32 x)

{

m_x = x;



}



public int32 CompareTo(object other)

{



return(m_x - ((somevaluetype)other).m_x); --------------声明1



}



}







static void main()

{



somevaluetype v = new somevaluetype(0);

Object o = new Object();

Int32 n = v。的CompareTo(v); ///导致拳击

n = v.CompareTo(o); ///程序终止并抛出无效的强制转换异常



Console.Readline();



} < br $>




为避免上述两种情况,我尝试了以下方法:



场景2:



内部结构somevaluetype:IComparable

{



private int32 m_x;

public somevaluetype(Int32 x)

{

m_x = x;



}



public int32 CompareTo(somevaluetype other)

{



return(m_x - other.m_x);



}



////我必须完成IComparable的合约所以我需要实施它的方法如下:





int32 IComparable.CompareTo(Object other)

{



返回m_x - ((somevaluetype)其他).m_x); -------------- statement2



}







}



static void main()

{





somevaluetype v = new somevaluetype(0);

Object o = new Object();

Int32 n = v.CompareTo(v); ///没有拳击

n = v.CompareTo(o); ////不编译



Console.Readline();





$ / b $ b $





- 这2个场景验证外部接口方法实现是否有用。

- 但是statement1和statement2之间有什么区别?

- Statement1导致运行时异常,而statement2导致编译时错误。

- 为什么我不能在显式接口方法之前放置任何访问修饰符?

Hello Sir,

I read about a fact that Explicit interface methods are used so as to avoid boxing and provide compile time safety.

I tried out a code:

SCENARIO 1:
internal struct somevaluetype : IComparable
{

private int32 m_x;
public somevaluetype(Int32 x)
{
m_x = x;

}

public int32 CompareTo(object other)
{

return(m_x - ((somevaluetype)other).m_x); -------------- statement 1

}

}



static void main()
{

somevaluetype v = new somevaluetype(0);
Object o = new Object();
Int32 n = v.CompareTo(v); /// causes boxing
n = v.CompareTo(o); ///program terminates and throws invalid cast exception

Console.Readline();

}


To avoid above 2 situations i tried out the following :

SCENARIO 2:

internal struct somevaluetype : IComparable
{

private int32 m_x;
public somevaluetype(Int32 x)
{
m_x = x;

}

public int32 CompareTo(somevaluetype other)
{

return (m_x - other.m_x);

}

//// i have to complete the contract of IComparable so i need to implement its method so below:


int32 IComparable.CompareTo(Object other)
{

return m_x - ((somevaluetype)other).m_x) ; -------------- statement2

}



}

static void main()
{


somevaluetype v = new somevaluetype(0);
Object o = new Object();
Int32 n = v.CompareTo(v); /// No boxing
n = v.CompareTo(o); ////doesnt compile

Console.Readline();



}


- These 2 scenarios verify that the External interface method implementations are helpful.
- But what is the difference between statement1 and statement2?
- Statement1 causes run time exception and statement2 causes compile time error.
- and why can i not put any access modifier before an explicit interface method?

推荐答案

你好Rahul,



声明1:



你在CompareTo函数中使用directcast。它抛出无效的强制转换异常

当参数不是somevaluetype的类型时。我想你必须使用以下。





Hello Rahul,

Statement1 :

you are using a directcast inside CompareTo function. its throws invalid cast exception
when argument is not a type of somevaluetype. i think you have to use following.


public int32 CompareTo(object other)
{
    temp = other as somevaluetype;
	if(temp != null)
	   return (m_x - temp.m_x);
    return -1;
}





语句2:



IComparable.CompareTo方法没有任何访问修饰符。所以它被认为是私人方法。

你不能从外面打电话。由于以下函数抛出编译时错误。





Statement2 :

IComparable.CompareTo method don't have any access modifiers. so its consider as private method.
you can't call it from out side. due to that following function throws an compile time error.

n = v.CompareTo(o); ////doesnt compile







我已经在C#中测试并实现了这段代码。



想想这个帮助。




I have tested and implemented this code in C#.

Think this help.


这篇关于Expilicit接口方法实现:的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆