I'm trying to reproduce the C# compiler error CS0840 with the exact code that's given in the website:
class Test36
{
public int myProp { get; } // CS0840
// to create a read-only property
// try the following line instead
public int myProp2 { get; private set; }
}
public Form1()
{
InitializeComponent();
Test36 test = new Test36();
}
I'm running it on .NET 4.0 using Visual Studio Community 2015. Surprisingly, I cannot reproduce it. Compiler doesn't throw any error:
Why the compiler isn't throwing any error?
You're using Visual Studio 2015, which implements C# 6. The fact that you're targeting .NET 4 is irrelevant - most of the C# 6 language features don't depend on framework features at all. The C# 6 code you're using can easily be compiled without reference to any modern CLR or framework features - it could have worked with .NET 1.0 if the language designers had decided to :)
You'll need to set you language level to C# 5 to see an error here. Do that in the project properties / Build / Advanced dialog:
You'll then get this error:
error CS8026: Feature 'readonly automatically implemented properties' is not available in C# 5. Please use language version 6 or greater.
Admittedly that's not the error you actually wanted to see - I think you'll need to use an earlier version of the compiler to get that exact error.
I guess it is because you are on Visual Studio 2015 with C# 6 which allows you to specify properties that are only set from the constructor (aka read-only properties).
See the following example:
class Test
{
public Test() // <-- this one does compile since it is the constructor
{
MyProp = 1;
}
public void SomeMethod() // <-- this one doesn't compile
{
MyProp = 1;
}
public int MyProp { get; } // <-- no CS0840 any more!
}
Related
Say I have a snippet like this from an external dll that I have to use.
public class DerivedClass : IBaseInterface
{
[JsonIgnore]
[NonOverridable]
[InspectorReadOnly]
public string name;
string IBaseInferface.name {
get {
return name;
}
set {
name = value;
}
}
}
And I am publicising the dll using https://github.com/AzeTheGreat/Publicise so that I can access the private members as public.
But if I use the publicised dll in JetBrains Rider like this:
public class Main
{
private DerivedClass dobj;
public void func()
{
// Jetbrains intellisense shows an ambiguous referencee error
// <AccessTest>\Main.cs:339 Ambiguous reference:
// string IBaseInterface.name (in class DerivedClass)
// string name (in class DerivedClass)
// match
dobj.name = "";
}
}
However, the project builds fine without any errors. And doing the same thing Visual Studio, VS intellisense does not detect any errors at all.
One possible explanation is that, the JetBrains decompiler seems to show the publicised IBaseInterface.name as public but Visual Studio decompiler does not. (This does not make any sense to me, since they are the same assembly and the decompilers are showing different things.)
Is there a way to solve this (I can't change the external DLL) ? Or at least mark the line so that intellisense doesn't complain in JetBrains Rider?
This line of code gives me the error
error CS1519: Unexpected symbol `=' in class, struct, or interface member declaration
public Transform target { get; protected set; } = null;
I don't get if I'm doing the get and set in a wrong way. I was following a tutorial and I works just fine for the dude. Also the error only appears inside Unity. Visual studio don't give me any error message.
Unity versions older than 2017 do not support C# 6.0 features such as property initializers.
Use a simple backing field initialization, or upgrade to the latest Unity (2019) which supports C# 7.3
Edit:
since objects always default to null
public Transform target { get; protected set; } = null;
is same as
public Transform target { get; protected set; }
In my project, I've tried to tidy up the structure a bit and put all the C# classes into a separate folder (App_Code).
The problem is that it seems even if the project is set to use C# 7.1 (and .net framework 4.7)
Trying to create in subfolder class with newer syntax in example:
Namespace MyProject {
public static class Class1
{ (//*****Available since C# 6.0****)
public static string Test1 { get; } = ConfigurationManager.AppSettings["User"];
public static int Test2 ()
{
string s = "10";
int.TryParse(s, out int y); //*****Available since C# 7.0
return y;
}
}
This causes a compilation error stating, that this feature requires version 6.x (or 7.x in the case of "Test2" above) instead of 5.0.
Intelisense prompts me to change the version but even after following the prompt
the error persists.
Not sure if it matters, but classes in the App_Code folder have the same namespace as the ones in the rest of the project.
I use VS 2017 Pro.
I have a following piece of code:
[DefaultParameterValue(false)]
public bool IsDataAvailable{ get; set; }
This used to work fine when I was using Visual Studio 2013 Professional.
Recently, I upgraded to Visual Studio 2015 Professional version and this line started giving Error:
"CS0592 Attribute 'DefaultParameterValue' is not valid on this declaration type. It is only valid on 'parameter' declarations."
I could not get any help from google why this behavior occurs. Any idea?
Maybe there was a Bug in VS2013 and they fixed it in VS2015. That could be a
reason why it workd in VS2013.
You cant use DefaultParameterValue on Properties cause its for "Parameter".
You can use it for Methods.
If you want a default value for your Setter you can do somehting like this:
private bool _IsDataAvailable =false;
public bool GetIsDataAvailable()
{
return _IsDataAvailable;
}
public void SetIsDataAvailable([DefaultParameterValue(false)] bool b)
{
_IsDataAvailable = b;
}
Greetings
If I try to compile the following code with scriptCS it fails compilation with a syntax error for the line containing the primary constructor (Error CS 1514 '{' expected to be exact):
public class MyClass(string someName)
{
public string Name { get; } = someName;
}
I know that the new syntax features have to be enabled on a per-project basis in the first CTP of Visual Studio 2014, but couldn't find a similar setting for scriptCS (or any other information about C# 6 and scriptCS). My understanding is, that scriptCS is using Roslyn, so there should be a way to support those features.
So how do I enable the new C# 6 syntax features in scriptCS? Or isn't that possible (yet)?