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
- What is a Data Type?
- Why Use Data Types?
- Value Types
- Reference Types
- Common Data Types
- Example Program
- Best Practices
- 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.
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 Type | Description | Example |
|---|---|---|
| int | Whole numbers | 25 |
| double | Decimal numbers | 99.95 |
| float | Decimal (single precision) | 15.5f |
| decimal | High precision financial values | 2500.75m |
| char | Single character | 'A' |
| bool | True or False | true |
Reference Types
Reference types store a reference (address) to the actual object in memory.
| Type | Example |
|---|---|
| string | "Hello World" |
| array | int[] marks = {10,20}; |
| class | Employee emp = new Employee(); |
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
f for float and m for decimal values.Best Practices
- Choose the smallest suitable data type.
- Use
stringfor text. - Use
boolfor true/false values. - Use
decimalfor financial calculations. - Give variables meaningful names.
Summary
| Concept | Description |
|---|---|
| Value Type | Stores the actual value. |
| Reference Type | Stores a reference to an object. |
| int | Whole numbers. |
| string | Stores text. |
| bool | Stores true or false. |
Interview Questions
- What is a data type?
- What is the difference between value and reference types?
- When should you use decimal?
- 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#
Learn how to convert one data type into another using implicit conversion, explicit casting, the Convert class, and parsing methods.
Table of Contents
- What is Type Conversion?
- Implicit Conversion
- Explicit Conversion (Casting)
- Convert Class
- Parse and TryParse
- Best Practices
- 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.
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
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);
| Method | Description |
|---|---|
| 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
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 Type | When to Use |
|---|---|
| Implicit | Safe automatic conversion |
| Explicit | Manual conversion with possible data loss |
| Convert | General-purpose conversions |
| TryParse | Safe conversion from user input |
Interview Questions
- What is type conversion?
- What is the difference between implicit and explicit conversion?
- Why is TryParse better than Parse?
- 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