What is a variable in c#

variable in c#


In the first c# program chapter, we declared a variable called "message" as shown below

For Example variable C#:-

  1. namespace CSharpTutorials
    {
        class Program
        {
            static void Main(string[] args)
            {
                string message = "Hello World!!";
    
                Console.WriteLine(message);
            }
        }
    }

The variable in C# is nothing but a name given to a data value. In the above example, message is the name of the variable that stores the string data value "Hello World!!". As the name suggests, the contents of a variable can vary, i.e., you can change the value of a variable at any time.
In C#, a variable is always defined with a data type. The following is the syntax variable declaration and initialization.
variable syntax :
  1. <data type> <variable name>;
    
    <datatype> <variable name> = <value>;

A variable can be declared and initialized later or it can be declared and initialized at the same time. In the following example, the first statement declares a variable called "message" without assigning any value to it. In the second statement, a value is assigned to the "message" variable.

  1. string message;
    
    // value can be assigned after it declared 
    message = "Hello World!!";

In the following example, variable is declared and initialized (a value is assigned to it) at the same time.

Example: Variable Declaration & Initialization
  1. string message = "Hello World!!";




















Post a Comment

0 Comments