Data types and variables in C# programming



Definition of Data Types

A variable holds data of a specific type. When you declare a variable to store data in an application, you need to choose an appropriate data type for that data. Visual C# is a type-safe language, which means that the compiler guarantees that values stored in variables are always of the appropriate type.

Commonly Used Data Types

The following table shows the commonly used data types in Visual C#, and their characteristics.

Type Description Size (bytes) Range
int Whole numbers 4 –2,147,483,648 to 2,147,483,647
long Whole numbers (bigger range) 8 –9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
float Floating-point numbers 4 +/–3.4 × 10^38
double Double precision (more accurate) floating-point numbers 8 +/–1.7 × 10^308
decimal Monetary values 16 28 significant figures
char Single character 2 N/A
bool Boolean 1 True or false
DateTime Moments in time 8 0:00:00 on 01/01/2001 to 23:59:59 on 12/31/9999
string Sequence of characters 2 per character N/A

Declaring and Assigning Variables

Before you can use a variable, you must declare it so that you can specify its name and characteristics. The name of a variable is referred to as an identifier. Visual C# has specific rules concerning the identifiers that you can use:

  • An identifier can only contain letters, digits, and underscore characters.
  • An identifier must start with a letter or an underscore.
  • An identifier for a variable should not be one of the keywords that Visual C# reserves for its own use.

Visual C# is case sensitive. If you use the name MyData as the identifier of a variable, this is not the same as myData . You can declare two variables at the same time called MyData and myData and Visual C# will not confuse them, although this is not good coding practice.

You can declare multiple variables in a single declaration by using the comma separator; all variables declared in this way have the same type.

Declaring a Variable:


        // DataType variableName;
    int price;
    
        // OR
    // DataType variableName1, variableName2;
    int price, tax;

After you declare a variable, you can assign a value to it by using an assignment statement. You can change the value in a variable as many times as you want during the running of the application. The assignment operator = assigns a value to a variable.

Assigning a Variable, Declaring and Assigning:


        //Declaring variableName = value;
    price = 10;
        //declaring and assigning variables
    int price = 10;

Implicitly Typed Variables

When you declare variables, you can also use the var keyword instead of specifying an explicit data type such as int or string. When the compiler sees the var keyword, it uses the value that is assigned to the variable to determine the type.

Declaring a Variable by Using the var Keyword:


    var price = 20;

In this example, the price variable is an implicitly typed variable. However, the var keyword does not mean that you can later assign a value of a different type to price. The type of price is fixed, in much the same way as if you had explicitly declared it to be an integer variable.

Implicitly typed variables are useful when you do not know, or it is difficult to establish explicitly, the type of an expression that you want to assign to a variable.

Object Variables

When you declare an object variable, it is initially unassigned. To use an object variable, you must create an instance of the corresponding class, by using the new operator, and assign it to the object variable.

The new Operator:


    ServiceConfiguration config = new ServiceConfiguration();

The new operator does two things:

  • It causes the CLR to allocate memory for your object
  • It then invokes a constructor to initialize the fields in that object.

The version of the constructor that runs depends on the parameters that you specify for the new operator.

Example of Variables

The program below show the use of int, var and string variables:


    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    namespace Variables
    {
        class Program
        {
        static void Main(string[] args)
        {
        //declaring and assigning using var
            var a = "C# Programming";
            var b = " Tutorials";
        //declaring and assigning string
            string myFirstName;
            string myLastName;
            myFirstName = "Info";
            myLastName = "codify";
            
        //declaring and assigning using int and string
            int x = 1;
            string y = "4";
        //converting int to string
            string myFirstTry = x.ToString() + y;
            
        //converting string to int
            int mySecondTry = x + int.Parse(y);
            Console.WriteLine(a + b);
            Console.WriteLine(myFirstName + myLastName);
            Console.WriteLine(myFirstTry + " is Admin BM Date of Birth");
            }
        }
    }

    Output:
    C# Programming Tutorials
    infocodify
    14 is Admin BM Date of Birth

Ads Right