C# Tutorial

C# String

C# Array

C# Flow Control

C# Class and Object

C# Inheritance

C# Interface

C# Collection

C# Generic

C# File I/O

C# Delegate and Event

C# Exception

C# Process and Thread

C# ADO.NET Database Operations

C# Data Types

In C#, there are two main categories of data types: value types and reference types. In this tutorial, we'll cover the most commonly used data types in each category.

  • Value Types:

Value types store the data directly, and when you pass a value type variable to a method or assign it to another variable, a copy of the value is made. Value types are derived from the System.ValueType class.

a. Basic Numeric Types:

C# has several built-in numeric types for both integers and floating-point numbers.

  • Integers:

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

    • float: 32-bit single-precision floating-point number
    • double: 64-bit double-precision floating-point number
  • Other Numeric Types:

    • decimal: 128-bit high-precision decimal number, suitable for financial calculations

b. Character and Boolean Types:

  • char: Represents a single 16-bit Unicode character
  • bool: Represents a boolean value (true or false)
  • Reference Types:

Reference types store a reference to the memory location where the data is stored. When you pass a reference type variable to a method or assign it to another variable, the reference is copied, not the actual data.

a. String Type:

  • string: Represents a sequence of characters. Strings are immutable in C#.

b. Array Types:

  • Arrays are used to store multiple values of the same data type in a contiguous block of memory. You can create single-dimensional, multi-dimensional, and jagged arrays in C#.

c. Object Type:

  • object: The base class for all types in C#. All other types, including value types, inherit from the object class.

Here's a simple example demonstrating the use of various data types in C#:

using System;

namespace DataTypesTutorial
{
    class Program
    {
        static void Main(string[] args)
        {
            // Numeric Types
            int myInt = 42;
            double myDouble = 3.14;

            // Character and Boolean Types
            char myChar = 'A';
            bool myBool = true;

            // String Type
            string myString = "Hello, World!";

            // Array Types
            int[] myIntArray = new int[] { 1, 2, 3, 4, 5 };
            string[,] my2DStringArray = new string[,]
            {
                { "A1", "B1" },
                { "A2", "B2" }
            };

            // Object Type
            object myObject = new object();
}
  1. Primitive data types in C#:

    • Description: Primitive data types are the basic building blocks in C#. Examples include int, float, char, and bool.
    • Code:
      int integerNumber = 42;
      float floatingPointNumber = 3.14f;
      char character = 'A';
      bool isTrue = true;
      
  2. Numeric data types in C#:

    • Description: Numeric data types include integers (int, long), floating-point numbers (float, double), and more.
    • Code:
      int integerNumber = 42;
      long longNumber = 1234567890123456789L;
      float floatingPointNumber = 3.14f;
      double doubleNumber = 3.1415926535;
      
  3. String data type in C#:

    • Description: The string data type is used to represent sequences of characters.
    • Code:
      string text = "Hello, C#!";
      
  4. C# char data type:

    • Description: The char data type represents a single Unicode character.
    • Code:
      char singleCharacter = 'A';
      
  5. Boolean data type in C#:

    • Description: The bool data type represents Boolean values (true or false).
    • Code:
      bool isTrue = true;
      bool isFalse = false;
      
  6. Reference types in C#:

    • Description: Reference types in C# include classes, interfaces, arrays, and delegates. They store references to the actual data.
    • Code:
      class MyClass
      {
          // Class members
      }
      
      MyClass myObject = new MyClass();
      
  7. Data type conversion in C#:

    • Description: Converting data between different data types, either implicitly or explicitly.
    • Code:
      int intValue = 42;
      double doubleValue = intValue; // Implicit conversion
      
  8. User-defined data types in C#:

    • Description: Creating custom data types using classes and structures in C#.
    • Code:
      public class Person
      {
          public string Name { get; set; }
          public int Age { get; set; }
      }
      
      Person myPerson = new Person { Name = "John", Age = 30 };