将类型“var”传递给 C# 中的方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14281289/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Passing type 'var' into a method in C#
提问by svs
I have some sub queries which are of data type var. These are getting working on a data table. I would like to pass these into another method, how can i do that? Ex:
我有一些数据类型为 var 的子查询。这些正在处理数据表。我想将这些传递给另一种方法,我该怎么做?前任:
var subquery1= from results in table.AsEnumerable()
where //some condition
select new
{
column1=results.field1,
column2 = results.field2
etc.,
}
var subquery2= from results in table.AsEnumerable()
where //different condition
select new
{
column1=results.field1,
column2 = results.field2
etc.,
}
Now, I would like to define some method to pass these subquery1, subquery2 as variables. ex:
现在,我想定义一些方法来将这些 subquery1、subquery2 作为变量传递。前任:
void updategrid(var queries)
{
//some logic
}
And execute the method:
并执行该方法:
updategrid(subquery1);
updategrid(subquery2);
When i try to use var in the method when defining it is not liking it. Please help me in how to do that. Thanks.
当我在定义它时尝试在方法中使用 var 时,我不喜欢它。请帮助我如何做到这一点。谢谢。
采纳答案by Ilya Ivanov
Your result is a sequence of anonymous objects. var
Keyword is just saying to compiler "Hey, deduce arguments for me from usage". The process of deducing types from usage is called Type Inference. But you can't say to C# compiler to infer types of method arguments in method declaration. Moreover, in case of anonymous objects
you cant specify their name. So you shouldn't pass them around outside and inside other methods. You can cast them to dynamic
and then access their members via CallSite
(compiler creates them when you access a member of dynamic object like myDynamic.Name
) or you can cast anonymous object to object
and access its properties via reflection, but all these methods are non C# idiomatic and really miss the point of anonymous objects.
Instead of creating anonymous objects in query
您的结果是一系列匿名对象。var
关键字只是对编译器说“嘿,从使用中为我推断出参数”。从使用中推导出类型的过程称为类型推断。但是你不能告诉 C# 编译器在方法声明中推断方法参数的类型。此外,万一anonymous objects
你不能指定他们的名字。所以你不应该在其他方法的外部和内部传递它们。您可以将它们转换为dynamic
然后通过访问它们的成员CallSite
(编译器在您访问动态对象的成员时创建它们,例如myDynamic.Name
),或者您可以将匿名对象转换为object
并通过反射访问其属性,但所有这些方法都不是 C# 惯用的,并且真的错过了匿名对象的重点。而不是在查询中创建匿名对象
select new
{
//members selection
}
Create a custom type
创建自定义类型
public class Result
{
//Members declaration
}
And instantiate it as the result of query like in next example: (you can substitute var
keyword instead of IEnumerable<Result>
- compiled code will be the same)
并将其实例化为下一个示例中的查询结果:(您可以替换var
关键字而不是IEnumerable<Result>
- 编译后的代码将相同)
IEnumerable<Result> subquery1 = from results in table.AsEnumerable()
where //some condition
select new Result
{
//fill members here
}
Method will look like
方法看起来像
void updategrid(IEnumerable<Result> queries)
{
//some logic here with strongly typed sequence
}
Then you will call updategrid
like simply updategrid(subquery1)
and you will have all the beauty of statically typed sequence of your elements in the body of updategrid
然后,您将updategrid
简单地调用like,updategrid(subquery1)
并且您将拥有元素的静态类型序列的所有美感updategrid
回答by atevans
Var is not a data type. It is short hand for "figure out what data type this actually is when you compile the app". You can figure out what the data type actually is and use that for your parameter or you could create a generic function that will work with any data type.
Var 不是数据类型。它是“在编译应用程序时弄清楚这实际上是什么数据类型”的简写。您可以找出实际的数据类型并将其用于您的参数,或者您可以创建一个适用于任何数据类型的通用函数。
回答by musefan
You could probably use a dynamicreference, but I wouldn't go for that.
您可能可以使用动态引用,但我不会这样做。
Instead, the better option is to create actual classes for those data types and pass them to the methods instead.
相反,更好的选择是为这些数据类型创建实际的类并将它们传递给方法。
For example:
例如:
public class MyColumns
{
public string column1 {get;set;}
public string column1 {get;set;}
//etc.
}
Which you can create like this:
您可以像这样创建:
var subquery1= from results in table.AsEnumerable()
where //some condition
select new MyColumns
{
column1=results.field1,
column2 = results.field2
//etc.
};
and have a function like this:
并有这样的功能:
public void updategrid(IEnumerable<MyColumns> queries)
{
}
回答by Oded
void updategrid(var queries)
Is not valid C#.
无效的 C#。
var
is syntactic sugar - the variable has a type, but you don't need to declare it if the compiler can statically determine what it should be.
var
是语法糖——变量有一个类型,但如果编译器可以静态确定它应该是什么,你就不需要声明它。
With a parameter, the compiler doesn't have enough information to determine the type.
对于参数,编译器没有足够的信息来确定类型。
Similarly, you can't declare a variable with var
without assignment:
同样,你不能在var
没有赋值的情况下声明一个变量:
var something;
You will need to ensure the types of subquery1
, subquery2
and the parameter to updategrid
are all the same.
您将需要确保的类型subquery1
,subquery2
以及参数updategrid
都是一样的。
回答by Oskar Berggren
In C#, there is no 'var' type. 'var' is just a keyword that tells the compiler to 'analyse whatever comes next to find out the type'. This is done during compilation, not runtime.
在 C# 中,没有“var”类型。“var”只是一个关键字,它告诉编译器“分析接下来的任何内容以找出类型”。这是在编译期间完成的,而不是运行时完成的。
The variables you speak of are actually typed as anonymous types that the compiler automatically generates during compilation. Such types, since they have no name, cannot be used to declare parameter types or return values.
您所说的变量实际上是编译器在编译期间自动生成的匿名类型。此类类型由于没有名称,因此不能用于声明参数类型或返回值。
You should either create explicit classes for this data, or in .Net4 or later use the Tuple<> generic type.
您应该为此数据创建显式类,或者在 .Net4 或更高版本中使用 Tuple<> 泛型类型。
回答by Alexander Balte
You can declare method with var
type of argument. But you can write so:
您可以使用var
参数类型声明方法。但是你可以这样写:
static void updategrid(dynamic queries)
{
}
var
means take type from right-hand side and declare variable with this type from left-hand side
and this is processed in compile-time. As you can seen using var
as method argument type makes no sense.
var
意味着take type from right-hand side and declare variable with this type from left-hand side
这是在编译时处理的。正如您所看到的,使用var
as 方法参数类型是没有意义的。
回答by Tilak
Use Object
or dynamic
使用Object
或dynamic
void updategrid(object queries)
void updategrid(dynamic queries)
var
-> static type that is determined by the right side of expression. Cannot be used as parameter/return type
var
-> 由表达式右侧确定的静态类型。不能用作参数/返回类型
object
-> Base class for all .NET types.
object
-> 所有 .NET 类型的基类。
dynamic
-> The type is resolved at runtime. Hence compile time checks are not made, and intellisense is not available. It has performance cost also.
dynamic
-> 类型在运行时解析。因此不进行编译时检查,并且智能感知不可用。它也有性能成本。