What are the Data Types in C# ? with Free Notes

What are the Data Types in C# ???

Data Types in C# define the type of data that can be stored in a variable. They determine the size and layout of the variable’s memory, the range of values that can be stored, and set of operations that can be performed on the variable. C# data types are divided into two main categories, value types and reference type.

Value types

Value types hold data directly. Examples include:

  • Integral types: int, byte, short, long, uint, ulong, sbyte
  • Floating-point types: float, double
  • Decimal type: decimal
  • Boolean type: bool
  • Character type: char
  • Structures: struct
  • Enumerations: enum

Example – Data Types in C#

Here’s an example program that demonstrates the use of different Data Types in C#

using System;

namespace DataTypesExample
{
    class Program
    {
        static void Main(string[] args)
        {
            int age = 30;                 // Integer type
            double height = 5.9;          // Double type
            string name = "John";         // String type
            bool isActive = true;         // Boolean type
            char grade = 'A';             // Char type
            float weight = 70.5f;         // Float type
            decimal salary = 50000.75m;   // Decimal type

            Console.WriteLine("Name: " + name);
            Console.WriteLine("Age: " + age);
            Console.WriteLine("Height: " + height);
            Console.WriteLine("Active: " + isActive);
            Console.WriteLine("Grade: " + grade);
            Console.WriteLine("Weight: " + weight);
            Console.WriteLine("Salary: " + salary);
        }
    }
}

Line-by-Line Notes

static void Main(string[] args)
  • The Main method is the entry point of the program. It is static because it is called by the runtime without creating an instance of the class. void indicates that the method does not return any value. string[] args is an array of strings that can hold command-line arguments.
int age = 30;
  • Declares an integer variable named age and initializes it with the value 30.
  • int: Data type for a 32-bit signed integer.
  • age: Name of the variable.
  • 30: Initial value assigned to age.
double height = 5.9;
  • Declares a double-precision floating-point variable named height and initializes it with the value 5.9.
  • double: Data type for double-precision floating-point numbers.
  • height: Name of the variable.
  • 5.9: Initial value assigned to height.
string name = "John";
  • Declares a string variable named name and initializes it with the value "John".
  • string: Data type for a sequence of characters.
  • name: Name of the variable.
  • "John": Initial value assigned to name.
bool isActive = true;
  • Declares a boolean variable named isActive and initializes it with the value true.
  • bool: Data type for boolean values (true or false).
  • isActive: Name of the variable.
  • true: Initial value assigned to isActive.
char grade = 'A';
  • Declares a character variable named grade and initializes it with the value 'A'.
  • char: Data type for a single 16-bit Unicode character.
  • grade: Name of the variable.
  • 'A': Initial value assigned to grade.
float weight = 70.5f;
  • Declares a single-precision floating-point variable named weight and initializes it with the value 70.5.
  • float: Data type for single-precision floating-point numbers. The f suffix indicates a float literal.
  • weight: Name of the variable.
  • 70.5f: Initial value assigned to weight.
decimal salary = 50000.75m;
  • Declares a decimal variable named salary and initializes it with the value 50000.75.
  • decimal: Data type for financial and monetary calculations. The m suffix indicates a decimal literal.
  • salary: Name of the variable.
  • 50000.75m: Initial value assigned to salary.

Leave a Comment

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

Scroll to Top