在C#中向上转换(从基础到派生) [英] Up casting (base to derived) in C#

查看:52
本文介绍了在C#中向上转换(从基础到派生)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿所有,


我有一个C#问题,关于你的铸造(基础到派生)。


我想知道从基类型转换为派生类型的最优雅的方式(可读,更少的代码)

。请考虑以下两个课程后的




class Base {

private int m_valA;

private int m_valB;


public Base(){}


public Base(int a,int b){

m_valA = a;

m_valB = b;

}


public int ValA

{

得到{return m_valA; }

set {m_valA = value; } b / b $

public int ValB

{

get {return m_valB; }

set {m_valB = value; }

}

} //结束课


类派生:基础

{

public int ValAPlusValB

{

get {return base.ValA + base.ValB; }

}

} //结束类


现在考虑我有Base实例的场景(someBase)

并且想把它投射到Derived实例(someDerived)。

Hey All,

I have a C# question for you regarding up casting (base to derived).

I was wondering about the most elegant way (readable, less code) to
cast from a base type to its derived type. Please consider the
following two classes:

class Base {
private int m_valA;
private int m_valB;

public Base() { }

public Base(int a, int b){
m_valA = a;
m_valB = b;
}

public int ValA
{
get { return m_valA; }
set { m_valA = value; }
}

public int ValB
{
get { return m_valB; }
set { m_valB = value; }
}
}//end class

class Derived : Base
{
public int ValAPlusValB
{
get { return base.ValA + base.ValB; }
}
}//end class

Now consider the scenario where I have an instance of Base (someBase)
and could like to up cast it to a Derived instance (someDerived).

推荐答案



AtariPete写道:

AtariPete wrote:

嘿所有,


我有一个关于向上铸造的C#问题(基于派生)。


我想知道从基类型转换为派生类型的最优雅的方式(可读,更少的代码)

。请考虑以下两个课程后的




class Base {

private int m_valA;

private int m_valB;


public Base(){}


public Base(int a,int b){

m_valA = a;

m_valB = b;

}


public int ValA

{

得到{return m_valA; }

set {m_valA = value; } b / b $

public int ValB

{

get {return m_valB; }

set {m_valB = value; }

}

} //结束课


类派生:基础

{

public int ValAPlusValB

{

get {return base.ValA + base.ValB; }

}

} //结束类


现在考虑我有Base实例的场景(someBase)

并且想把它投射到Derived实例(someDerived)。
Hey All,

I have a C# question for you regarding up casting (base to derived).

I was wondering about the most elegant way (readable, less code) to
cast from a base type to its derived type. Please consider the
following two classes:

class Base {
private int m_valA;
private int m_valB;

public Base() { }

public Base(int a, int b){
m_valA = a;
m_valB = b;
}

public int ValA
{
get { return m_valA; }
set { m_valA = value; }
}

public int ValB
{
get { return m_valB; }
set { m_valB = value; }
}
}//end class

class Derived : Base
{
public int ValAPlusValB
{
get { return base.ValA + base.ValB; }
}
}//end class

Now consider the scenario where I have an instance of Base (someBase)
and could like to up cast it to a Derived instance (someDerived).



我不明白你想知道什么。您不能将实例

转换为另一个实例。您转换引用,而不是实例。如果你有一个指向Derived实例的

基本引用,并且你想将

转换为Derived引用,有两种方法可以做到这一点。要么是


基础someBase = new派生();

派生someDerived =(派生)someBase;


或者


基础someBase = new派生();

派生someDerived = someBase为派生;


第一种方式如果someBase
引用的实例不是Derived类型或某个子类型Derived,则抛出异常。第二种方式不会在这种情况下抛出异常,而是返回null。


您使用哪种方式取决于您的申请中是否

指向代码,实例必须肯定。是一个派生的实例,

或者实例可能是一个派生的实例,你想要

处理它不是的情况。


这是你想知道的吗?

I don''t understand what you want to know. You cannot cast an instance
to another instance. You cast references, not instances. If you have a
Base reference pointing to an instance of Derived, and you want to cast
it to a Derived reference, there are two ways to do this. Either

Base someBase = new Derived();
Derived someDerived = (Derived)someBase;

or

Base someBase = new Derived();
Derived someDerived = someBase as Derived;

The first way throws an exception if the instance to which "someBase"
refers is not of type Derived or some child type of Derived. The second
way does not throw an exception in this case but instead returns null.

Which one you use depends upon whether, in your application at that
point in the code, the instance must "certainly" be a Derived instance,
or instead the instance "could be" a Derived instance and you want to
handle the case where it isn''t.

Is this what you wanted to know?


AtariPete写道:
AtariPete wrote:

嘿所有,


我有关于向上投射的一个C#问题(基础到派生)。


我想知道最优雅的方式(可读,代码少)到/ b
从基类型到其派生类型。
Hey All,

I have a C# question for you regarding up casting (base to derived).

I was wondering about the most elegant way (readable, less code) to
cast from a base type to its derived type.



[...]

[...]


现在考虑我有Base实例的场景(someBase )

并且想把它投射到Derived实例(someDerived)。
Now consider the scenario where I have an instance of Base (someBase)
and could like to up cast it to a Derived instance (someDerived).



我个人喜欢这样:

派生someDerived = someBase为Derived;

I personally like this:

Derived someDerived = someBase as Derived;


你不能将实例强制转换为另一个实例。


布鲁斯,


感谢您的反馈。


你是对的,我并不是说将一个实例强制转换为另一个实例。

我的意思是将一个Base实例转换为派生类型。
You cannot cast an instance to another instance.

Bruce,

Thanks for your feedback.

You''re correct, I didn''t mean "cast one instance to another instance".
I had meant cast an instance of Base to type derived.

Base someBase = new Derived();

派生someDerived = someBase为Derived;
Base someBase = new Derived();
Derived someDerived = someBase as Derived;



这个问题是我无法控制Base的创建方式。因此

base实例化如下:

基础someBase = new Base();

但是如果我做了演员

派生someDerived = someBase为Derived;

someDerived将为null。

Bruce Wood写道:

The issue with this is that i do not control how Base is created. Thus
base is instantiated as follows:
Base someBase = new Base ();
but then if I do the cast
Derived someDerived = someBase as Derived;
someDerived will be null.

Bruce Wood wrote:


AtariPete写道:
AtariPete wrote:

嘿所有,


关于向上铸造,我有一个C#问题(基于派生)。


我想知道从基类型转换为派生类型的最优雅方式(可读,代码较少)

。请考虑以下两个课程后的




class Base {

private int m_valA;

private int m_valB;


public Base(){}


public Base(int a,int b){

m_valA = a;

m_valB = b;

}


public int ValA

{

得到{return m_valA; }

set {m_valA = value; } b / b $

public int ValB

{

get {return m_valB; }

set {m_valB = value; }

}

} //结束课


类派生:基础

{

public int ValAPlusValB

{

get {return base.ValA + base.ValB; }

}

} //结束类


现在考虑我有Base实例的场景(someBase)

并且想把它投射到Derived实例(someDerived)。
Hey All,

I have a C# question for you regarding up casting (base to derived).

I was wondering about the most elegant way (readable, less code) to
cast from a base type to its derived type. Please consider the
following two classes:

class Base {
private int m_valA;
private int m_valB;

public Base() { }

public Base(int a, int b){
m_valA = a;
m_valB = b;
}

public int ValA
{
get { return m_valA; }
set { m_valA = value; }
}

public int ValB
{
get { return m_valB; }
set { m_valB = value; }
}
}//end class

class Derived : Base
{
public int ValAPlusValB
{
get { return base.ValA + base.ValB; }
}
}//end class

Now consider the scenario where I have an instance of Base (someBase)
and could like to up cast it to a Derived instance (someDerived).



我不明白你想知道什么。您不能将实例

转换为另一个实例。您转换引用,而不是实例。如果你有一个指向Derived实例的

基本引用,并且你想将

转换为Derived引用,有两种方法可以做到这一点。要么是


基础someBase = new派生();

派生someDerived =(派生)someBase;


或者


基础someBase = new派生();

派生someDerived = someBase为派生;


第一种方式如果someBase
引用的实例不是Derived类型或某个子类型Derived,则抛出异常。第二种方式不会在这种情况下抛出异常,而是返回null。


您使用哪种方式取决于您的申请中是否

指向代码,实例必须肯定。是一个派生的实例,

或者实例可能是一个派生的实例,你想

处理它不是的情况。


这是你想知道的吗?


I don''t understand what you want to know. You cannot cast an instance
to another instance. You cast references, not instances. If you have a
Base reference pointing to an instance of Derived, and you want to cast
it to a Derived reference, there are two ways to do this. Either

Base someBase = new Derived();
Derived someDerived = (Derived)someBase;

or

Base someBase = new Derived();
Derived someDerived = someBase as Derived;

The first way throws an exception if the instance to which "someBase"
refers is not of type Derived or some child type of Derived. The second
way does not throw an exception in this case but instead returns null.

Which one you use depends upon whether, in your application at that
point in the code, the instance must "certainly" be a Derived instance,
or instead the instance "could be" a Derived instance and you want to
handle the case where it isn''t.

Is this what you wanted to know?


这篇关于在C#中向上转换(从基础到派生)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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