C# Programming Examples Of Variables And Data Types
Hey there! Today, we’re going to play around with variables and data types in C#. Think of variables as little storage boxes where you keep different kinds of data—numbers, text, or even true/false values. And data types? Well, they tell C# what kind of data can go into those boxes.
We’ll keep things super simple with easy-to-follow examples. You’ll see how to declare variables, store values, and even convert between different types.
No boring theory
Just code, Quick explanations, and Fun!
Value Type Example
1. Declare and Print a Variable
Write a C# program to declare a variable name
of type string
and assign your name to it. Print the value.
- Use
string
to store text. - Use
Console.WriteLine()
to print.
using System;
class Program
{
static void Main()
{
string name = "Steven";
Console.WriteLine("My name is " + name);
}
}
Output
My name is Steven
Explanation:
- Declared a
string
variablename
and stored"Steven"
. - Used
Console.WriteLine()
to print it.
2. Add Two Integers
Question:
Write a program to declare two integers and print their sum.
Hint:
- Use
int
for whole numbers. - Use
+
for addition.
Solution:
using System;
class Program
{
static void Main()
{
int num1 = 10;
int num2 = 20;
int sum = num1 + num2;
Console.WriteLine("Sum: " + sum);
}
}
Output
Sum: 30
Explanation:
- Declared two
int
variables and assigned values. - Added them and stored in
sum
. - Printed the result.
3. Boolean Variable Example
Question:
Declare a bool
variable and print its value.
Hint:
- Use
true
orfalse
.
Solution:
using System;
class Program
{
static void Main()
{
bool isStudent = true;
Console.WriteLine("Am I a student? " + isStudent);
}
}
Output
Am I a student? True
Explanation:
- Declared
bool
and assignedtrue
.
4. Convert int to double
Question:
Declare an int
, convert it to double
, and print both.
Hint:
- Use
(double)
to convert anint
todouble
.
Solution:
using System;
class Program
{
static void Main()
{
int number = 5;
double converted = (double)number;
Console.WriteLine("Integer: " + number);
Console.WriteLine("Double: " + converted);
}
}
Output
Integer: 5
Double: 5
Explanation:
- Declared an
int
variable. - Used
(double)
to convert it. - Printed both values.
5. Convert string to int
Question:
Take a number as a string, convert it to an integer, and print.
Hint:
- Use
int.Parse()
to convert.
Solution:
using System;
class Program
{
static void Main()
{
string strNumber = "100";
int number = int.Parse(strNumber);
Console.WriteLine("Converted Number: " + number);
}
}
Output
Converted Number: 100
Explanation:
- Stored
"100"
asstring
. - Used
int.Parse()
to convert it. - Printed the converted value.
6. Explicit Type Conversion (double to int)
Question:
Convert a double
to an int
and print both.
Hint:
- Use
(int)
for explicit conversion.
Solution:
using System;
class Program
{
static void Main()
{
double num = 9.8;
int intNum = (int)num;
Console.WriteLine("Double: " + num);
Console.WriteLine("Integer: " + intNum);
}
}
Output
Double: 9.8
Integer: 9
(Note: The fractional part .8
is truncated during explicit type casting from double
to int
.)
Explanation:
- Stored
"100"
asstring
. - Used
int.Parse()
to convert it. - Printed the converted value.
Reference Data Type Example
7. String Example (Reference Behavior)
Question:
Write a program to declare two string
variables. Assign one to another and modify one of them. Observe what happens when you print both.
Hint:
- Strings are immutable (they don’t change in memory).
- Modifying one won’t affect the other.
Solution:
using System;
class Program
{
static void Main()
{
string firstName = "John";
string secondName = firstName; // Copy reference
secondName = "David"; // Modify secondName
Console.WriteLine("First Name: " + firstName);
Console.WriteLine("Second Name: " + secondName);
}
}
Explanation:
- Assigned
"John"
tofirstName
. - Assigned
firstName
tosecondName
. - Changed
secondName
to"David"
, butfirstName
remains"John"
. - This proves that string variables don’t share memory locations (immutable behavior).
Output
First Name: John
Second Name: David
Explanation:
- Even though we assigned
firstName
tosecondName
, strings are immutable. - Changing
secondName
to"David"
does not affectfirstName
.
8. Class Example (Reference Sharing Behavior)
Question:
Create a Person
class with a name
field. Assign one object to another and modify one. Observe how both change.
Hint:
- Classes are reference types.
- Changing one object affects the other.
Solution:
using System;
class Person
{
public string name;
}
class Program
{
static void Main()
{
Person person1 = new Person();
person1.name = "Alice";
Person person2 = person1; // Both refer to the same memory
person2.name = "Bob"; // Modify person2
Console.WriteLine("Person1 Name: " + person1.name);
Console.WriteLine("Person2 Name: " + person2.name);
}
}
Explanation:
- Created a
Person
class with aname
field. person1
is assigned toperson2
(both now refer to the same object).- Changing
person2.name
also affectsperson1.name
. - This proves objects share the same memory when assigned.
Output
Person1 Name: Bob
Person2 Name: Bob
Explanation:
person1
andperson2
refer to the same object in memory.- Changing
person2.name = "Bob"
also changesperson1.name
because both point to the same instance.
9.Array Example (Reference Type Behavior)
Question:
Create an integer array. Assign one array to another and modify one of the elements. Observe how both arrays are affected.
Hint:
- Arrays are reference types.
- Changing one affects the other.
Solution:
using System;
class Program
{
static void Main()
{
int[] numbers1 = { 1, 2, 3 };
int[] numbers2 = numbers1; // Both refer to the same memory
numbers2[0] = 99; // Modify numbers2
Console.WriteLine("numbers1[0]: " + numbers1[0]);
Console.WriteLine("numbers2[0]: " + numbers2[0]);
}
}
Explanation:
- Declared an integer array
numbers1
. - Assigned
numbers1
tonumbers2
, making them share the same reference. - Modified
numbers2[0] = 99
, which also affectednumbers1[0]
. - This proves arrays work as reference types.
Output
numbers1[0]: 99
numbers2[0]: 99
Explanation:
- Arrays are reference types, so both
numbers1
andnumbers2
point to the same array in memory. - Changing
numbers2[0] = 99
also affectsnumbers1[0].
10. Object Example (Reference Behavior in Action)
Question:
Create a Car
class with a model
field. Create an object, assign it to another, and modify one. Observe how both objects change.
Hint:
- Objects are reference types.
- Modifying one affects the other.
Solution:
using System;
class Car
{
public string model;
}
class Program
{
static void Main()
{
Car car1 = new Car();
car1.model = "Tesla";
Car car2 = car1; // Both refer to the same memory
car2.model = "Ford"; // Modify car2
Console.WriteLine("Car1 Model: " + car1.model);
Console.WriteLine("Car2 Model: " + car2.model);
}
}
Explanation:
Explanation:
- Created a
Car
class with amodel
field. - Assigned
car1
tocar2
, making them share the same memory reference. - Modified
car2.model
, which also changedcar1.model
. - This confirms objects are reference types.
Output
Car1 Model: Ford
Car2 Model: Ford
Explanation:
car1
andcar2
share the same memory reference.- Modifying
car2.model = "Ford"
also changescar1.model
.
Conclusion
Great job! 🎉 You just explored value types and reference types in C# with real examples. Now, you understand how variables, data types, and type conversion work—and how reference types behave differently from value types.
Here’s a quick recap:
✅ Value types (int, double, bool) store data directly.
✅ Reference types (string, class, array, object) store a memory reference instead of actual data.
✅ Strings are immutable, so modifying one doesn’t affect its copy.
✅ Classes, arrays, and objects share references, so changing one affects all.
The best way to master this? Experiment! Try modifying values, changing references, and observing the results. Keep coding, keep learning, and soon, you’ll be writing powerful C# programs like a pro! 💻