MSDN Documentation

Basic Concepts: Understanding Data Types

Understanding Data Types

Data types are fundamental to programming. They define the kind of values a variable can hold and the operations that can be performed on those values. In most programming languages, data types can be broadly categorized into primitive types and complex types.

Primitive Data Types

Primitive data types are the most basic types of data. They are built into the language and represent single values.

Numeric Types

These types represent numbers. They can be further divided into integers and floating-point numbers.

  • Integers: Whole numbers (e.g., 10, -5, 0). Examples include int, short, long, byte, each differing in their range of values.
  • Floating-Point Numbers: Numbers with decimal points (e.g., 3.14, -0.001). Examples include float and double, with double typically offering higher precision.

Boolean Type

Represents logical values, typically true or false. It's crucial for conditional statements and logic.

  • Example: bool or boolean

Character Type

Represents a single character (e.g., 'A', 'z', '$').

  • Example: char

Complex Data Types

Complex data types are built from primitive types or other complex types. They can represent more structured data.

String Type

Represents a sequence of characters (text). Strings are enclosed in quotes.

  • Example: "Hello, World!", "MSDN"

Arrays

An array is a collection of elements of the same data type, stored in contiguous memory locations. Elements are accessed using an index, typically starting from 0.

// Example of an integer array
int[] numbers = {10, 20, 30, 40, 50};
string[] names = {"Alice", "Bob", "Charlie"};

Objects/Structs/Records

These allow you to group related data under a single name, creating custom data structures. They can contain multiple fields of different data types.

// Example of a user object structure
struct User {
    int userId;
    string username;
    bool isActive;
}

User admin = { 1, "adminUser", true };

Enumerations (Enums)

Enums define a set of named constants, making code more readable and maintainable.

enum DayOfWeek {
    Sunday,
    Monday,
    Tuesday,
    Wednesday,
    Thursday,
    Friday,
    Saturday
}

DayOfWeek today = DayOfWeek.Wednesday;

Data Type Conversion (Casting)

Sometimes, you may need to convert a value from one data type to another. This is known as type casting. Casting can be implicit (done automatically by the compiler) or explicit (performed by the programmer).

Implicit Conversion: A safer conversion where data is not lost. For example, converting an int to a double.

Explicit Conversion: May result in data loss or errors if not handled carefully. For example, converting a double to an int truncates the decimal part.

double pi = 3.14159;
int truncatedPi = (int)pi; // Explicit cast, truncatedPi will be 3

Common Data Type Summary

Type Description Example
Integer (e.g., int) Whole numbers. 100, -42
Floating-Point (e.g., double) Numbers with decimal points. 3.14159, -2.718
Boolean (e.g., bool) Logical true or false. true, false
Character (e.g., char) A single character. 'A', '%'
String (e.g., string) A sequence of characters. "Hello", "123 Main St."
Array Ordered collection of same-typed elements. int[] arr = {1, 2, 3};