C# Variables

C# Code Snippets Variables
Share:

About

In this code snippet, we will learn how to use variables in C#.

A variable is a “container” that holds a value. The value can be any of the many data types in C#. To make a variable you must first declare its data type followed by the name you will use to reference that particular variable. Then you can assign it a value using the = assignment operator.

Let’s see how to declare and use variables in the code example below.

Code:

using System;

namespace Variables
{
    class Program
    {
        static void Main(string[] args)
        {
            //To declare a variable you first define the data type and then the name of the variable;
            int myVariable;
            //Use the = assignment operator to assign a value to your variable.
            myVariable = 10;

            //Declaration and assignment all at once.
            int myOtherVariable = 1;

            Console.WriteLine(myVariable);
            Console.WriteLine(myOtherVariable);

            Console.ReadLine();
        }
    }
}

Resulting output:

Share:

Leave a Reply

Your email address will not be published. Required fields are marked *

The following GDPR rules must be read and accepted:
This form collects your name, email and content so that we can keep track of the comments placed on the website. For more info check our privacy policy where you will get more info on where, how and why we store your data.

Advertisment ad adsense adlogger