C#.NET Tutorialprovides basic and advanced concepts of C# for beginners and professionals.

C#.Net Data Types, Variables, and Constants

Back to: C#.NET Tutorial

In C#, data types, variables, and constants are fundamental concepts that help you work with and manage data in your applications. Here's a breakdown of each:

Data Types

C# is a strongly-typed language, which means that every variable and constant has a specific data type. Data types in C# can be broadly categorized into:

1. Primitive Types

These are the basic types provided by C#:

  • Integer Types

    • byte: 8-bit unsigned integer, ranges from 0 to 255.
    • sbyte: 8-bit signed integer, ranges from -128 to 127.
    • short: 16-bit signed integer, ranges from -32,768 to 32,767.
    • ushort: 16-bit unsigned integer, ranges from 0 to 65,535.
    • int: 32-bit signed integer, ranges from -2,147,483,648 to 2,147,483,647.
    • uint: 32-bit unsigned integer, ranges from 0 to 4,294,967,295.
    • long: 64-bit signed integer, ranges from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807.
    • ulong: 64-bit unsigned integer, ranges from 0 to 18,446,744,073,709,551,615.
  • Floating-Point Types

    • float: 32-bit single-precision floating-point number, with a precision of about 7 decimal places.
    • double: 64-bit double-precision floating-point number, with a precision of about 15-16 decimal places.
  • Decimal Type

    • decimal: 128-bit precise decimal number, with a precision of 28-29 significant digits. Used for financial and monetary calculations.
  • Character Type

    • char: 16-bit Unicode character.
  • Boolean Type

    • bool: Represents a boolean value, true or false.

2. Non-Primitive Types

These include complex types like:

  • String

    • string: Represents a sequence of characters.
  • Object

    • object: The base type for all other types in C#. Can hold any data type.
  • Dynamic

    • dynamic: A type that bypasses compile-time type checking. Its type is determined at runtime.

Variables

Variables are used to store data that can be changed during the execution of a program. To declare a variable, you specify its type and name, and optionally, initialize it:

You can also use var for implicit typing, where the type is inferred from the value:

csharp code:
int age = 30; // Declaration with initialization string name; // Declaration without initialization name = "John"; // Initialization

Constants

Constants are immutable values that are known at compile time and cannot be changed at runtime. They are declared using the const keyword:

csharp code:
const int DaysInWeek = 7; const string Greeting = "Hello, World!";

Constants must be initialized at the time of declaration and their value cannot be modified afterwards.

Example

Here's a simple example that combines these elements:

csharp code:
using System;

class Program
{
    // Constant
    const double Pi = 3.14159;

    static void Main()
    {
        // Variable declarations and initializations
        int age = 25;
        string name = "Alice";
        bool isStudent = true;
        float height = 5.6f; // Note the 'f' suffix for float literals

        // Using the variables
        Console.WriteLine($"Name: {name}");
        Console.WriteLine($"Age: {age}");
        Console.WriteLine($"Is Student: {isStudent}");
        Console.WriteLine($"Height: {height} meters");
        Console.WriteLine($"Value of Pi: {Pi}");
    }
}
Scroll to Top