Variables In C#

A variable is  a name given to a storage area that our programs can manipulate.

For example,

  1. string message = "Hello from C# Corner!";  

In this example, message is the name of the variable that stores the string data value "Hello from C# Corner!".

In C#, a variable is always defined with a datatype.

A variable can be declared and initialized later, or it can be declared and initialized at the same time.

So, we can change our  declaration from this 

  1. string message = "Hello from C# Corner!";  
 to this.

  1. string message;  
  2. message = "Hello from C# Corner!";  

You can also declare multiple variables at the same line. For example:

  1. string firstName, lastName;  

But, you need to be careful what you assign to your variable. Let us try to assign an int value to our string variable.

  1. string firstName, lastName = 10;  

We see that we get an error which says: "Integer cannot be implicitly converted into a string. Check out my previous article to learn about type conversions in C#.

Also, a value must be assigned to a variable before using it otherwise it will give compile time error.