C#

C# is a modern, object-oriented programming language developed by Microsoft. It is used for developing desktop applications, web applications, and games using the .NET framework. In this chapter, we will learn about variables, data types, operators, control statements, loops, functions, classes, and a final example.
Introduction to C#
C# programs are typically written in files with .cs extension. The entry point of a C# program is the Main method. C# is strongly typed and supports object-oriented concepts.
Variables and Data Types
C# supports various data types like int, float, double, char, string, and bool. Variables must be declared with a type before use.
int age = 25;<br>
double height = 5.8;<br>
char grade = 'A';<br>
string name = "Riya";<br>
bool isStudent = true;<br>
>Operators
C# supports arithmetic, relational, logical, assignment, and other operators for performing operations on variables.
Control Statements
Conditional statements like if, else if, else, and switch allow decision-making in programs.
Loops
C# supports for, while, do-while, and foreach loops for repetitive tasks.
Functions
Functions (methods) in C# are defined using return type, name, and parameters. void return type indicates no value is returned.
Classes and Objects
C# is object-oriented. Classes define objects with properties and methods. Objects are instances of classes, created using the new keyword.
Final Example — Collatz Sequence
This example demonstrates the Collatz sequence using C#. Save as Collatz.cs.
using System;<br>
class Collatz<br>
{<br>
static void Main()<br>
{<br>
Console.Write("Enter a number: ");<br>
int n = int.Parse(Console.ReadLine());<br>
while (n != 1)<br>
{<br>
Console.Write(n + " ");<br>
if (n % 2 == 0) n /= 2;<br>
else n = 3*n + 1;<br>
}<br>
Console.WriteLine("1");<br>
}<br>
}<br>
>MCQs
1. Which keyword is used to declare a variable in C#?
(a) var
(b) let
(c) int
(d) dim
► (c) int
2. Which namespace is commonly used for basic input/output?
(a) System.IO
(b) System.Console
(c) System
(d) System.Text
► (c) System
3. How do you print output to the console in C#?
(a) print()
(b) Console.WriteLine()
(c) echo()
(d) cout
► (b) Console.WriteLine()
4. Which operator is used for assignment in C#?
(a) ==
(b) =
(c) :=
(d) <-
► (b) =
5. Which of the following is a valid comment in C#?
(a) // comment
(b) # comment
(c) <!-- comment -->
(d) /* comment */
► (a) // comment
6. Which loop executes at least once?
(a) for
(b) while
(c) do-while
(d) foreach
► (c) do-while
7. How do you define a method with no return value in C#?
(a) void
(b) function
(c) method
(d) def
► (a) void
8. Which is the correct way to start the main method in C#?
(a) static void main()
(b) void Main() { }
(c) static void Main(string[] args)
(d) main()
► (c) static void Main(string[] args)
9. Which symbol ends a statement in C#?
(a) .
(b) ;
(c) :
(d) ,
► (b) ;
10. Which operator is used for equality comparison?
(a) =
(b) ==
(c) !=
(d) <>
► (b) ==
11. Which data type stores a single character in C#?
(a) string
(b) char
(c) int
(d) bool
► (b) char
12. Which type stores true or false values?
(a) int
(b) bool
(c) char
(d) float
► (b) bool
13. What is the result of 7 % 3?
(a) 1
(b) 2
(c) 0
(d) 3
► (b) 1
14. Which keyword defines a constant in C#?
(a) constant
(b) const
(c) final
(d) fixed
► (b) const
15. How do you terminate a program in C#?
(a) stop();
(b) Environment.Exit(0);
(c) return 1;
(d) quit();
► (b) Environment.Exit(0);
16. Which keyword is used for selection statements?
(a) switch
(b) case
(c) if
(d) break
► (a) switch
17. Which namespace is required for math functions like Math.Sqrt()?
(a) System.Math
(b) System
(c) System.MathF
(d) System.MathLib
► (b) System
18. Which loop is best when iteration count is known?
(a) while
(b) do-while
(c) for
(d) loop
► (c) for