Signup/Sign In
LAST UPDATED: SEPTEMBER 2, 2019

C# Variables and Data Types

    In this article, we will learn about C# variables, various different datatypes in C# with code examples to understand how we can declare variables of different data types in C# programming language.

    C# variables and datatypes with examples

    What is Variable?

    In programming, variables are used to hold or store information(data) to be referenced and manipulated in a computer program. They provide a way of labeling data with a descriptive name, so our programs can be understood more precisely by the reader and ourselves.

    Their primary purpose is to label and store data in memory and then, you can use the data throughout your program whenever necessary using the variable name.


    Rules for defining variables:

    1. Variable name must start with either an alphabet or an underscore followed by alphabets, digits, underscore etc.
      Valid set of variable names: {addition, _addition, add_result, addition1, addition_, etc.}
      Invalid set of variable names: {1addition, @addition, #addition, add ition, etc.}

    2. White space is not allowed in between the variable name.

    3. Variable name must not be reserved word or keyword in C# programming language (for example: int, double, etc.).

    Important Note: A variable name should be descriptive and understandable to another reader. Sometimes you yourself becomes the reader when you revisit a program that you wrote months or even years earlier.




    What is the data type?

    A data type is a data storage format that can contain a specific type or range of values. Unlike humans, a computer does not know the difference between 15892 and "Studytonight", in such cases, data type specifies the type of data that a variable can store such as integer, floating, string, etc, so that it becomes easier for the machine/programming language to understand what type of data to expect.

    In simpler words, data type specifies what type of data can be stored in a variable, for example, integer type, or maybe characters, etc.

    Data types in C# are broadly classified into the following types:

    1. Value Data Type: These are the data types which are used to store simple values. It can be integer value, float value, decimal value, simple character, boolean value etc. These are all predefined types, while there are some user-defined types as well, like Structures, Enumerations, etc.

    2. Reference Data Type

    3. Pointer Data Type

    C sharp data types




    1. Value Data Type

    The value types directly contain data. They are derived from the class System.ValueType. Some examples are int, char, and float, which stores numbers, alphabets, and floating-point numbers, respectively. When you declare an int type, the system allocates memory to store the valueof integer type. To get the size of a value data type, refer to the following program.

    Filename: Program.cs (Program to print size of value data types using the sizeof operator)

    using System;
    namespace Studytonight
    {
        public class Program
        {
            public static void Main(string[] args)
            {
                Console.WriteLine("Size of int | {0}", sizeof(int));
                Console.WriteLine("Size of float | {0}", sizeof(float));
                Console.WriteLine("Size of double | {0}", sizeof(double));
                Console.WriteLine("Size of char | {0}", sizeof(char));
                Console.WriteLine("Size of bool | {0}", sizeof(bool));
                Console.WriteLine("Size of byte | {0}", sizeof(byte));
                Console.WriteLine("Size of short | {0}", sizeof(short));
                Console.WriteLine("Size of long | {0}", sizeof(long));
                Console.WriteLine("Size of decimal | {0}", sizeof(decimal));
                Console.WriteLine("Size of sbyte | {0}", sizeof(sbyte));
                Console.WriteLine("Size of uint | {0}", sizeof(uint));
                Console.WriteLine("Size of ulong | {0}", sizeof(ulong));
                Console.WriteLine("Size of ushort | {0}", sizeof(ushort));
                Console.ReadKey();
            }
        }
    }

    Output:

    Size of int | 4
    Size of float | 4
    Size of double | 8
    Size of char | 2
    Size of bool | 1
    Size of byte | 1
    Size of short | 2
    Size of long | 8
    Size of decimal | 16
    Size of sbyte | 1
    Size of uint | 4
    Size of ulong | 8
    Size of ushort | 2



    2. Reference Data Type

    The reference types do not contain the actual data stored in a variable, but they contain a reference to the variables (memory location). If two variable of reference type, stores the same memory location, then if the value of one variable is changed then it reflects in another reference variable also which is storing the same memory location reference. Example of built-in reference types: string, object, etc.

    Reference type datatypes are also user-defined, like Class, Interfaces etc.

    Note: in our upcoming chapters, while studying about functions, we will see the examples for both calls by value and call by reference.




    3. Pointer Data Type

    These type of variables store the memory address of another type. Pointers in C# have the same capabilities as the pointers in C or C++.

    For example:

    int* ptr; //means pointer to int



    Declaring Variables of different Datatypes

    Now that we know what are variables and datatypes, let's see a few code examples where we will be declaring variables of different datatypes:

    int a;    // declaring one variable of type int
    
    float x,y;    // declaring two variable of float type
    
    int num = 2;    // defining one variable of type int and assigning value to it
    
    char myChar = 'A'    // defining a character type variable



    Conclusion

    With this, now you know what are different data types in C# programming language and how to define variables of different data types along with code examples. In the next tutorial, we will learn about Operators and Reserved Keywords in C#.

    Subject Matter Expert of C# Programming at Studytonight.
    IF YOU LIKE IT, THEN SHARE IT
    Advertisement

    RELATED POSTS