C# (pronounced "C-sharp") is a modern, object-oriented programming language developed by Microsoft as part of the .NET framework. It is widely used for developing a range of applications, including web, desktop, mobile, and game development. C# is a versatile and powerful language designed to be easy to learn and use, with a focus on simplicity and efficiency.
Here’s an introduction to the fundamentals of C#:
C# is a case-sensitive language, which means that Variable and variable are treated as different identifiers. The basic structure of a C# program consists of:
Example of a simple C# program:
using System;
class Program
{
static void Main()
{
Console.WriteLine("Hello, World!");
}
}
In this example:
using System;: This includes the System namespace, which contains useful classes like Console for input/output operations.class Program: Defines a class named Program.static void Main(): The entry point of the C# program. When you run a C# application, this is where the program starts.Console.WriteLine("Hello, World!");: Prints the string "Hello, World!" to the console.Variables are used to store data, and each variable must have a data type that defines the kind of data it can store. In C#, some common data types include:
Examples of variable declarations:
int age = 25;
double height = 5.9;
char grade = 'A';
string name = "Alice";
bool isActive = true;
C# supports a variety of operators, which allow you to perform operations on variables and values. These include:
Arithmetic Operators: +, -, *, /, %
int sum = 10 + 5; // sum will be 15
Comparison Operators: ==, !=, >, <, >=, <=
bool result = (10 > 5); // result will be true
Logical Operators: && (AND), || (OR), ! (NOT)
bool result = (true && false); // result will be false
Control flow statements are used to control the flow of execution in a program. C# provides several types of control flow mechanisms:
Conditional Statements: Used to make decisions based on conditions.
if, else, else ifint age = 18;
if (age >= 18)
{
Console.WriteLine("Adult");
}
else
{
Console.WriteLine("Minor");
}
Switch Statement: A way to select one of many code blocks based on a value.
int day = 3;
switch (day)
{
case 1:
Console.WriteLine("Monday");
break;
case 2:
Console.WriteLine("Tuesday");
break;
case 3:
Console.WriteLine("Wednesday");
break;
default:
Console.WriteLine("Invalid day");
break;
}
Loops: Used to repeat a block of code multiple times.
for, while, do-whilefor (int i = 1; i <= 5; i++)
{
Console.WriteLine(i); // Prints 1, 2, 3, 4, 5
}
Methods are blocks of code designed to perform a specific task. Methods in C# can return a value or be void (meaning they don’t return anything). They can also take parameters.
Example of a method with parameters and a return value:
int AddNumbers(int a, int b)
{
return a + b;
}
int result = AddNumbers(5, 10); // result will be 15
C# is an object-oriented language, which means it uses classes to define objects. A class is a blueprint for creating objects, and an object is an instance of a class.
Example:
class Person
{
public string Name;
public int Age;
public void Introduce()
{
Console.WriteLine("Hello, my name is " + Name + " and I am " + Age + " years old.");
}
}
class Program
{
static void Main()
{
Person person1 = new Person(); // Creating an object
person1.Name = "Alice";
person1.Age = 30;
person1.Introduce(); // Prints: Hello, my name is Alice and I am 30 years old.
}
}
C# supports key object-oriented programming principles, including:
Encapsulation: Grouping related data (fields) and methods in a class. C# also supports access modifiers (public, private, protected) to control the visibility of members.
Example:
class Car
{
private string model;
public void SetModel(string model)
{
this.model = model;
}
public string GetModel()
{
return model;
}
}
Inheritance: A class can inherit members (fields, methods) from another class, allowing for code reuse and extension.
Example:
class Animal
{
public void Eat()
{
Console.WriteLine("Eating...");
}
}
class Dog : Animal
{
public void Bark()
{
Console.WriteLine("Woof!");
}
}
Polymorphism: Allows objects to be treated as instances of their parent class, enabling method overriding and method overloading.
Example:
class Animal
{
public virtual void MakeSound()
{
Console.WriteLine("Some generic sound");
}
}
class Dog : Animal
{
public override void MakeSound()
{
Console.WriteLine("Woof!");
}
}
Animal myDog = new Dog();
myDog.MakeSound(); // Prints "Woof!"
Abstraction: Hiding the implementation details and exposing only the essential parts of an object.
Example using an interface:
interface IAnimal
{
void Speak();
}
class Dog : IAnimal
{
public void Speak()
{
Console.WriteLine("Woof!");
}
}
In C#, collections are used to store groups of related objects. Some common collection types include:
Arrays: Fixed-size collections of elements.
int[] numbers = { 1, 2, 3, 4, 5 };
Lists: Flexible, dynamically-sized collections that can grow or shrink.
List<int> numbers = new List<int> { 1, 2, 3 };
numbers.Add(4);
C# is a powerful and versatile programming language that enables developers to build a wide range of applications. It follows the principles of object-oriented programming (OOP) and is built on a strong foundation of type safety and modern development practices. With its simple syntax, extensive libraries, and robust development environment, C# is an excellent choice for developing applications for the .NET platform.
Open this section to load past papers