Coding Schools


 
Python | C Sharp | Azure AI | HTML | JavaScript | CSS | SQL Server
OOPS in C#
C# Data Types
Boxing and Unboxing in C#
Garbage Collection in C#
C# - Conditional Statements
C# - Loops
Interfaces in C#
Generics in C#
Collections in C#
C# 8.0 new features
C# Singleton Design Pattern
C# Factory Design Pattern
LINQ in C#
C# - Program to Find Prime Numbers
C# - Fibonacci Sequence
C# - Factorial of a number
C# - Recursive methods
C# - Anonymous Methods
C# - String Methods
C# TDD - XUnit
C# TDD - NUnit
C# Multiton Design Pattern
C# Facade Design Pattern
C# Abstract Factory Design Pattern
C# Decorator Design Pattern
C# Composite Design Pattern

C# Data Types



C# is a type-safe language, which means that every variable and object must have a specific data type. Here are the main data types available in C#:

Value Types

Value types directly contain their data. They are stored on the stack.

  1. Primitive Data Types:

    • int: Integer type (e.g., int age = 30;)

    • float: Single-precision floating point type (e.g., float pi = 3.14f;)

    • double: Double-precision floating point type (e.g., double g = 9.81;)

    • bool: Boolean type (e.g., bool isTrue = true;)

    • char: Character type (e.g., char letter = 'A';)

  2. Integral Types:

    • byte: 8-bit unsigned integer (e.g., byte b = 255;)

    • sbyte: 8-bit signed integer (e.g., sbyte sb = -128;)

    • short: 16-bit signed integer (e.g., short s = -32768;)

    • ushort: 16-bit unsigned integer (e.g., ushort us = 65535;)

    • int: 32-bit signed integer (e.g., int i = 2147483647;)

    • uint: 32-bit unsigned integer (e.g., uint ui = 4294967295;)

    • long: 64-bit signed integer (e.g., long l = 9223372036854775807;)

    • ulong: 64-bit unsigned integer (e.g., ulong ul = 18446744073709551615;)

  3. Floating Point Types:

    • float: Single-precision 32-bit floating point (e.g., float f = -1.5e38f;)

    • double: Double-precision 64-bit floating point (e.g., double d = 1.7e308;)

  4. Decimal Type:

    • decimal: 128-bit precise decimal type (e.g., decimal dec = 3.14m;)

  5. Boolean Type:

    • bool: Boolean type (e.g., bool isHappy = true;)

  6. Character Type:

    • char: Character type (e.g., char ch = 'A';)

  7. Structs and Enum Types:

    • User-defined value types.

Reference Types

Reference types store references to their data (objects), which are stored on the heap.

  1. String Type:

    • string: Sequence of characters (e.g., string name = "Hello";)

  2. Object Type:

    • object: The base type for all other types (e.g., object obj = "Hello";)

  3. Array Types:

    • Arrays of any data type (e.g., int[] arr = new int[5];)

  4. Class Types:

    • User-defined reference types (e.g., defining a class Student).

  5. Interface Types:

    • User-defined contracts for classes (e.g., defining an interface IShape).

  6. Delegate Types:

    • References to methods (e.g., delegate void MyDelegate(int x);)

  7. Null Type:

    • null: Represents a null reference.

Example in C#:

csharp
using System;

public class Program
{
    public static void Main()
    {
        int age = 30;
        float pi = 3.14f;
        double gravity = 9.81;
        bool isTrue = true;
        char letter = 'A';
        string name = "Alice";
        
        Console.WriteLine(age);
        Console.WriteLine(pi);
        Console.WriteLine(gravity);
        Console.WriteLine(isTrue);
        Console.WriteLine(letter);
        Console.WriteLine(name);
    }
}

These are the core data types you'll work with in C#. Each serves different purposes and offers various levels of precision or specificity depending on your needs. If you have any more inquiries or need deeper context on any of these types, just let me know!




All rights reserved | Privacy Policy | Sitemap