新旧之间的区别传递对象的类型 [英] Difference between new & passing Type of the object

查看:74
本文介绍了新旧之间的区别传递对象的类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hello Every One,

我的项目中有一个ResultClass。

我正在创建该类的对象

Hello Every One,
I am having One ResultClass in my project.
I am creating the object of that class

<pre lang="c#">ResultClass r=new ResultClass();</pre>

one more thing i am doing is

<pre lang="cs">
//m_ResultList is temp resultclass object
ResultClass m_RecedResult = (ResultContainer)m_ResultList[0];
               m_ResultList.RemoveAt(0);</pre>





现在我不理解这两个声明之间的区别。新关键字的区别是什么obj传递的类型。



now i am not understanding the difference between this both statement.what is difference between new keyword and type of obj passing.

推荐答案

在第一种情况下,使用类构造函数创建某个类的实例,并为该变量分配一个引用。在第二种情况下,您尝试获取对前一次创建(实例化)的实例的引用,类型为case。请注意,它可能会失败并抛出一个或另一个异常。首先,一个集合可能是空的,如果索引范围和集合元素的实际运行时类型可能不同,那么索引为零将会抛出,这将抛出一个类型转换异常。



如果你不理解这些基本的东西,你应该停止做你正在做的事情并阅读有关编程,.NET和语言的书籍/文档,否则你不会得到任何例外的挫折。



-SA
In first case, you create an instance of some class using the class constructor and assign a reference to the variable. In second case, you try to get a reference to the instance which was created (instantiated) some time ago, with type case. Note that it can be unsuccessful and throw one or another exception. First, a collection might be empty, then indexing at zero would be out if index range, and the actual runtime type of the collection element might be different, that would throw a type cast exception.

If you did not understand such elementary things, you should stop doing what you are doing and read some book/documentation on programming, .NET and the language, otherwise you won't get anything exception frustration.

—SA


new:

新关键字可以用作运算符,修饰符或约束。



新运算符 :用于创建对象和调用构造函数。

动物an =新动物();



新修饰符:用于隐藏基类成员的继承成员。

new :
the new keyword can be used as an operator, a modifier, or a constraint.

new Operator : Used to create objects and invoke constructors.
Animal an = new Animal();

new Modifier : Used to hide an inherited member from a base class member.
public class BaseC<br />
{<br />
public int x;<br />
public void Invoke() { }<br />
}<br />
public class DerivedC : BaseC<br />
{<br />
new public void Invoke() { }<br />
}





new Constraint:用于限制可能用作泛型声明中类型参数的参数的类型。



new Constraint : Used to restrict types that might be used as arguments for a type parameter in a generic declaration.

class ItemFactory<t> where T : new()<br />
{<br />
public T GetNewItem()<br />
{<br />
    return new T();<br />
}<br />
}</t>







typeof:

用于获取类型的System.Type对象。 typeof表达式采用以下形式:

System.Type type = typeof(int);



您可以在MSDN上找到更多信息。 :)



-KR




typeof :
Used to obtain the System.Type object for a type. A typeof expression takes the following form:
System.Type type = typeof(int);

You could have find more info on MSDN. :)

-KR


这篇关于新旧之间的区别传递对象的类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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