C#

C# is a modern, object-oriented language developed by Microsoft. It is widely used for desktop, web, and game development with frameworks like .NET and Unity. C# syntax is influenced by C++ and Java but with features such as garbage collection, properties, events, and safe type checking. Programs are stored in .cs files and executed through the .NET runtime. This chapter introduces the basics: output, variables, conditions, loops, functions, and ends with a subtraction calculator program as the final example.
Hello world
The simplest program in C# uses the System namespace and the Console.WriteLine method to print text.
using System;
class Program {
static void Main() {
Console.WriteLine("Hello, world!");
}
}
Variables and types
C# supports primitive types like int, double, char, bool, and reference types like string. Variables must be declared with explicit types or var for inference.
int age = 20;
double height = 5.9;
char grade = 'A';
bool isStudent = true;
string name = "Alice";
Conditions
Use if and else for decisions. C# requires boolean expressions inside condition checks.
int score = 75;
if (score >= 50) {
Console.WriteLine("Pass");
} else {
Console.WriteLine("Fail");
}
Loops
C# supports for, while, and foreach loops. foreach is especially clear for collections.
for (int i = 1; i <= 3; i++) {
Console.WriteLine("Count: " + i);
}
Functions
Functions in C# are defined as methods inside classes. A return type must be declared, and static allows them to be called without creating an object.
static int Multiply(int a, int b) {
return a * b;
}
Final example — calculator for subtraction
The following program asks the user to enter two numbers, subtracts them, and prints the difference. Save the file as Main.cs and run it with dotnet run if inside a project, or compile with csc Main.cs.
Main.cs
using System;
class Program {
static void Main() {
double minuend, subtrahend, difference;
Console.Write("Enter minuend: ");
minuend = Convert.ToDouble(Console.ReadLine());
Console.Write("Enter subtrahend: ");
subtrahend = Convert.ToDouble(Console.ReadLine());
difference = minuend - subtrahend;
Console.WriteLine("Difference: " + difference);
}
}
MCQs
1. What is the correct file extension for C# source files?
(a) .cpp
(b) .c
(c) .cs
(d) .java
► (c) .cs
2. Which namespace contains the Console class in C#?
(a) System
(b) Console
(c) IO
(d) Main
► (a) System
3. Which method prints output to the screen in C#?
(a) print()
(b) printf()
(c) Console.WriteLine()
(d) echo()
► (c) Console.WriteLine()
4. Which keyword is used to define a class in C#?
(a) class
(b) object
(c) def
(d) structure
► (a) class
5. What is the entry point of a C# program?
(a) Main()
(b) Start()
(c) Run()
(d) Begin()
► (a) Main()
6. Which type is used for true/false values in C#?
(a) int
(b) bool
(c) char
(d) string
► (b) bool
7. What will the expression (10 > 5) evaluate to in C#?
(a) 10
(b) 5
(c) true
(d) false
► (c) true
8. Which loop is guaranteed to run at least once in C#?
(a) for
(b) while
(c) do...while
(d) foreach
► (c) do...while
9. Which operator is used for equality comparison in C#?
(a) =
(b) ==
(c) !=
(d) :=
► (b) ==
10. Which keyword declares a method that belongs to the class itself?
(a) static
(b) const
(c) void
(d) global
► (a) static
11. Which loop is best for iterating over elements in a collection?
(a) for
(b) while
(c) foreach
(d) do...while
► (c) foreach
12. Which keyword is used to exit a loop immediately in C#?
(a) quit
(b) stop
(c) break
(d) end
► (c) break
13. Which keyword is used to skip the rest of a loop and continue with the next iteration?
(a) skip
(b) continue
(c) break
(d) next
► (b) continue
14. Which access modifier makes members visible only inside the class?
(a) public
(b) private
(c) protected
(d) internal
► (b) private
15. What does Convert.ToDouble("5.5") return in C#?
(a) "5.5"
(b) 5.5
(c) 55
(d) error
► (b) 5.5
16. Which symbol is used to indicate a single-line comment in C#?
(a) #
(b) //
(c) /* */
(d) --
► (b) //
17. Which operator is used to create objects in C#?
(a) create
(b) new
(c) alloc
(d) make
► (b) new
18. In the subtraction calculator, which variable stores the result?
(a) total
(b) sum
(c) difference
(d) result
► (c) difference