Data Types

Lesson 5
Author : Afrixi
Last Updated : February, 2023
C# - Programming Language
This course covers the basics of programming in C#.

In programming, a data type is a classification of data that determines what type of operations can be performed on that data. In C#, there are several built-in data types, including:

int: integers (whole numbers)

double: floating-point numbers (decimal numbers)

bool: Boolean values (true or false)

char: single characters (such as ‘a’ or ‘7’)

string: a sequence of characters

Here are some examples of using these data types in C#:


int num1 = 10;
double num2 = 3.14;
bool isTrue = true;
char letter = 'a';
string name = "John";

In this example, we’ve declared variables of different data types and assigned them values. The int variable num1 stores the value 10, the double variable num2 stores the value 3.14, the bool variable isTrue stores the value true, the char variable letter stores the character 'a', and the string variable name stores the string "John".

We can also perform operations on variables of different data types. For example, we can add two int variables together:


int num1 = 10;
int num2 = 5;
int sum = num1 + num2;

In this example, we’ve declared two int variables, num1 and num2, and assigned them the values 10 and 5, respectively. We’re then using the + operator to add them together and assign the result to the int variable sum.

We can also convert data types using type casting. For example, we can convert an int to a double using the double keyword:


int num1 = 10;
double num2 = (double)num1;

In this example, we’re using type casting to convert the int variable num1 to a double variable num2. We’re using the (double) syntax to specify that we want to convert num1 to a double data type.

Here’s an example of how to declare and use these data types in C#:

using System;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            // Integer types
            byte b = 10;
            short s = 20;
            int i = 30;
            long l = 40L;

            // Floating-point types
            float f = 3.14f;
            double d = 3.141592653589793;

            // Boolean type
            bool isTrue = true;
            bool isFalse = false;

            // Character type
            char c = 'A';

            // String type
            string str = "Hello, world!";

            // Output values to console
            Console.WriteLine(b);
            Console.WriteLine(s);
            Console.WriteLine(i);
            Console.WriteLine(l);
            Console.WriteLine(f);
            Console.WriteLine(d);
            Console.WriteLine(isTrue);
            Console.WriteLine(isFalse);
            Console.WriteLine(c);
            Console.WriteLine(str);

            Console.ReadKey();
        }
    }
}

In this example, we’re declaring variables of each data type and initializing them with different values. We’re then outputting the values to the console using the Console.WriteLine() method.

Note that when declaring a long variable, we need to append an “L” to the end of the value to indicate that it’s a long. Similarly, when declaring a float variable, we need to append an “f” to the end of the value to indicate that it’s a float.