什么关键字"新"的确在C#中的结构? [英] What does the keyword "new" does to a struct in C#?

查看:134
本文介绍了什么关键字"新"的确在C#中的结构?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C#中,结构是在价值观方面进行管理,对象是在参考。从我的理解,创建类的实例时,关键字导致C#中使用该类信息作出的实例,如下:

In C#, Structs are managed in terms of values, and objects are in reference. From my understanding, when creating an instance of a class, the keyword new causes C# to use the class information to make the instance, as in below:

class MyClass
{
    ...
}
MyClass mc = new MyClass();

有关结构,你没有创建一个对象,而是简单地设置一个值的变量:

For struct, you're not creating an object but simply set a variable to a value:

struct MyStruct
{
    public string name;
}
MyStruct ms;
//MyStruct ms = new MyStruct();     
ms.name = "donkey";

我不明白的是,如果通过 MYSTRUCT毫秒=新MYSTRUCT()声明变量,什么是关键字在这里是做给说法? 。如果结构不能是一个对象,什么是这里实例?

What I do not understand is if declare variables by MyStruct ms = new MyStruct(), what is the keyword new here is doing to the statement? . If struct cannot be an object, what is the new here instantiating?

推荐答案

结构(C#参考) MSDN上:

From struct (C# Reference) on MSDN:

在使用new运算符创建结构对象时,它被创建和适当的构造函数被调用。不同于类,结构可以不使用new运算符实例化。如果不使用新的,该字段将保持未分配而不能使用的对象,直到所有字段被初始化。

When you create a struct object using the new operator, it gets created and the appropriate constructor is called. Unlike classes, structs can be instantiated without using the new operator. If you do not use new, the fields will remain unassigned and the object cannot be used until all of the fields are initialized.

据我了解,你不会真正能够正确使用结构不使用的新的的,除非你确定你手动初始化所​​有字段。如果您使用new运算符,构造函数会为你做到这一点。

To my understanding, you won't actually be able to use a struct properly without using new unless you make sure you initialise all the fields manually. If you use the new operator, the constructor will do this for you.

。希望清除它。如果您需要澄清这让我知道。

Hope that clears it up. If you need clarification on this let me know.

修改

有相当长的评论跟帖,所以我想我会添加一些在这里。我想了解它的最好办法是给它一展身手。请在Visual Studio中控制台项目名为StructTest及以下code复制到它。

There's quite a long comment thread, so I thought I'd add a bit more here. I think the best way to understand it is to give it a go. Make a console project in Visual Studio called "StructTest" and copy the following code into it.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace struct_test
{
    class Program
    {
        public struct Point
        {
            public int x, y;

            public Point(int x)
            {
                this.x = x;
                this.y = 5;
            }

            public Point(int x, int y)
            {
                this.x = x;
                this.y = y;
            }

            // It will break with this constructor. If uncommenting this one
            // comment out the other one with only one integer, otherwise it
            // will fail because you are overloading with duplicate parameter
            // types, rather than what I'm trying to demonstrate.
            /*public Point(int y)
            {
                this.y = y;
            }*/
        }

        static void Main(string[] args)
        {
            // Declare an object:
            Point myPoint;
            //Point myPoint = new Point(10, 20);
            //Point myPoint = new Point(15);
            //Point myPoint = new Point();


            // Initialize:
            // Try not using any constructor but comment out one of these
            // and see what happens. (It should fail when you compile it)
            myPoint.x = 10;
            myPoint.y = 20;

            // Display results:
            Console.WriteLine("My Point:");
            Console.WriteLine("x = {0}, y = {1}", myPoint.x, myPoint.y);

            Console.ReadKey(true);
        }
    }
}

玩它。取出构造,看看会发生什么。尝试使用只有初始化一个变量构造函数(我评论过一出来......它不会编译)。试着用和不用的新的的关键字(我注释掉了一些例子,取消他们,并给他们一个尝试)。

Play around with it. Remove the constructors and see what happens. Try using a constructor that only initialises one variable(I've commented one out... it won't compile). Try with and without the new keyword(I've commented out some examples, uncomment them and give them a try).

这篇关于什么关键字"新"的确在C#中的结构?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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