c#中变量和字符串之间的区别 [英] Difference between variable and string in c#

查看:93
本文介绍了c#中变量和字符串之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

`c`中`variable`和`string`有什么区别?



为什么`c#`只支持这个

What is the difference between `variable` and `string` in `c#`

Why the `c#` is supporting only this

var data = GetData("");



为什么不呢?


Why not this?

string data = GetData("");



或者它会支持两者?哪一个更好用?如何实现?


Or it will support both? Which one is better to use ? How it is implemented?

  private DataTable GetData(string query)
{
    string conString = ConfigurationManager.ConnectionStrings["ProjectConnectionString"].ConnectionString;
    SqlCommand cmd = new SqlCommand(query);
    using (SqlConnection cn = new SqlConnection(conString))
    {
        using (SqlDataAdapter da = new SqlDataAdapter())
        {
            cmd.Connection = cn;
            da.SelectCommand = cmd;
            using (DataTable dt = new DataTable())
            {
                da.Fill(dt);
                return dt;
            }


        }
    }


}

推荐答案

你的函数的返回类型是DataTable,这就是为什么

the return type of your function is DataTable that's why
//it will work
var data = GetData(""); 




// it will not
string data = GetData("");




The syntax for defining a method in C# is 

<Access Specifier> <Return Type> <Method Name>(Parameter List)
{
   Method Body
}


字符串一个变量。您要问的是 var 关键字,其中数据类型是从上下文推断出来的。在你的第一个例子中,因为 GetData 返回一个 DataTable ,那么这就是编译器将创建的变量类型命名数据。您不能使用 string 作为类型的原因是因为它与从该方法返回的对象类型冲突。
A string is a variable. What you are asking about is the var keyword, where the data type is inferred from the context. In your first example since GetData returns a DataTable, then that is what the compiler will create as the type for the variable named data. The reason you cannot use string as the type is because it conflicts with the object type returned from that method.


var 声明一个变量,该变量从分配给它的初始值中获取类型,并被添加到支持Linq查询,它可以返回复杂的匿名类型。写的时候

var declares a variable that takes the type from the initial value assigned to it, and was added to support Linq enquires, which can return complex anonymous types. When you write
var data = GetData("");
...
private DataTable GetData(string query)



您正在有效地写作


You are effectively writing

DataTable data = GetData("");
...
private DataTable GetData(string query)



所以当你写的时候


So when you write

string data = GetData("");
...
private DataTable GetData(string query)



您特意告诉编译器将 DataTable 值分配给字符串变量,它(正确地)抱怨。



问题是使用var可以节省一些人不必考虑他们在做什么:所有他们必须做的是写 var myVariable = ... 并且编译器将它排除在外,而没有他们真正的想法是什么对象类型 myVariable 实际上是。这会导致像你这样的混乱,并且会使代码更难以理解和维护。



请使用 var 因为它的目的 - Linq - 而不是正常变量。输入几乎不需要额外的时间,它使您的代码更清晰。


You are specifically telling the compiler to assign a DataTable value to a string variable, and it (rightly) complains.

The problem is that using var saves some people from having to think about what they are doing: all they have to do is write var myVariable = ... and the compiler sorts it out without them having any real idea what object type myVariable actually is. This leads to confusion like yours and can make code a lot harder to understand and maintain.

Please, use var for it's intended purpose - Linq - and not for "normal" variables. It takes almost no extra time to type, and it makes your code a lot clearer.


这篇关于c#中变量和字符串之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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