Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I handle a C# codebase being edited in both VS 2013 and VS2015 with regards to the breaking change of "using static" in C# 6.0?

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.

like image 269
Raging Llama Avatar asked Feb 10 '23 12:02

Raging Llama


2 Answers

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(...);
like image 173
Wai Ha Lee Avatar answered May 05 '23 06:05

Wai Ha Lee


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.

like image 30
Paulo Morgado Avatar answered May 05 '23 05:05

Paulo Morgado