Industrial manufacturing
Industrial Internet of Things | Industrial materials | Equipment Maintenance and Repair | Industrial programming |
home  MfgRobots >> Industrial manufacturing >  >> Industrial programming >> C Language

C# Data Types Explained: Int, Double, Bool, String & More

What are Data Types in C#?

C# offers a set of fundamental data types that form the backbone of any application. These types define the kind of values that variables can hold and determine how the runtime handles them. Below we’ll walk through the most common types—int, double, bool, and string—with clear, executable examples.

1) Integer (int)

The int (Int32) type stores whole numbers ranging from –2,147,483,648 to 2,147,483,647. It’s the most frequently used numeric type for counting, indexing, and arithmetic operations.

C# Data Types Explained: Int, Double, Bool, String & More

using System;

namespace DemoApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            int num = 30;
            Console.Write(num);
            Console.ReadKey();
        }
    }
}

Explanation

  1. Declare an int variable num and assign it the value 30.
  2. Print num to the console.

Running the program produces the output:

C# Data Types Explained: Int, Double, Bool, String & More

2) Double (double)

The double type represents a double‑precision floating‑point number, suitable for high‑precision calculations such as scientific data.

C# Data Types Explained: Int, Double, Bool, String & More

using System;

namespace DemoApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            double num = 30.33;
            Console.Write(num);
            Console.ReadKey();
        }
    }
}

Explanation

  1. Declare a double variable num and assign it 30.33.
  2. Display num on the console.

The console shows:

C# Data Types Explained: Int, Double, Bool, String & More

3) Boolean (bool)

Booleans hold the logical values true or false and are essential for control flow, conditionals, and flags.

C# Data Types Explained: Int, Double, Bool, String & More

using System;

namespace DemoApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            bool status = true;
            Console.Write(status);
            Console.ReadKey();
        }
    }
}

Explanation

  1. Define a bool variable status and set it to true.
  2. Print the boolean value.

Output:

C# Data Types Explained: Int, Double, Bool, String & More

4) String (string)

Strings store sequences of Unicode characters and are indispensable for text processing, user interaction, and data serialization.

C# Data Types Explained: Int, Double, Bool, String & More

using System;

namespace DemoApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            string message = "Hello";
            Console.Write(message);
            Console.ReadKey();
        }
    }
}

Explanation

  1. Declare a string variable message and initialize it with "Hello".
  2. Output the string to the console.

Result:

C# Data Types Explained: Int, Double, Bool, String & More

These examples illustrate the core data types that every C# developer should master. By understanding the semantics and appropriate use cases for each type, you can write cleaner, safer, and more performant code.

C Language

  1. Mastering C# Using Statements: Imports, Aliases, and Static Directives
  2. C Data Types Explained: int, float, char, and More – A Complete Guide
  3. C++ Variables and Data Types: Expert Guide to int, double, char, string, bool
  4. C++ Operators Explained: Types, Examples, and Sample Programs
  5. Mastering C++ Char Data Types: Declaration, ASCII, and Conversion Techniques
  6. C Typecasting: Mastering Implicit and Explicit Conversions with Code Examples
  7. Mastering C’s free() Function: Practical Guide & Example
  8. Java Variables and Data Types – A Comprehensive Guide with Examples
  9. Java Inheritance Explained: Types, Syntax, and Practical Examples
  10. Integrating Senseye with ThingSpeak: A Quick Start Guide