如何为"var"数据类型分配null? [英] How to assign a null to 'var' datatype ?

查看:90
本文介绍了如何为"var"数据类型分配null?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用VS2008.
我想为var数据类型分配一个空值.怎么做?这是我的问题.
[ 解决方案2 解释了为什么无法做到这一点-SA]

I am using VS2008.
I want to assign a null value to var datatype. How to do it? Here is my problem.
[Solution 2 explains why you cannot do it — SA]

class cl
{
buildmenu()
{
if(condition=1)
var vv=some linq query                          -----A
else
if(condition=2)
var vv=some linq query                          -----B
}

...
...
...
foreach (var vx in vv)                           ----C
{

}}


现在,如果您看到A和B,它们是本地的,并且我不能在C位置使用它(因为A和B是在本地定义的).
我也无法全局定义var vv,因为它是不允许的.

我应该怎么办 ????我想为var数据类型分配一个空值,以便无论从A或B得出的结果如何,都可以在C处使用. [ 解决方案4 解释了如何处理-SA]

快速帮助将不胜感激.

问候.


Now if you see A and B, they are local and i cannot use it location C(since A and B were defined locally).
I also cant define var vv globally since it is not allowed.

What should i do ???? I want to assign a null value to var datatype so that whatever result comes out from A or B, could be used at C. [Solution 4 explains what to do — SA]

Quick help will be appreciated.

Regards.

推荐答案

没有诸如var类型的东西. 这根本不是类型.这是用于类型推断的关键字,请参见 ^ ], http://msdn.microsoft. com/en-us/library/ms364047%28v = vs.80%29.aspx [

There is not such thing as var type. This is not a type at all. This is a keyword used for type inference, see http://en.wikipedia.org/wiki/Type_inference[^], http://msdn.microsoft.com/en-us/library/ms364047%28v=vs.80%29.aspx[^].

使用此关键字严格等效于编译器从上下文中推断出的类型的显式类型名称(如果可能).由于null含糊不清(引用类型的任何nullable都可以与null赋值兼容),因此无法进行推断,因此不能使用var.

Using this keyword is strictly equivalent to explicit type name of the type which would be inferred by a compiler from the context if this is possible. As null is ambiguous (any nullable of reference type is assignment-compatible with null), inference is impossible, so var can not be used.

重要的是要了解使用并不意味着要严格输入.推断类型时,将其与任何其他静态已知类型一样对待..


shikhar gilhotra写道:
shikhar gilhotra wrote:

我也不能全局定义var vv,因为不允许使用它.我该怎么办?

I also can''t define var vv globally since it is not allowed. What should I do?



显然,在我的回答中,我已经解释了您无法执行的操作以及原因,请参见
解决方案2 .我没有给您和有关如何做的想法.我现在将解决此问题.

让我们考虑解释LINQ用法的第一个Microsoft示例,
http://msdn.microsoft.com/en-us/library/bb397906.aspx [ ^ ].

这是使用var编写的示例:



Apparently, in my answer I''ve explained what you cannot do and why, see Solution 2. I did not give you and idea on what to do. I''ll fix this problem now.

Let''s consider the very first Microsoft sample explaining the use of LINQ,
http://msdn.microsoft.com/en-us/library/bb397906.aspx[^].

Here is that sample, written using var:

static void Main(string[] args) {
    int[] numbers = new int[7] { 0, 1, 2, 3, 4, 5, 6 };
    var numQuery =
        from num in numbers
        where (num % 2) == 0
        select num;
    foreach (int num in numQuery)
        Console.Write("{0,1} ", num);
} //Main



现在,您要使用不同的查询创建一个if分支.您正确地解释了为什么无法使用var执行此操作.让我们合乎逻辑.这意味着,如果要使用类似的代码逻辑和结构,则需要使用显式类型排除var.方法如下:



Now, you want to make an if branch with different queries. You correctly explain why you cannot do it with var. Let''s be logical. It means that if you want to use the similar code logic and structure, you will need to exclude var by using explicit type. Here is how:

static void Sample(bool condition) {
    int[] numbers = new int[7] { 0, 1, 2, 3, 4, 5, 6 };
    // here is the declaration in question:
    // you don't need to assign null to numQuery
    // because you don't use unassigned value of it anyway, but could be
    // IEnumerable<int> numQuery = null;
    IEnumerable<int> numQuery;
    if (condition)
        numQuery =
            from num in numbers
            where (num % 2) == 0
            select num;
    else
        numQuery =
            from num in numbers
            where (num > 4)
            select num;
    foreach (int num in numQuery)
        Console.Write("{0,1} ", num);
} //Sample



重要的是要理解,运行时类型与上面所示代码示例中声明的编译时类型不同.您可以使用与foreach容器的操作数兼容的最接近的通用基类型.

该问题被简化为为显式声明选择适当的查询类型的问题.此处对规则进行了很好的解释: http://msdn.microsoft.com/en-us/library/bb397924.aspx [ ^ ].

祝你好运,

—SA



It is important to understand, that the run-time type is different from the compile-time type declared in the code sample shown above. You can use the nearest common base type which is assigned-compatible with the operand of your foreach container.

The problem is reduced to the problem of selection of appropriate query type for the explicit declaration. The rules are very well explained here: http://msdn.microsoft.com/en-us/library/bb397924.aspx[^].

Good luck,

—SA


您可以在.NET中使用Nullable类型.参考: http://msdn.microsoft.com/en-us/library/2cf62fcy%28v = vs.80%29.aspx [
You can use Nullable types in .NET. Ref: http://msdn.microsoft.com/en-us/library/2cf62fcy%28v=vs.80%29.aspx[^]


这篇关于如何为"var"数据类型分配null?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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