Comprehensive Guide to C# Programming Concepts and Features
Type Safety
Type safety in C# ensures that the language enforces type constraints at compile time, preventing errors related to data types during program execution. C# is a strongly typed language, meaning that every variable and expression has a specific data type associated with it, and type checking occurs statically at compile time. This static type checking ensures that type mismatch errors are caught before execution. Explicit type conversion is required for converting values between incompatible types, reducing the risk of unintended conversions. C# supports type inference through the var keyword while maintaining type safety. Generics and nullable types further enhance type safety by providing mechanisms for parameterized types and handling null values.
Common Language Runtime (CLR)
The Common Language Runtime (CLR) is the heart of the .NET framework. It’s responsible for executing managed code, providing various services like memory management, exception handling, security, and thread management. Features: Garbage collector, Just-In-Time (JIT) Compilation, Exception Handling, Security, Type Safety.
.NET Core
is a cross-platform, open-source framework for building general-purpose applications, including console applications, desktop applications, and web applications. It provides a modular and lightweight runtime optimized for high-performance, scalability, and flexibility.
ASP.NET
is a web application framework within the .NET ecosystem specifically designed for building web applications and services. It provides libraries, tools, and patterns for developing dynamic web applications using languages like C# or VB.NET.
Windows Presentation Foundation (WPF)
is a graphical subsystem in the Microsoft .NET Framework for building Windows desktop applications.# WPF enables developers to create rich and interactive user interfaces for Windows applications. #It uses Extensible Application Markup Language (XAML) for designing user interfaces declaratively. #WPF supports powerful data binding capabilities, allowing developers to establish connections between UI elements and data sources.
Universal Windows Platform (UWP)
is for building Windows apps that run on all Windows 10 devices. #It ensures a consistent user experience across desktops, laptops, tablets, phones, Xbox, and IoT. #UWP supports adaptive layouts for dynamic UI adjustments based on device characteristics. #Provides access to modern Windows 10 features like Live Tiles, Cortana, and inking. #UWP apps run securely in a sandboxed environment with optimized performance.
VS POP-OOP, PROPERTIES-INDEXERS, DELEGATE-INTERFACE
Procedural Oriented Programming
- Here, the program is divided into small parts called functions.
- It follows a top-down approach.
- There is no access specifier in procedural programming.
- Adding new data and functions is not easy.
- overloading is not possible.
Ex: C, FORTRAN, Pascal, Basic, etc.
Object-Oriented Programming
- Here, the program is divided into small parts called objects.
- It follows a bottom-up approach.
- It has access specifiers like private, public, protected, etc.
- Adding new data and function is easy.
- Overloading is possible in object-oriented programming.
Ex: C++, Java, Python, C#, etc.
Properties
- are declared by giving a unique name.
- are identified by the names.
- can be declared as a static or an instance member.
- are invoked through a described name.
- does not needs this keyword in their creation.
Indexers
- are declared without giving a name.
- are identified by the signatures.
- are always declared as instance member, never as static member.
- are invoked using an index of the created object.
- needs this keyword in their keyword.
Delegate
- It could be a method only.
- It can be applied to one method at a time.
- If a delegate available in your scope you can use it.
- It can me implemented any number of times.
- It is used to handling events.
Interface
- It contains both methods and properties.
- If a class implements an interface, then it will implement all the methods related to that interface.
- It is used when your class implements that interface, otherwise not.
- Interface can be implemented only one time.
- It is not used for handling events.
Inheritance
Inheritance is a fundamental object-oriented programming concept that allows a class to inherit properties and behaviors from another class. Single inheritance is the simplest form of inheritance where a class inherits from only one base class. Multilevel inheritance involves a chain of inheritance where a derived class inherits from another derived class. Hierarchical inheritance involves multiple classes inheriting from a single base class. Interface is a reference type that serves as a contract defining a set of members (methods, properties, events, or indexers) that a class must implement. It provide a way to specify a common behavior that multiple classes can adhere to, promoting code reuse and enabling polymorphism.
CODE::::
interface IMyInterface {
void Method1();
int Property1 { get; set; } > event EventHandler MyEvent; }
Namespaces
Namespaces are organizational tools used to group related classes, interfaces, structures, enumerations, and delegates into named scopes. They provide a hierarchical naming structure that prevents naming conflicts and enhances code readability, particularly in large projects. Namespaces create unique identifiers for types within an application or library, allowing types with the same name to coexist without conflicts by defining distinct scopes.
using System;
using MyNamespace;
class Program {
static void Main(string[] args) {
MyClass myObject = new MyClass();
myObject.SayHello();
}} A Constructor is a special method within a class that is automatically called when an instance of the class is created. TYPES: #Default Constructor: without any parameters #Parameterized Constructor: with at least one parameter #Copy Constructor: which creates an object by copying variables from another object #Static Constructor: used to initialize static members of a class before any instance of the class is created #Private Constructor: it is not possible for other classes to derive from this class. A Destructor is a specialized method within a class responsible for performing cleanup tasks and releasing resources associated with an object before it’s garbage collected. It’s denoted by a tilde (~) followed by the class name and automatically invoked by the garbage collector when the object is no longer in use. Methods serve as encapsulated blocks of code within classes, designed to execute specific tasks or operations when called. By defining methods within classes, developers can implement logic for various actions, such as calculations, data processing, or interaction with external resources. Fields represent the state or data associated with instances of a class. They define the attributes or characteristics of objects and store information that persists throughout the lifetime of the object. Finalizer is a special method within a class that is called automatically by the garbage collector before an object is destroyed and its memory is reclaimed. Dispose method is used for explicit resource cleanup and is typically implemented as part of the IDisposable interface. The Dispose method is responsible for releasing unmanaged resources, such as file handles, database connections, or unmanaged memory, held by an object.
Polymorphism is a fundamental concept in object-oriented programming (OOP) that allows objects of different classes to be treated as objects of a common superclass. TYPES: Compile-time Polymorphism (Method Overloading):allows a class to have multiple methods with the same name but with different parameters. class Calculator {
public int Add(int x, int y) {
return x + y; }
public double Add(double x, double y) {
return x + y; }} Run-time Polymorphism (Method Overriding):allows a subclass to provide a specific implementation of a method that is already defined in its superclass. >> class Animal {
public virtual void MakeSound() {
Console.WriteLine(“Animal makes a sound”); }}
class Dog : Animal {
public override void MakeSound() {
Console.WriteLine(“Dog barks”);}}
class Cat : Animal {
public override void MakeSound() {
Console.WriteLine(“Cat meows”); }} Access modifiers are keywords used to specify the accessibility or visibility of types and type members (such as fields, methods, properties, and events) within a program. Public: Members marked as public are accessible from any other code in the same assembly or from other assemblies that reference the assembly containing the public member. Private: Members marked as private are accessible only within the same class or struct in which they are declared. Protected: Members marked as protected are accessible within the same class, derived classes, or classes in the same assembly. Internal: Members marked as internal are accessible only within files in the same assembly. Protected internal: protected +internal. Private protected: restricts access to the containing class or types derived from the containing class within the same assembly. Enums, short for enumerations, provide a way to define a set of named integral constants. They allow you to define a type with a fixed set of values, which makes your code more readable and maintainable by giving meaningful names to those values. Enums are essentially integer types, but you can associate any integral type with enum members, including byte, sbyte, short, ushort, int, uint, long, or ulong. Struct is a value type that can encapsulate related data members and related functionalities. are similar to classes, but they are used for lightweight objects that contain small amounts of data.
Events provide a way for objects to notify other objects when something of interest happens. Events are a key feature of the C# language, especially when working with user interfaces, asynchronous programming, or implementing the observer pattern. >> using System;
public class Publisher {
public event EventHandler DataReceived;
public void SimulateDataReceived(string data) {
OnDataReceived(data);}
protected virtual void OnDataReceived(string data) {
DataReceived?.Invoke(this, data); }}
class Program {
static void Main(string[] args) {
Publisher publisher = new Publisher();
publisher.DataReceived += (sender, data) => {
Console.WriteLine($”Data received: {data}”); };
publisher.SimulateDataReceived(“Hello, world!”);}}
Lambda expressions in C# provide a concise way to represent anonymous methods, allowing you to define inline functions without explicitly declaring a separate method. Types: #Expression Lambdas:are used to represent an expression that evaluates to a single value eg x => x * x #Statement Lambdas: are used to represent a block of statements eg (x, y) => { return x + y; } #Predicate Lambdas: They typically represent a condition that returns a Boolean value. eg x => x > 0 #Action Lambdas: represent a method that performs some action but does not return a value. eg () => Console.WriteLine(“Hello”) #Function Lambdas: represent a method that takes input and returns a value.eg (x, y) => x + y. Exception handling in C# allows you to gracefully manage errors that occur during program execution. #Try Block: You enclose the code that might throw an exception inside a try block. #Catch Block: you can follow the try block with one or more catch blocks to handle specific types of exceptions. If an exception occurs in the try block, the runtime looks for a matching catch block to handle it. Finally Block (Optional):is commonly used for cleanup tasks like closing files or releasing resources. >>>> try
{ int[] numbers = { 1, 2, 3 };
Console.WriteLine(numbers[4]); }
catch (IndexOutOfRangeException ex)
{ Console.WriteLine(“Index out of range.”);}
catch (Exception ex)
{Console.WriteLine(“An error occurred: ” + ex.Message);}
finally { Console.WriteLine(“Cleanup code executed.”);}
LINQ (Language-Integrated Query) is a powerful feature introduced in C# that allows you to query and manipulate data from different data sources using a SQL-like syntax directly within your C# code. It provides a uniform way to query various data sources such as collections, arrays, databases, XML, and more.
using System;
using System.Collections.Generic;
using System.Linq;
class Program {
static void Main(string[] args) {
// Sample list of students
List students = new List()
{
new Student { Id = 1, Name = “John”, Address = “Kirtipur”, College = “Universal College” },
new Student { Id = 2, Name = “Alice”, Address = “Kathmandu”, College = “Universal College” },
new Student { Id = 3, Name = “Bob”, Address = “Kirtipur”, College = “Oxford College” },
new Student { Id = 4, Name = “Emily”, Address = “Kirtipur”, College = “Universal College” }
};
// LINQ query to select students who live in Kirtipur and study at Universal College
var selectedStudents = from student in students
where student.Address == “Kirtipur” && student.College == “Universal College”
select student;
// Displaying the selected students
Console.WriteLine(“Students who live in Kirtipur and study at Universal College:”);
foreach (var student in selectedStudents)
{
Console.WriteLine($”Id: {student.Id}, Name: {student.Name}, Address: {student.Address}, College: {student.College}”); } } }
// Student class
class Student
{
public int Id { get; set; }
public string Name { get; set; }
public string Address { get; set; }
public string College { get; set; } }
