如何将var类型声明为全局 [英] how to declare var type as globally

查看:150
本文介绍了如何将var类型声明为全局的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用var类型的声明作为全局,我这样使用,但它不接受,我怎么能全局声明这种类型的变量。或者我们有另外的声明来存储linq查询结果。只需



var testdata;

int count; //计数值通过方法参数传递为1或2或3,在这里我宣布为样本

if(count == 1)

{

testdata =(来自s中的spe。联系人选择s)。orderbyDesending(x => x.id).take(1);

}

  else   if (count ==  2 
{
testdata =(来自 s spe.contacts 选择 s)。orderbyDesending(x => x.id).take( 2 );
}











  if (count ==  3 
{
testdata =(来自 s spe.contacts 选择 s)。orderbyDesending(x => x.id).take( 3 );
}





但它不接受,请帮我解决这个问题。

解决方案

在这种特殊情况下,您可以使用 count 作为 Take 的参数:

  var  testdata =( from  s < span class =code-keyword> in  spe.contacts  select  s).OrderByDesending(x => x.id).Take(count) ; 



在其他情况下,如果您不返回匿名类型,则可以使用 IQueryable< Type> ,其中类型是您返回的元素的类型。

 IQueryable< Type> testdata =  null ; 
// 此处的if语句

//
if (testdata!= null
{
// 继续
}
else
{
// testdata为null,之前的if语句中没有任何代码被执行
}


没有什么可以全局定义,或者无论如何。没有var type这样的东西。



使用 var 声明变量时,它完全等同于写出具体的完全定义的类型。您只是假设编译器可以从代码的上下文中推断这种类型,并自动替换此类型而不是 var ,就像您自己动手一样。如果无法进行明确的推断,编译器会给出语法错误并正确解释歧义。



请参阅:

http://en.wikipedia.org/wiki/Type_inference [ ^ ],

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



-SA

hi, i want to use var type of declarations as globally, i used like this, but it is not accepting, how can i declare this type of variable globally. or do we have another declarations to store linq query result. simply

var testdata;
int count;//count value is coming through method parameters as 1 or 2 or 3, here i have declared as sample
if(count==1)
{
testdata=(from s in spe.contacts select s).orderbyDesending(x=>x.id).take(1);
}

else if(count==2)
{
testdata=(from s in spe.contacts select s).orderbyDesending(x=>x.id).take(2);
}






if(count==3)
{
testdata=(from s in spe.contacts select s).orderbyDesending(x=>x.id).take(3);
}



but it is not accepting, please help me to solve this.

解决方案

In this specific case, you can just use count as argument for Take:

var testdata = (from s in spe.contacts select s).OrderByDesending(x=>x.id).Take(count);


In other cases, if you do not return an anonymous type, you can use IQueryable<Type>, where Type is the type of the elements you return.

IQueryable<Type> testdata = null;
// your if statements here

// after the if statements:
if (testdata != null)
{
    // continue
}
else
{
    // testdata is null, none of the code in the previous if statements has been executed
}


There is nothing to "define globally", or anyhow. There is no such thing as "var type".

When you declare a variable using var, it is strictly equivalent to writing on of the concrete fully-defined types. You just assume that the compiler can infer this type from the context of your code and "automatically substitute" this type instead of var, as if you did it with your own hands. If unambiguous inference is not possible, the compiler will give you a syntax error with proper explanation of ambiguity.

Please see:
http://en.wikipedia.org/wiki/Type_inference[^],
http://msdn.microsoft.com/en-us/library/bb384061.aspx[^].

—SA


这篇关于如何将var类型声明为全局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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