小编use*_*398的帖子

我不明白为什么我们需要'new'关键字

我是C#的新手,来自C++背景.在C++中,您可以这样做:

class MyClass{
....
};
int main()
{
   MyClass object; // this will create object in memory
   MyClass* object = new MyClass(); // this does same thing
}
Run Code Online (Sandbox Code Playgroud)

而在C#中:

class Program
{
    static void Main(string[] args)
    {
        Car x;
        x.i = 2;
        x.j = 3;
        Console.WriteLine(x.i);
        Console.ReadLine();

    }
}
class Car
{
    public int i;
    public int j;


}
Run Code Online (Sandbox Code Playgroud)

你不能这样做.我想知道为什么Car x不做它的工作.

c# language-design

36
推荐指数
5
解决办法
1万
查看次数

标签 统计

c# ×1

language-design ×1