1
我有以下两类C#6初级男星
public class Point(int x, int y)
{
public int X { get; } = x;
public int Y { get; } = y;
}
public class Point2
{
public int X { get; private set; }
public int Y { get; private set; }
public Point2(int x, int y)
{
this.X = x;
this.Y = y;
}
}
,并使用JetBrains公司dotpeek我拿到的时候反编译如下:
namespace ConsoleApplication1
{
public class Point
{
public int X
{
get
{
return this.\u003CX\u003Ek__BackingField;
}
}
public int Y
{
get
{
return this.\u003CY\u003Ek__BackingField;
}
}
public Point(int x, int y)
{
}
}
}
和
namespace ConsoleApplication1
{
public class Point2
{
public int X { get; private set; }
public int Y { get; private set; }
public Point2(int x, int y)
{
this.X = x;
this.Y = y;
}
}
}
我无法理解使用主要构造函数的类类,反编译器在反编译时为空。认为它将和Point2一样设置后台字段。 有人可以解释这一点吗?
FWIW,[主要构造已不再是C#6.0部分(https://roslyn.codeplex.com/wikipage?title =语言%20Feature%20Status)。 – 2014-11-01 13:57:11
查看[this](http://thomasardal.com/primary-constructors-in-c-6-0/)获取正确的反编译代码。 – 2014-11-01 14:03:23
这是默认的构造函数,没有显式构造函数的每个C#类都有一个。由C#编译器自动生成。你的反编译器不会说明为什么这是必要的,它省略了':base()'。必须确保基础构造函数也可以运行。你的情况下的System.Object构造函数。使用ildasm.exe可以给你更多的见解,它隐藏较少。 – 2014-11-01 14:04:15