-1

I have a method that has code that depends on c# 6.0. (The 6.0 new features are cool BTW!)

My co-workers may open it with an older version and be confused by the compile error or think it's a typo or syntax error. I don't want them wasting time, and I also want to CMA.

I'd like to mark the method with a marker attribute that indicates that this method uses c# 6.0.

Something like:

[LanguageVersion(6.0, "This uses a C# 6.0 (Visual Studio 2015) language feature!")]
public string MyCoolMethod() => "hello world";

Is there such an attribute in the framework? What would be the closest thing to indicate my intention?

EDIT (POSSIBLE SOLUTION):

Per user2864740's suggestion, I found a project setting under Build, Advanced Build... enter image description here

...Unfortunately, Visual Studio 2010 does not recognize the C# 6.0 value of the Language Version setting and simply thinks it's a grammar error. The Language Version of the project, as opened in Visual Studio 2010 project settings, is blanked out! enter image description here

EDIT (Chosen Solution):

This is the experience someone will get if they open this class in an older IDE: enter image description here

toddmo
  • 20,682
  • 14
  • 97
  • 107
  • 1
    you could write your own! – Daniel A. White Sep 19 '15 at 19:12
  • And what should do a compiler that doesn't understand C# 6.0 when it sees this attribute? – Steve Sep 19 '15 at 19:12
  • 3
    It's probably best to talk to your coworkers about the new c# features and make sure everyone's machine is setup properly so they can compile it. – David Sherret Sep 19 '15 at 19:13
  • 1
    or just use code comments! – Daniel A. White Sep 19 '15 at 19:13
  • @Steve, nothing. It would just be a marker attribute. – toddmo Sep 19 '15 at 19:14
  • @DanielA.White, that is what I would do. A simple xml comment probably. – Rahul Sep 19 '15 at 19:15
  • So, what is the reason for your co-workers to work on this project? Why they try to fix a problem when the project is not for their compiler? I don't know, but this seems to be a false problem. (or some problem with the communications in your shop) – Steve Sep 19 '15 at 19:17
  • It seems a bit silly for a *compiler* version .. because the code would fail to compile without the correct compiler and thus the annotation would never be uhm, annotated. – user2864740 Sep 19 '15 at 19:18
  • @DavidSherret, note that it isn't the .net framework that denotes the language feature; it's the version of c# (and correspondingly, the compiler). The framework version can vary independently of the version of C#. – toddmo Sep 19 '15 at 19:18
  • @user2864740, without some comment or indication, they would waste time scratching their heads (and maybe cussing me), until they figured that out. – toddmo Sep 19 '15 at 19:21
  • @Steve, we are in a transition period (lol, aren't we always?). The shop standard is VS 2015 but we have some hold outs, for whatever reason, and we work on the same code base. For one ticket, I might work on it, then for the next ticket, another developer might pick it up. – toddmo Sep 19 '15 at 19:23
  • @toddmo I think a better approach may be: Is there a *Visual Studio Project/Solution Setting* that clearly indicates/mandates - from a VS UI experience - a minimum compiler version? (Due to the code failing to compile with an incompatible version; this differs from it compiling, but failing to function as expected.) – user2864740 Sep 19 '15 at 19:23
  • @user2864740, good idea! I'll check it out. – toddmo Sep 19 '15 at 19:25
  • @toddmo At the 'worst' might be able to put something into a before-build action. – user2864740 Sep 19 '15 at 19:26
  • You could use the preprocessor directive #if with a define like USE_CSHARP_6_0. Then create tow configurations, one with this define and one without. Your coworkers could use the second one while you could use the first. Of course you need to provide working code for both compilers – Steve Sep 19 '15 at 19:32
  • @DanielA.White, you are the winner. Can you answer and let me give you 15 points please? :) – toddmo Sep 19 '15 at 19:57

1 Answers1

0

You could use a custom attribute or simply use code comments.

Daniel A. White
  • 187,200
  • 47
  • 362
  • 445