c# 中new 的含义
c#中的new是什么含义?在c++中看到new分配内存,c#中new是什么含义?
比如int abc;和int abc = new int();有什么不同?如果new分配内存的话,那么没有new的时候,int abc作了写什么工作?
谢谢...
问题点数:30、回复次数:9Top
1 楼venus9008()回复于 2006-03-10 15:45:02 得分 0
c#中的new是什么含义?在c++中看到new分配内存,c#中new是什么含义?
比如int abc;和int abc = new int();有什么不同?如果new分配内存的话,那么没有new的时候,int abc作了些什么工作?
谢谢...Top
2 楼independently(我是风筝高高飞)回复于 2006-03-10 15:47:57 得分 0
是一样的啊,可以声明变量和类的对象Top
3 楼time_is_life(今夜太冷:http://timeislife.blog.sohu.com)回复于 2006-03-10 15:48:53 得分 10
在 C# 中,new 关键字可用作运算符或修饰符。
new 运算符 用于在堆上创建对象和调用构造函数。
new 修饰符 用于隐藏基类成员的继承成员。
new 运算符
new 运算符用于创建对象和调用构造函数,例如:
Class1 MyClass = new Class1();
也用于为值类型调用默认的构造函数,例如:
int myInt = new int();
在上一个语句中,myInt 初始化为 0,它是 int 类型的默认值。该语句的效果等同于:
int myInt = 0;
有关默认值的完整列表,请参见默认值表。
记住,为结构声明默认的构造函数是一个错误,因为每一种值类型都隐式具有一个公共的默认构造函数。但是,可以在结构类型上声明参数化的构造函数。
值类型对象(例如结构)是在堆栈上创建的,而引用类型对象(例如类)是在堆上创建的。
不能重载 new 运算符。
如果 new 运算符分配内存失败,则它将引发 OutOfMemoryException 异常。
示例
在下例中,通过使用 new 运算符创建和初始化结构对象和类对象,然后给它们赋值。显示了默认值和所赋的值
// cs_operator_new.cs
// The new operator
using System;
class NewTest
{
struct MyStruct
{
public int x;
public int y;
public MyStruct (int x, int y)
{
this.x = x;
this.y = y;
}
}
class MyClass
{
public string name;
public int id;
public MyClass ()
{
}
public MyClass (int id, string name)
{
this.id = id;
this.name = name;
}
}
public static void Main()
{
// Create objects using default constructors:
MyStruct Location1 = new MyStruct();
MyClass Employee1 = new MyClass();
// Display values:
Console.WriteLine("Default values:");
Console.WriteLine(" Struct members: {0}, {1}",
Location1.x, Location1.y);
Console.WriteLine(" Class members: {0}, {1}",
Employee1.name, Employee1.id);
// Create objects using parameterized constructors::
MyStruct Location2 = new MyStruct(10, 20);
MyClass Employee2 = new MyClass(1234, "John Martin Smith");
// Display values:
Console.WriteLine("Assigned values:");
Console.WriteLine(" Struct members: {0}, {1}",
Location2.x, Location2.y);
Console.WriteLine(" Class members: {0}, {1}",
Employee2.name, Employee2.id);
}
}
输出
Default values:
Struct members: 0, 0
Class members: , 0
Assigned values:
Struct members: 10, 20
Class members: John Martin Smith, 1234
注意在该例中,字符串的默认值为 null,因此未显示它。
new 修饰符
使用 new 修饰符显式隐藏从基类继承的成员。若要隐藏继承的成员,请使用相同名称在派生类中声明该成员,并用 new 修饰符修饰它。
请看下面的类:
public class MyBaseC
{
public int x;
public void Invoke() {}
}
在派生类中用 Invoke 名称声明成员会隐藏基类中的 Invoke 方法,即:
public class MyDerivedC : MyBaseC
{
new public void Invoke() {}
}
但是,因为字段 x 不是通过类似名隐藏的,所以不会影响该字段。
通过继承隐藏名称采用下列形式之一:
引入类或结构中的常数、指定、属性或类型隐藏具有相同名称的所有基类成员。
引入类或结构中的方法隐藏基类中具有相同名称的属性、字段和类型。同时也隐藏具有相同签名的所有基类方法。有关更多信息,请参见 3.6 签名和重载。
引入类或结构中的索引器将隐藏具有相同名称的所有基类索引器。
在同一成员上同时使用 new 和 override 是错误的。
在不隐藏继承成员的声明中使用 new 修饰符将生成警告。
有关隐藏名称的更多信息,请参见 3.7.1 名称隐藏。
有关完全限定名的更多信息,请参见 3.8.1 完全限定名。
示例
在该例中,基类 MyBaseC 和派生类 MyDerivedC 使用相同的字段名 x,从而隐藏了继承字段的值。该例说明了 new 修饰符的使用。同时也说明了如何使用完全限定名访问基类的隐藏成员。
// cs_modifier_new.cs
// The new modifier
using System;
public class MyBaseC
{
public static int x = 55;
public static int y = 22;
}
public class MyDerivedC : MyBaseC
{
new public static int x = 100; // Name hiding
public static void Main()
{
// Display the overlapping value of x:
Console.WriteLine(x);
// Access the hidden value of x:
Console.WriteLine(MyBaseC.x);
// Display the unhidden member y:
Console.WriteLine(y);
}
}
输出
100
55
22
如果移除 new 修饰符,程序将继续编译和运行,但您会收到以下警告:
The keyword new is required on 'MyDerivedC.x' because it hides inherited member 'MyBaseC.x'.
如果嵌套类型正在隐藏另一种类型,如下例所示,也可以使用 new 修饰符修改此嵌套类型。
示例
在该例中,嵌套类 MyClass 隐藏了基类中具有相同名称的类。该例不仅说明了如何使用完全限定名访问隐藏类成员,同时也说明了如何使用 new 修饰符消除警告消息。
// cs_modifer_new_nested.cs
// Using the new modifier with nested types
using System;
public class MyBaseC
{
public class MyClass
{
public int x = 200;
public int y;
}
}
public class MyDerivedC : MyBaseC
{
new public class MyClass // nested type hiding the base type members
{
public int x = 100;
public int y;
public int z;
}
public static void Main()
{
// Creating object from the overlapping class:
MyClass S1 = new MyClass();
// Creating object from the hidden class:
MyBaseC.MyClass S2 = new MyBaseC.MyClass();
Console.WriteLine(S1.x);
Console.WriteLine(S2.x);
}
}
输出
100
200
Top
4 楼Ivony(授人以鱼不如授人以渔,上海谋生)回复于 2006-03-10 15:56:53 得分 20
在C#中,new是创建一个对象。
int abc = new int();//其实这个是创建一个Int32的对象(尽管它是值类型的),并调用它的默认构造函数。
实际上int abc = new int();等价于int abc = 0;
int abc = new int();
if ( abc == 0 )//正确
int abc;
if ( abc == 0 )//编译错误Top
5 楼venus9008()回复于 2006-03-10 16:00:44 得分 0
谢谢 哥们~~姐们~~~捧场...在msdn中找到这样一句话:"在 C# 中不允许使用未初始化的变量。"
大家共勉!!
Top
6 楼iseelxj(莹祈)回复于 2006-03-10 16:18:03 得分 0
int abc=0;和int abc = new int();
是一样的,看IL代码就可以知道这点。
其实 int abc;在进程堆栈上也是被初始化为0的。Top
7 楼qwertxp(上车的乘客请做好下车准备)回复于 2006-03-10 17:31:26 得分 0
CSDN中有一句话,解决问题不结贴,以后没人回答问题。
与搂主共勉。。。Top
8 楼venus9008()回复于 2006-03-10 17:43:42 得分 0
呵呵 马上结,刚刚出去一下 谢谢大家Top
9 楼diandian82(点点(nothing))回复于 2006-03-10 17:46:18 得分 0
JFTop
相关问题
- c语言中'#'号是什么含义
- 请教:关键字const的含义,用法(c中)
- 请教c++ 中 几种函数定义的含义......
- 在线等候解答:Delphi中的string[10] 对应C++Builder的什么含义?
- 在线等候解答:Delphi中的string[10] 对应C++Builder的什么含义?
- 求解,请问c/c++中堆和栈的含义(非数据结构中的堆栈)
- 请解释一下这一句中各元素的含义<%@ Page language="c#" Codebehind="PreDeliver.aspx.cs" AutoEventWireup="false" Inherits="Issue.Pre
- 求教一个非常简单的问题. c语言中exit(1) 的具体含义是什么..
- 请问Excel中的$符号表示什么?比如=Sheet1!$A$2:$C$7这种的指的是什么含义?
- C语言中有个类型限定符restrict,谁能解释下这个限定符的含义!




