Cross_Column

Wednesday, 1 July 2026

Data Type and Type Conversion in C#



Data Types in C# - Beginner Tutorial

Data Types in C#

Learn what data types are, why they matter, and how to choose the correct data type with practical examples.

Table of Contents

  1. What is a Data Type?
  2. Why Use Data Types?
  3. Value Types
  4. Reference Types
  5. Common Data Types
  6. Example Program
  7. Best Practices
  8. FAQ

What is a Data Type?

A data type tells the C# compiler what kind of value a variable can store. It also determines how much memory is required and what operations are allowed on that value.

Note: Every variable in C# must have a data type.

Why Use Data Types?

  • Store data efficiently.
  • Improve program performance.
  • Prevent invalid values.
  • Help the compiler detect errors early.

Value Types

Value types store the actual value directly in memory.

Data TypeDescriptionExample
intWhole numbers25
doubleDecimal numbers99.95
floatDecimal (single precision)15.5f
decimalHigh precision financial values2500.75m
charSingle character'A'
boolTrue or Falsetrue

Reference Types

Reference types store a reference (address) to the actual object in memory.

TypeExample
string"Hello World"
arrayint[] marks = {10,20};
classEmployee emp = new Employee();
Tip: Use decimal for money calculations because it provides better precision than float or double.

Example Program

using System;

class Program
{
    static void Main()
    {
        int age = 30;
        double height = 5.9;
        char grade = 'A';
        bool isStudent = false;
        string name = "John";

        Console.WriteLine(name);
        Console.WriteLine(age);
        Console.WriteLine(height);
        Console.WriteLine(grade);
        Console.WriteLine(isStudent);
    }
}

Output

John
30
5.9
A
False
Common Mistake: Don't forget suffixes like f for float and m for decimal values.

Best Practices

  • Choose the smallest suitable data type.
  • Use string for text.
  • Use bool for true/false values.
  • Use decimal for financial calculations.
  • Give variables meaningful names.

Summary

ConceptDescription
Value TypeStores the actual value.
Reference TypeStores a reference to an object.
intWhole numbers.
stringStores text.
boolStores true or false.

Interview Questions

  1. What is a data type?
  2. What is the difference between value and reference types?
  3. When should you use decimal?
  4. What data type stores a single character?

Practice Exercise

  • Create variables using int, double, bool, char and string.
  • Print all values using Console.WriteLine().
  • Create a decimal variable for salary.

FAQ

Can I change a variable's data type later?
No. A variable's declared data type remains the same.

Which data type should I use for names?
Use string.


Next Tutorial: Operators in C#

Type Conversion in C# - Beginner Tutorial

Type Conversion in C#

Learn how to convert one data type into another using implicit conversion, explicit casting, the Convert class, and parsing methods.

Table of Contents

  1. What is Type Conversion?
  2. Implicit Conversion
  3. Explicit Conversion (Casting)
  4. Convert Class
  5. Parse and TryParse
  6. Best Practices
  7. FAQ

What is Type Conversion?

Type conversion is the process of converting a value from one data type to another. It is useful when different types need to work together in a program.

Note: Type conversion is also known as type casting.

1. Implicit Conversion

Implicit conversion happens automatically when there is no risk of data loss.

int number = 100;
double result = number;

Console.WriteLine(result);   // 100

2. Explicit Conversion (Casting)

Explicit conversion must be done manually when data loss is possible.

double price = 99.99;
int amount = (int)price;

Console.WriteLine(amount);   // 99
Warning: Explicit casting may remove the decimal part or lose data.

3. Convert Class

The Convert class provides safe methods to convert between many common data types.

string age = "25";
int number = Convert.ToInt32(age);

Console.WriteLine(number);
MethodDescription
Convert.ToInt32()Convert to int
Convert.ToDouble()Convert to double
Convert.ToBoolean()Convert to bool
Convert.ToString()Convert to string

4. Parse and TryParse

Parse() converts a valid string into another type. TryParse() avoids exceptions by returning true or false.

string value = "500";
int marks = int.Parse(value);

string input = "ABC";
bool ok = int.TryParse(input, out int result);

Console.WriteLine(ok);   // False
Pro Tip: Prefer TryParse() when accepting user input because it prevents runtime exceptions.

Complete Example

using System;

class Program
{
    static void Main()
    {
        int age = 30;
        double salary = age;

        double score = 95.8;
        int finalScore = (int)score;

        string number = "150";
        int value = Convert.ToInt32(number);

        Console.WriteLine(salary);
        Console.WriteLine(finalScore);
        Console.WriteLine(value);
    }
}

Summary

Conversion TypeWhen to Use
ImplicitSafe automatic conversion
ExplicitManual conversion with possible data loss
ConvertGeneral-purpose conversions
TryParseSafe conversion from user input

Interview Questions

  1. What is type conversion?
  2. What is the difference between implicit and explicit conversion?
  3. Why is TryParse better than Parse?
  4. When should you use the Convert class?

Practice Exercise

  • Convert an int to double.
  • Convert a double to int using casting.
  • Convert a string to int using Convert.ToInt32().
  • Validate user input using TryParse().

FAQ

Can implicit conversion lose data?
No. It only happens when the conversion is safe.

Which is safer, Parse or TryParse?
TryParse is safer because it does not throw an exception for invalid input.


Next Tutorial: Operators in C#

No comments:

Post a Comment

Few More

Recursion in C#

Recursion in C# | Beginner Tutorial Recursion in C# Learn how recursion works in C#, understand the base case and re...