Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/333.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#_Properties - Fatal编程技术网

C# 非法财产申报

C# 非法财产申报,c#,properties,C#,Properties,我一直在试图找出其中哪些是非法的: public int MyProperty { get; set;} public int MyProperty { get; protected set; } public int MyProperty { get; } public int MyProperty { get{ return 0; } } public int MyProperty { get { return 0; } set { ; } } 他们没有一个给我返回一个错误,但其中

我一直在试图找出其中哪些是非法的:

public int MyProperty { get; set;}

public int MyProperty { get; protected set; }

public int MyProperty { get; }

public int MyProperty { get{ return 0; } }

public int MyProperty { get { return 0; } set { ; } }
他们没有一个给我返回一个错误,但其中一个应该是非法的。有人能告诉我是哪一个以及为什么吗?

在C#6.0中,在以前的版本中,所有这些都是合法的

   public int MyProperty { get; }
是非法的-您不能分配(即设置)返回值。然而,在C#6.0中,您可以

  public int MyProperty { get; } = 0;
或在构造函数中赋值

  public MyClass() {
    MyProperty = 0;
  }
在C#6.0中,在以前的版本中,所有这些都是合法的

   public int MyProperty { get; }
是非法的-您不能分配(即设置)返回值。然而,在C#6.0中,您可以

  public int MyProperty { get; } = 0;
或在构造函数中赋值

  public MyClass() {
    MyProperty = 0;
  }
这是违法的

你可以简单地写

public int MyProperty { get; private set; }
存档

这是违法的

你可以简单地写

public int MyProperty { get; private set; }

为了将其存档

它现在在C#6中是合法的。我只是在他之前的问题中添加了一个问题,以了解该问题所指的是C#的哪个版本。它现在在C#6中是合法的。我只是在他之前的问题中添加了一个问题,以了解该问题所指的是C#的哪个版本。您能否添加您的问题所指的C#版本你能补充一下你的问题所指的C版本吗?