Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/308.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
“的含义;这";对于结构(C#)_C# - Fatal编程技术网

“的含义;这";对于结构(C#)

“的含义;这";对于结构(C#),c#,C#,根据MSDN(本规范第11.3.6节): 在 结构,此对应于一个输出 结构类型的参数,以及 在的实例函数成员中 结构,this对应于ref 结构类型的参数。两者 案例,此被归类为 变量,并且可以修改 为其 函数成员已由调用 分配给此或通过传递此 作为ref或out参数 我不明白。结构的与类的有何不同?代码示例很受欢迎Eric Lippert不久前在变异只读结构方面做了出色的工作,这将真正帮助您澄清问题。甚至还有一个代码示例和一个测试 突出的一点是,structs遵守值语义,而classes不遵守

根据MSDN(本规范第11.3.6节):

在 结构,
对应于一个
输出
结构类型的参数,以及 在的实例函数成员中 结构,
this
对应于
ref
结构类型的参数。两者 案例,
被归类为 变量,并且可以修改 为其 函数成员已由调用 分配给
或通过传递
作为
ref
out
参数


我不明白。结构的
与类的
有何不同?代码示例很受欢迎

Eric Lippert不久前在变异只读结构方面做了出色的工作,这将真正帮助您澄清问题。甚至还有一个代码示例和一个测试

突出的一点是,
struct
s遵守值语义,而
class
es不遵守值语义,因此
这个
对于这两个词来说一定有不同的含义
对于
,此
只读
,但对于
结构
。以下代码是合法的

struct Point {
    public int x;
    public int y;

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

    public void DoGoTime() {
        GoTime(ref this);
    }

    public static void GoTime(ref Point p) {
        p.x = 100;
        p.y = 100;
    }
}

但如果“
struct
”替换为“
class

,则在处理结构时,处理的是值类型

在类中,“this”是对当前实例的引用。这允许您通过设置类的属性/字段来改变类实例

然而,如果你在一个结构中,事情的表现就不同了。当您使用结构的方法时,“this”允许您修改结构。然而,如果您在方法中使用这个,那么您几乎总是在处理“原始”结构的副本

例如:

struct Test
{
    int i;
    void Mutate() {
         this.i += 1;
    }
}
使用此选项时:

void MutateTest(Test instance)
{
    instance.Mutate();
}

{
    Test test = new Test();
    test.i = 3;
    Console.WriteLine(test.i); // Writes 3
    test.Mutate(); // test.i is now 4
    Console.WriteLine(test.i); // Writes 4
    MutateTest(test); // MutateTest works on a copy.. "this" is only part of the copy itself
    Console.WriteLine(test.i); // Writes 4 still
}
现在,陌生人的部分-这是有效的,以及引用的内容:

struct Test
{
    public Test(int value)
    {
       this.i = value;
    }
    int i;

    void Mutate(int newValue) {
         this = new Test(newValue); // This wouldn't work with classes
    }
}


///
{
    Test test = new Test();
    test.i = 3;
    Console.WriteLine(test.i); // Writes 3
    test.Mutate(4); 
    Console.WriteLine(test.i); // Writes 4

Jason的回答和Eric的帖子展示了这篇文章的一个方面,这很有趣。。。但还有一个更令人担忧的问题:

您可以在方法中重新分配
,即使该类型在其他方面是不可变的。

为了演示它,我们将使用存储在非只读变量中但包含只读字段的结构:

using System;

public struct LooksImmutable
{
    private readonly int value;
    public int Value { get { return value; } }

    public LooksImmutable(int value)
    {
        this.value = value;
    }

    public void GoCrazy()
    {
        this = new LooksImmutable(value + 1);
    }
}

public class Test
{
    static void Main()
    {
        LooksImmutable x = new LooksImmutable(5);
        Console.WriteLine(x.Value);
        x.GoCrazy();
        Console.WriteLine(x.Value);
    }
}

我感到原力中有一股巨大的骚动,仿佛数百万人突然发出恐怖的叫喊声,要求警察纠正。请不要向我的C++同事展示。呃,C++中的const是个谎言。在C++中,const常常意味着“我保证不改变这个东西”,而不是“我保证这东西是不可变的”。乔恩例子不错。这里您要演示的是,尽管结构可能是不可变的,但保存结构的变量却不是。这个变量仍然可以变化;这就是为什么它被称为变量。这只是一种特别可怕的方式,让它发生变化。