4

I have a C# codebase which is being edited in both VS 2013 and VS 2015 CTP 6. With CTP 6 has come C# v6 which requires "using static" on some imports.

Is there a way in which I can determine which version (either VS or C#) is being used such that I can use a preprocessor directive to use either "using" or "using static"?

e.g.

#if CS6
   using static ...
#else
   using ...
#endif

A preprocessor directive is my initial thought. If there is another way to do this I am all ears.

svick
  • 236,525
  • 50
  • 385
  • 514
  • I was writing an answer then I reread your question and saw you said "*With CTP 6 has come C# v6 which requires "using static"*" Is it the case that a static `using` is **required** to use CTP6 and you want to be able to concurrently develop in C#5 and C#6 CTP6, or that you **want** the syntactic sugar of static `using`s **and** to concurrently develop in C#5 and C#6 CTP6? I ask because [MSDN](https://msdn.microsoft.com/en-us/magazine/dn683793.aspx) says "*it’s possible to eliminate an explicit reference to the type when invoking a static method*" which leads me to think the latter. – Wai Ha Lee Apr 04 '15 at 16:51
  • Hi Wai Ha Lee. I think it is the former case. Basically when I build in C# 5 - no problem. When I build in C# 6 the build fails because it wants "using static" - in this case for System.Environment. – Raging Llama Apr 04 '15 at 20:04
  • Out of interest, which member of `Environment` is causing the error? – Wai Ha Lee Apr 04 '15 at 20:16
  • It is the GetFolderPath() method. I appreciate your help :o) – Raging Llama Apr 04 '15 at 20:39
  • Hmm. Does calling [`System.Environment.GetFolderPath(...)`](http://msdn.microsoft.com/en-us/library/system.environment.getfolderpath(v=vs.100).aspx) not work when compiling as C#6 CTP6? – Wai Ha Lee Apr 04 '15 at 20:42
  • 1
    LOL. The blinding flash of the obvious that went flying past me. It works :o) Thanks Wai Ha Lee. – Raging Llama Apr 04 '15 at 22:11
  • No problem. :) I have to admit, I was rather curious as to why this was causing a problem for you. – Wai Ha Lee Apr 04 '15 at 22:15
  • 1
    Would you reply with an answer so that I can give you credit for this? – Raging Llama Apr 04 '15 at 22:23

3 Answers3

6

The static using shouldn't be required; it is syntactic sugar that has been added to C# 6.0. You should always be able to specify the fully qualified name of a static method to call it, e.g. instead of

using System.Environment;

// class and method declaration elided

var path = GetFolderPath(...);

You could always have

// no static using here!

// class and method declaration elided

var path = System.Environment.GetFolderPath(...);

or, if you don't have a class of your own called System (why would you do that?):

// still no static using here!
using System;

// class and method declaration elided

var path = Environment.GetFolderPath(...);
Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92
1

It isn't a breaking change if it doesn't break previously compilable code. Since there weren't static imports in C# before 6.0, this isn't a breaking change.

It is also not required. That would be a real breaking change.

If you want to work on a code base simultaneously with Visual Studio 2013 and Visual Studio 2015, you'll have to use the maximum common denominator which is C# 5.0.

Paulo Morgado
  • 14,111
  • 3
  • 31
  • 59
  • Hi Paulo. Thanks for the info. I have a question though. "It is also not required". I have heard this once before, but there is some piece of info I am missing here. In this particular instance I am using the GetFolderPath method in System.Environment. In C# 6, if I do not use either "using static System.Environment" or within the code use "System.Environment.GetFolderPath" I get a build error. Is there a configuration piece or some other such info I am missing? Thanks very much. – Raging Llama Apr 04 '15 at 22:34
  • Are you talking about [this](https://msdn.microsoft.com/library/system.environment.getfolderpath.aspx "Environment.GetFolderPath Method")? What exactly is the build error you're getting? – Paulo Morgado Apr 05 '15 at 11:47
  • Hi Paulo. Yes, System.Environment.GetFolderPath is indeed the function in question. The error I am getting is this: Error CS0138 A 'using namespace' directive can only be applied to namespaces; 'Environment' is a type not a namespace. Consider a 'using static' directive instead. Thanks again for your help on this one. – Raging Llama Apr 06 '15 at 11:22
  • That's because your using directive looks like this: `using System.Environment`. To import a class instead of a namespace, `C#-6.0` requires the using directive to be: `using static System.Environment` But that will be invalida prior to `C#-6.0'. You should use `using System;` and `Environment.GetFolderPath()`. – Paulo Morgado Apr 06 '15 at 16:21
1

You can try enabling C# 6.0 support in Visual Studio 2013 by following the instructions described here: How to enable C# 6.0 feature in Visual Studio 2013?

I have not tested it though.

Community
  • 1
  • 1
Erwin Mayer
  • 18,076
  • 9
  • 88
  • 126