根据MSDN(C#规范第11.3.6节):
在struct的实例构造函数中,
this对应于outstruct类型的参数,并且在struct的实例函数成员中,this对应于refstruct类型的参数.在两种情况下,this被分类为一个变量,它可以修改的量,功能部件是由分配给调用的整个结构this或通过传递this作为ref或out参数.
我不明白.如何是this一个结构比一类有什么不同?代码示例表示赞赏
jas*_*son 11
埃里克·利珀特(Eric Lippert)有一篇关于变异sa 的精彩帖子,readonly struct这对于你来说真的有助于澄清这个问题.甚至还有一个代码示例和一个测验!
重点是structs服从价值语义而classes则不然而且this必须意味着两者不同.this是readonly为了class,但不是为了struct.以下代码是合法的
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;
}
}
Run Code Online (Sandbox Code Playgroud)
但如果" struct"被" class." 取代,则不是.
当你处理结构时,你正在处理值类型.
在类中,"this"是对当前实例的引用.这允许您通过在类上设置属性/字段来改变类实例.
但是,如果你在一个结构中,事情就会有所不同.当你使用struct的方法时,"this"允许你改变结构.但是,如果您在方法中使用它,则几乎总是处理"原始"结构的副本.
例如:
struct Test
{
int i;
void Mutate() {
this.i += 1;
}
}
Run Code Online (Sandbox Code Playgroud)
当你使用这个:
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
}
Run Code Online (Sandbox Code Playgroud)
现在,陌生人的一部分 - 这是有效的,这句话的意思是:
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
Run Code Online (Sandbox Code Playgroud)
杰森的回答和埃里克的帖子显示其中一个方面this很有趣......但还有一个更令人担忧的是:
您可以this在方法中重新分配,即使该类型是不可变的.
为了演示它,我们将使用一个存储在非只读变量中的结构,但它包含一个只读字段:
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);
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1491 次 |
| 最近记录: |