Click here to Skip to main content
15,895,709 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
What is the difference between `variable` and `string` in `c#`

Why the `c#` is supporting only this
C#
var data = GetData("");

Why not this?
C#
string data = GetData("");

Or it will support both? Which one is better to use ? How it is implemented?
C#
  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;
            }


        }
    }


}
Posted
Updated 10-Dec-13 22:35pm
v2

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

C#
// 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
}
 
Share this answer
 
v2
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.
 
Share this answer
 
Comments
CPallini 11-Dec-13 5:06am    
5.Best answer, in my opinion.
Richard MacCutchan 11-Dec-13 5:20am    
Thanks; I was wondering if it was very clear?
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
C#
var data = GetData("");
...
private DataTable GetData(string query)

You are effectively writing
C#
DataTable data = GetData("");
...
private DataTable GetData(string query)

So when you write
C#
string data = GetData("");
...
private DataTable GetData(string query)

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.
 
Share this answer
 
Var is variant type hence at run time it can identify the correct type. Were as string is a concrete type and need to be explicitly converted from other type if required.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900