I'm building a DLL class library - I want to make it usable by as many people as possible. Which version of the .NET Framework and which C# version should I use? Is it possible to produce a backwards-compatible DLL or different DLLs for different versions? Or does Windows automatically update the .NET framework so I should just use the latest version? Any guidance appreciated!
相关问题
- Sorting 3 numbers without branching [closed]
- Graphics.DrawImage() - Throws out of memory except
- Generic Generics in Managed C++
- Why am I getting UnauthorizedAccessException on th
- 求获取指定qq 资料的方法
We target multiple runtime versions concurrently (.NET 1.1, .NET 2.0, and .NET 3.5) for some products.
We handle this in several ways:
eg:
Defining
NET_X_X
symbols in each of the solutionsFor .NET Framework specific code, we use preprocessor instructions such as this:
For clarification, the above lines starting with # are preprocessor commands. When you compile a solution, the C# Compiler (csc) pre-processes the source files. If you have an
#ifdef
statement, then csc will evaluate to determine if that symbol is defined - and if so, include the lines within that segment when compiling the project.It's a way to mark up code to compile in certain conditions - we also use it to include more intensive debugging information in specific verbose debug builds, like so:
It does make things more complicated, so we only tend to do it where we need to maintain a legacy .NET 1.1 or 2.0 instance (eg where a customer can't/won't upgrade).
I imagine that when .NET 4.0 rolls around, we'll do the same thing and just add a NET_4_0 symbol.
I would keep it at 2.0 unless you need to use 3.0 or 3.5 features.
From my point of view, if you want a wide range of users, you should do it with the early versions, 1.1 will be nice, because it will work on any machine has .Net what ever its version.
This solution works quite well. I just set up two different projects each with a unique "Project Properties->Build->Conditional compilation Symbols" and used in the code like this:
Personally, I'd target .NET 2.0. This means, among other things:
No linq
you CAN use lambda expressions
The thing is, you can use C# 3.x language features (the so-called syntactic sugar), but you can't use libraries that target C# 3.x (System.Core to name one, includes extension methods and linq).
I wouldn't try to support C# 1.x, as it's quite different from C# 2.x and higher. Besides, I expect most people who would use your library are people building new things, who wouldn't in their right minds use C# 1.x ;-)
If I were to start a new project, I would always use the newest runtime! If 3.5 is available, why would I need to start a project in 2.0, or 1.0 unless I knew that there is something seriously wrong with the new version? New versions mean fixing old bugs and adding new features so this is good.
When it comes to upgrading old project to a new version, then you need to consider your gains and losses. If its worthed, upgrade it, if not stick with the old version.
Be careful thought because new tools might not support older versions. Though this is not the case with 2010 as it will support all version up to 2.0.