In C# 3.0 we use “var” what is its alternative in C# 2.0?

心已入冬 提交于 2019-12-11 02:22:28

问题


I am learning plug able architecture in .Net using Managed Extensibility Framework (MEF.) I saw sample code on the net, but when I tried to implement it I got stuck at one point.

The code was using:

 var catalog = new AttributedAssemblyPartCatalog(Assembly.GetExecutingAssembly());
 var container = new CompositionContainer(catalog.CreateResolver());

This var is available on C# 3.0 where as I am coding in C# 2.0.

What is the alternative of above two statements? How can I make them work in c# 2.0 using VS 2005?


i tried this bt its saying now

Error 1 The type or namespace name 'AttributedAssemblyPartCatalog' could not be found (are you missing a using directive or an assembly reference?) C:\Documents and Settings\test\Desktop\MEFDemo\MEFDemo\Program.cs 31 13 MEFDemo

where as i have added referance to SystemComponentModel.Composition


回答1:


This is the use of type inference in C# 3.0.

When using the keyword

var

in c# 3.0 the compiler infers the type. See scott guthries explanation

In c# 2.0 you have to declare the type of the variable the same as c# 1.1

e.g.

Type variableName = new Type();

Making you above code example

AttributedAssemblyPartCatalog catalog = new AttributedAssemblyPartCatalog(Assembly.GetExecutingAssembly());

CompositionContainer container = new CompositionContainer(catalog.CreateResolver());

HTH




回答2:


Basically, var forces the compiler to determine (infer) the compile-time type of a variable based on it's "initializer" -- effectively, an expression to the right from = sign. Here the types are obvious:

AttributedAssemblyPartCatalog catalog = 
    new AttributedAssemblyPartCatalog(Assembly.GetExecutingAssembly());
CompositionContainer container = 
    new CompositionContainer(catalog.CreateResolver());

And make sure you add using System.ComponentModel.Composition; statement. Plus, be advised that AttributedAssemblyPartCatalog was renamed to AssemblyCatalog.




回答3:


AttributedAssemblyPartCatalog catalog = new 
    AttributedAssemblyPartCatalog(Assembly.GetExecutingAssembly());

CompositionContainer container = new 
    CompositionContainer(catalog.CreateResolver());



回答4:


Variables in C# are still strongly typed. var is implicit typing - see the MSDN.

In most cases it just means you have to type less, but there are cases where it's necessary - in the second example on the page I've linked to:

var must be used because the result is a collection of anonymous types, and the name of that type is not accessible except to the compiler itself.




回答5:


var is a C# 3.0 keyword and does nothing other than inferring the strong type from the initialization value.

In the absence of var, you manually do what the compiler is doing behind the scenes; you specify the type of the variable in the declaration.

Hence;

AttributedAssemblyPartCatalog catalog = new 
    AttributedAssemblyPartCatalog(Assembly.GetExecutingAssembly());

CompositionContainer container = new 
    CompositionContainer(catalog.CreateResolver());



回答6:


MEF uses LINQ, so it requires .NET 3.5. You won't have any luck trying to use it on .NET 2.0.



来源:https://stackoverflow.com/questions/856780/in-c-sharp-3-0-we-use-var-what-is-its-alternative-in-c-sharp-2-0

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!