关键字var在.netframwork 2.0中不起作用-Visual Studio 2005 [英] keyword var is not working in .netframwork 2.0 - visual studio 2005

查看:120
本文介绍了关键字var在.netframwork 2.0中不起作用-Visual Studio 2005的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我在.net framework 3.5中开发了一个网站,然后老板要求我将其集成到2.0.现在几乎我已经解决了由于版本更改而产生的所有错误,但是我无法克服一个不允许我使用var关键字的错误.实际上,我正在使用EPPlus软件包将数据导出到我网站上的excel表格.代码需要在许多地方使用var关键字,但我收到一条错误消息,提示您是否缺少程序集引用?

即使我尝试使用

Hello all,

I developed a website in .net framework 3.5 and than I was asked by my boss to integrate that to 2.0. Now almost I have fixed all the errors that were generating because of version changes but I am not able to over come an error which is not allowing me to use var keyword. Actually I am making use of EPPlus package for exporting data to excel sheets in my website. The code needs to use the var keyword at many places and I get an error which says are u missing an Assembly reference ?

Even i tried with

type x = new type();

,但还是没有成功.有使用var的替代方法吗?请帮帮我.
这是我的代码:

but still no go. Is there any alternative for using var ? Please help me out.
Here is my code:

var cell = ws.Cells[rowIndex, colIndex];



                   //Setting the background color of header cells to Gray

                   var fill = cell.Style.Fill;
                   var vallign = cell.Style.VerticalAlignment;
                   var hallign = cell.Style.HorizontalAlignment;


谢谢!


Thank you !

推荐答案

Var关键字适用于.net Framework 3.0及更高版本,因此在.net f/w 2.0中不起作用.有关更多信息,请参阅:-

http://msdn.microsoft.com/en-us/library/bb383973.aspx [ ^ ]
Var keyword is avaliable for.net framework 3.0 and above hence it will not work in .net f/w 2.0. For more information please refer:-

http://msdn.microsoft.com/en-us/library/bb383973.aspx[^]


与使用var关键字相反,可以使用Object关键字.

对象类型是.NET Framework中Object的别名.在C#的统一类型系统中,所有类型(预定义和用户定义的类型,引用类型和值类型)都直接或间接继承自Object.您可以将任何类型的值分配给类型为object的变量.当将值类型的变量转换为对象时,将其装箱.将类型为object的变量转换为值类型时,将其取消装箱.

从Visual C#3.0开始,在方法范围内声明的变量可以具有隐式类型var.隐式类型化的局部变量是强类型化的,就像您自己声明了类型一样,但是编译器会确定类型.

使用.Net Framework 3.0及更高版本,您将使用

As opposed to using the var keyword, you can use the Object keyword.

The object type is an alias for Object in the .NET Framework. In the unified type system of C#, all types, predefined and user-defined, reference types and value types, inherit directly or indirectly from Object. You can assign values of any type to variables of type object. When a variable of a value type is converted to object, it is said to be boxed. When a variable of type object is converted to a value type, it is said to be unboxed.

Beginning in Visual C# 3.0, variables that are declared at method scope can have an implicit type var. An implicitly typed local variable is strongly typed just as if you had declared the type yourself, but the compiler determines the type.

Using .Net framework 3.0 and higher you''ll use

var myVar = new UnknownType();



使用低于3.0的.Net框架,您将使用



Using .Net framework lower than 3.0, you''ll use

object myVar = new UnknownType();


我还没有用过,但是在下面的链接上,可以使用var来解释.

I haven''t used myself but on following link, it is explained you can use var.

http://weblogs.asp.net/shahar/archive/2008/01/23/use-c-3-features-from-c-2-and-net-2-0-code.aspx[^]

By the way, can you not use object ?
-Milind


这篇关于关键字var在.netframwork 2.0中不起作用-Visual Studio 2005的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆