C# Tutorial

C# Tutorial C# First Application C# Variables C# Data Types C# Operators C# Keywords

C# Control Statement

C# If Statements C# Switch Statements C# for Loop C# While Loop C# do While loop C# Jump Statements C# Function C# functions with out variable

C# Arrays

C# Arrays

C# Function

C# Function call by value C# Call by reference C# Passing array to function C# Multidimensional Arrays C# Jagged Arrays C# Params C# Array Class C# Command Line Arguments

C# Object Class

C# Object and Classes C# Constructors C# Destructor C# this Keyword C# static field C# static class C# Static Constructor C# Structs C# enum C# Properties

C# Inheritance

C# Inheritance C# Multilevel Inheritance C# Aggregation C# Member overloading C# Method Overriding C# Base

C# Polymorphism

C# Polymorphism C# Sealed

C# Abstraction

C# Abstraction C# Interface

C# Namespace

C# Namespace C# Access Modifiers C# Encapsulation

C# Strings

C# String

C# Misc

C# Design Patterns Dictionary in C# Boxing and Unboxing in C# Ref and Out in C# Serialization in C# Dispose and Finalize in C# CONSOLE LOG IN C# Get File extension in C# Insert query in c# Difference Between List and Dictionary in C# Getters and Setters in C# Extension Methods in C# Insert query in c# CONSOLE LOG IN C# Get File extension in C# Random.NextDouble() Method in C# Binary Search in C# Difference between Delegates and Interfaces in C# Double.IsFinite() Method in C# Index Constructor in C# Abstraction in C# Basic OOPS Concepts In C# Queue.CopyTo() Method in C# single.compareto() method in C# C# Throw Exception in Constructor DECODE IN C# file.setlastwritetimeutc() method in C# Convert Object to List in C# convert.ToSByte(string, IFormatProvider) Method in C# C# Declare Delegate in Interface console.TreatControl C As Input property in C# Copying the queue elements to 1-D Array in C# Array.Constrainedcopy() Method in C# C# in and out Char.IsLetterOrDigit() method in C# Debugging in C# decimal.compare() method in C# Difference between Console.Read and Console.Readline in C# Metadata in C# C# Event Handler Example Default Interface Methods in C# Difference between hashtable and dictionary in C# C# program to implement IDisposable Interface Encapsulation in C# SortedList.IndexOfVaalue(Object) Method in C# Hash Maps in C# How to clear text files in C# How to Convert xls to xlsx in C# Foreach loop in C# FIFO in C# How to handle null exception in C# Type.Is Instance Of Type() Method in C# How to add data into MySQL database using C# How to use angular js in ASP net Csharp decimal.compare() method in Csharp Difference between Console.Read and Console.Readline in Csharp How to Implement Interface in Csharp char.IsUpper() Method in C# Index Of Any() Method in C# Quantifiers in C# C# program to Get Extension of a Given File C# Error Logging C# ENCRIPTION Can we create an object for Abstract Class in C# Console.CursorVisible in C# SortedDictionary Implementation in C# C# Hash Table with Examples Setting the Location of the Label in c# Collections in c# Virtual Keyword in C# Reverse of string in C# String and StringBuilder in C# Encapsulation in C# SortedList.IndexOfVaalue(Object) Method in C# Hash Maps in C# How to clear text files in C# How to Convert xls to xlsx in C# Foreach loop in C# FIFO in C# How to handle null exception in C# Type.Is Instance Of Type() Method in C# How to add data into MySQL database using C# How to use angular js in ASP net Csharp decimal.compare() method in Csharp Difference between Console.Read and Console.Readline in Csharp How to Implement Interface in Csharp char.IsUpper() Method in C# Index Of Any() Method in C# Quantifiers in C# C# program to Get Extension of a Given File Difference between ref and out in C# Singleton Class in C# Const And Readonly In Csharp BinaryReader and BinaryWriter in C# C# Attributes C# Delegates DirectoryInfo Class in C# Export and Import Excel Data in C# File Class in C# FileInfo Class in C# How to Cancel Parallel Operations in C#? Maximum Degree of Parallelism in C# Parallel Foreach Loop in C# Parallel Invoke in C# StreamReader and StreamWriter in C# TextReader and TextWriter in C# AsQueryable() in C# Basic Database Operations Using C# C# Anonymous Methods C# Events C# Generics C# Indexers C# Multidimensional Indexers C# Multithreading C# New Features C# Overloading of Indexers Difference between delegates and events in C# Operator overloading in C# Filter table in C# C# Queue with Examples C# Sortedlist With Examples C# Stack with Examples C# Unsafe Code File Handling in C# HashSet in C# with Examples List Implementation in C# SortedSet in C# with Examples C# in Depth Delegates and Events in C# Finally Block in C# How to Split String in C# Loggers in C# Nullable Types in C# REVERSE A STRING IN C# TYPE CASTING IN C# What is Generics in C# ABSTRACT CLASS IN C# Application of pointer in C# Await in c# READONLY AND CONSTANT IN C# Type safe in C# Types of Variables in c# Use of delegates in c# ABSTRACT CLASS IN C# Application of pointer in C# Await in c# READONLY AND CONSTANT IN C# Type safe in C# Types of Variables in c# Use of delegates in c# ABSTRACT CLASS IN C# Application of pointer in C# Await in c# READONLY AND CONSTANT IN C# Type safe in C# Types of Variables in c# Use of delegates in c# Atomic Methods Thread Safety and Race Conditions in C# Parallel LINQ in C# Design Principles in C# Difference Between Struct And Class In C# Difference between Abstraction and Encapsulation in C# Escape Sequence Characters in C# What is IOC in C# Multiple Catch blocks in C# Appdomain in C# Call back methods in C# Change Datetime format in C# Declare String array in C# Default Access Specifier in c# Foreach in LINQ C# How to compare two lists in C# How to Convert String to Datetime in c# How to get only Date from DateTime in C# Ispostback in asp net C# JSON OBJECT IN C# JSON STRINGIFY IN C# LAMBDA FUNCTION IN C# LINQ Lambda Expression in C# Microservices in C# MSIL IN C# Reference parameter in C# Shadowing(Method hiding) in C# Solid principles in C# Static Members in C# Task run in C# Transaction scope in C# Type Conversion in c# Unit of Work in C# Unit Test Cases in c# User Defined Exception in c# Using Keyword in C# Var Keyword in C# What is gac in C#

C# in Depth

Microsoft created the flexible programming language C# as a component of the.NET framework. It is pronounced "C sharp." It is often used to make many applications, including desktop software, online applications, mobile apps, and game development. Simple, type-safe, and scalable are three qualities that C# is renowned for.

You must investigate complex ideas and methods to learn C# in depth. Consider the following topics:

Object-Oriented Programming (OOP)

It enables you to organize your code and develop reusable components by allowing you to construct classes, objects, and interfaces. A fundamental idea of C#, object-oriented programming (OOP), is essential to designing and creating software applications. The main components of OOP in C# are as follows:

  • Classes and Objects
  • Encapsulation
  • Inheritance
  • Polymorphism
  • Abstraction
  • Modifiers
  • Polymorphism
  • Composition and Aggregation
  • Constructors and Destructors
  • Accessor and Mutator Methods

By comprehending and implementing these concepts, you may design and create robust, modular, and maintainable C# programs. Effective OOP in C# mastery requires practice and practical experience.

Code:

using System;

// Base class

class Shape

{

    // Properties

    public string Color { get; set; }

    // Constructor

    public Shape(string color)

    {

        Color = color;

    }

    // Method

    public virtual void Draw()

    {

        Console.WriteLine("Drawing a shape with color: " + Color);

    }

}

// Derived class

class Circle: Shape

{

    // Properties

    public double Radius { get; set; }

    // Constructor

    public Circle(string color, double radius) : base(color)

    {

        Radius = radius;

    }

    // Method overriding

    public override void Draw()

    {

        Console.WriteLine("Drawing a circle with color: " + Color + ", and radius: " + Radius);

    }

}

class Program

{

    static void Main()

    {

        // Create objects and invoke methods

        Shape shape = new Shape("Red");

        shape.Draw();

        Circle circle = new Circle("Blue", 5.0);

        circle.Draw();

    }

}

Output:

C# in Depth

Strong Typing:

C# is a strongly typed language which enforces strict type-checking during compilation. It helps prevent common programming errors and enhances code reliability.

  • Static Typing
  • Type Safety
  • Explicit Type Declarations
  • Type Inference
  • Compile-Time Type Checking
  • Strongly Typed Collections
  • Type Casting
  • Type Safety in APIs

Code:

using System;

class Program

{

    static void Main()

    {

        int age = 30; // Declaring an integer variable 'age'

        string name = "John"; // Declaring a string variable 'name'

        Console.WriteLine("My name is " + name + " and I am " + age + " years old.");

        Console.ReadKey();

    }

}

Output:

C# in Depth

Generics:

Generics are supported in C#, enabling you to develop type-safe classes, functions, and data structures that interact with various types. Generics offer flexibility and improved performance by eliminating the requirement for packing and unboxing procedures.

  • Generic Classes and Interfaces
  • Type Parameters
  • Creating Instances
  • Generic Methods
  • Constraints
  • Covariance and Contravariance
  • Generic Constraints
  • Generic Collections

Code:

using System;

class GenericList<T>

{

    private T[] items;

    private int count;

    public GenericList(int size)

    {

        items = new T[size];

        count = 0;

    }

    public void Add(T item)

    {

        If (count < items.Length)

        {

            items[count] = item;

            count++;

        }

        else

        {

            Console.WriteLine("List is full. Cannot add more items.");

        }

    }

    public void DisplayItems()

    {

        Console.WriteLine("List Items:");

        for (int i = 0; i < count; i++)

        {

            Console.WriteLine(items[i]);

        }

    }

}

class Program

{

    static void Main()

    {

        // Create a list of integers

        GenericList<int> intList = new GenericList<int>(5);

        intList.Add(10);

        intList.Add(20);

        intList.Add(30);

        intList.DisplayItems();

        // Create a list of strings

        GenericList<string> stringList = new GenericList<string>(3);

        stringList.Add("Hello");

        stringList.Add("World");

        stringList.DisplayItems();

    }

}

Output:

C# in Depth

Language Integrated Query (LINQ):

With a single, powerful syntax, LINQ in C# makes it possible to access and manipulate data from many sources, including databases, collections, and XML files. You may use it to create expressive queries using syntax that resembles SQL.

  • LINQ Query Expressions
  • LINQ to Objects
  • LINQ to SQL
  • LINQ to Entities (Entity Framework)
  • LINQ to XML
  • LINQ to DataSet
  • Deferred Execution
  • Composition and Extensibility
  • Strong Typing and IntelliSense

LINQ is a vital tool that makes it easier to access and manipulate data in C# by offering a consistent syntax and set of operations for use with different data sources. It encourages code readability, type safety, and code reuse, making it simpler to deal consistently with many forms of data.

Code:

using System;

using System.Linq;

class Program

{

    static void Main()

    {

        // Sample data

        int[] numbers = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };

        // Query Syntax

        var evenNumbersQuery =

            from num in numbers

            where num % 2 == 0

            select num;

        // Method syntax

        var evenNumbersMethod = numbers.Where(num => num % 2 == 0);

        // Displaying the results

        Console.WriteLine("Even Numbers (Query Syntax):");

        foreach (var num in evenNumbersQuery)

        {

            Console.WriteLine(num);

        }

        Console.WriteLine("Even Numbers (Method Syntax):");

        foreach (var num in evenNumbersMethod)

        {

            Console.WriteLine(num);

        }

    }

}

Output:

C# in Depth

Asynchronous Programming:

The async and await keywords in C# offer native support for asynchronous programming. By carrying out non-blocking operations—like network requests or file I/O—without obstructing the main execution thread, asynchronous programming enables you to develop effective and responsive programs. Here's an overview of asynchronous programming in C#:

  • Asynchronous Methods
  • Await Operator
  • Task-based Asynchronous Pattern (TAP)
  • Asynchronous APIs
  • Task Parallel Library (TPL)
  • Asynchronous Event Handling
  • Async/await Best Practices
  • Asynchronous Streams

Building responsive and scalable apps using asynchronous operations and parallelism is possible with C# asynchronous programming. Performance, responsiveness, and resource use are all improved. To guarantee accuracy and maintainability, it necessitates careful consideration of best practices and appropriate management of asynchronous code.

Exception Handling:

Runtime exceptions may be caught and handled thanks to the robust exception handling system in C#. It aids in developing applications that are fault-tolerant and more dependable. Here's an overview of exception handling in C#:

  • Exceptions and the Exception Hierarchy
  • try-catch Block
  • Multiple catch Blocks
  • finally Block
  • throw Statement
  • Custom Exceptions
  • Exception Propagation
  • Exception Handling Best Practices

You may elegantly manage problems and guarantee the stability and dependability of your programs by using proper exception handling in C#. It allows you to recognize and deal with unusual situations, recover from mistakes, and provide users insightful feedback or log errors for further troubleshooting.

Code:

using System;

class Program

{

    static void Main()

    {

        try

        {

            int dividend = 10;

            int divisor = 0;

            int result = Divide(dividend, divisor);

            Console.WriteLine("Result: " + result);

        }

        Catch (DivideByZeroException ex)

        {

            Console.WriteLine("Error: Divide by zero");

        }

        Catch (Exception ex)

        {

            Console.WriteLine("Error: " + ex.Message);

        }

        Console.WriteLine("Program execution continues...");

    }

    static int Divide(int dividend, int divisor)

    {

        if (divisor == 0)

        {

            throw new DivideByZeroException();

        }

        Return dividend/divisor;

    }

}

Output:

C# in Depth

Memory Management:

C# uses automatic memory management through the .NET Common Language Runtime (CLR). It provides a garbage collector that automatically deallocates memory for no longer referenced objects, reducing the risk of memory leaks. Here are some key points to understand about memory management in C#:

  • Automatic Memory Allocation
  • Garbage Collection
  • Reachability and Object Lifetimes
  • Finalization
  • Dispose of Pattern
  • Memory Leaks
  • Performance Considerations

Because of the garbage collector, memory management in C# is often visible to the developer. When working with limited resources or situations where performance is crucial, solid knowledge of the underlying concepts may help you develop practical and reliable code.

Code:

using System;

class Person

{

    public string Name { get; set; }

    public Person(string name)

    {

        Name = name;

    }

    public void Display()

    {

        Console.WriteLine("Name: " + Name);

    }

}

class Program

{

    static void Main()

    {

        Person person = new Person("John");

        person.Display();

        person = null; // Set the reference to null to release the object

        Console.WriteLine("Program execution continues...");

    }

}

Output:

C# in Depth

Events and Delegates:

Events and delegates are available in C# and used to construct the publish-subscribe paradigm. Representatives offer a mechanism to declare and reference methods with a specified signature, whereas events allow objects to alert and respond to actions or changes. Here's an overview of events and delegates in C#:

  • Delegates: A delegate is a type that represents a method signature, allowing you to define and reference methods with a compatible signature.
  • Events: An event is a mechanism for communication between objects where one object (the publisher) notifies other things (subscribers) when a specific action or state change occurs.
  • Event Handlers
  • Raising Events

The event and delegate keywords can simplify events and delegates starting with C# 2.0. The compiler automatically generates the event's underlying delegate type and accessor functions

.

Using events and delegates is a potent method for implementing loosely linked systems and decoupling components. They are widely used in C# to create asynchronous programming patterns, UI programming, and event-driven programming techniques.

Code:

using System;

// Define a delegate for event handlers

public delegate void EventHandler();

// Publisher class

class Button

{

    //event declaration

    public event EventHandler Clicked;

    // Method to simulate a button click

    public void Click()

    {

        Console.WriteLine("Button clicked");

        OnClicked();

    }

    // Invoke the event

    protected virtual void OnClicked()

    {

        Clicked?.Invoke();

    }

}

// Subscriber class

class Program

{

    static void Main()

    {

        // Create a button object

        Button button = new Button();

        // Subscribe to the Clicked event by attaching an event handler

        button.Clicked += HandleButtonClick;

        // Simulate button click

        button.Click();

    }

    //event handler method

    static void HandleButtonClick()

    {

        Console.WriteLine("Button Click event handled");

    }

}

Output:

C# in Depth

Reflection:

Reflection, a potent tool provided by C#, lets you study and alter types, objects, and assemblies in real time. It is helpful for activities like dynamically loading bodies, looking at metadata, and executing methods. Here's an overview of the use of reflection in C#:

  • Obtaining Type Information
  • Inspecting Type Members
  • Invoking Methods and Constructors
  • Accessing and Modifying Properties and Fields
  • Retrieving and Applying Attribute
  • Dynamic Code Generation
  • Performance Considerations

Reflection is a potent tool that may be used for many things, like developing generic frameworks, producing code analysis tools, putting dependency injection containers in place, or constructing dynamic and extensible systems. But because it is active, it must be used carefully and with appropriate error handling because improper use might result in runtime errors and poor performance.

Code:

using System;

using System.Reflection;

class MyClass

{

    public string Name { get; set; }

    public int Age { get; set; }

    public void DisplayInfo()

    {

        Console.WriteLine("Name: " + Name);

        Console.WriteLine("Age: " + Age);

    }

}

class Program

{

    static void Main()

    {

        // Create an instance of MyClass

        MyClass obj = new MyClass();

        obj.Name = "John";

        obj.Age = 30;

        // Get the type of information using reflection

        Type = obj.GetType();

        // Display the property values using reflection

        PropertyInfo[] properties = type.GetProperties();

        foreach (PropertyInfo property in properties)

        {

            Console.WriteLine(property.Name + ": " + property.GetValue(obj));

        }

        // Invoke a method using reflection

        MethodInfo method = type.GetMethod("DisplayInfo");

        method.Invoke(obj, null);

    }

}

Output:

C# in Depth

Parallel Programming:

The Task Parallel Library (TPL) and Parallel LINQ (PLINQ) are two capabilities C# offers for parallel programming. These characteristics make it easier to create parallel and multi-threaded programs, effectively using current multi-core technology. Here are some key aspects of parallel programming in C#:

  • Task Parallelism
  • Parallel LINQ (PLINQ)
  • Parallel.ForEach
  • Parallel. Invoke
  • Concurrent Collections
  • Synchronization Primitives
  • Asynchronous Programming

It's crucial to consider aspects like data dependencies, load balancing, and potential thread synchronization problems while dealing with parallel programming in C#. To ensure your parallel code runs as planned and doesn't cause issues like thread congestion or deadlocks, it's also essential to profile and test it.

Code:

using System;

using System.Threading.Tasks;

class Program

{

    static void Main()

    {

        Console.WriteLine("Sequential Execution:");

        SequentialExecution();

        Console.WriteLine("\nParallel Execution:");

        ParallelExecution();

        Console.WriteLine("\nProgram execution completed.");

    }

    static void SequentialExecution()

    {

        for (int i = 1; i <= 5; i++)

        {

            Console.WriteLine("Task {0} started", i);

            PerformTask(i);

            Console.WriteLine("Task {0} completed", i);

        }

    }

    static void ParallelExecution()

    {

        Parallel.For(1, 6, i =>

        {

            Console.WriteLine("Task {0} started", i);

            PerformTask(i);

            Console.WriteLine("Task {0} completed", i);

        });

    }

    static void PerformTask(int taskNumber)

    {

        // Simulating some work

        Task.Delay(1000).Wait();

    }

}

Output:

C# in Depth

These are but a few examples of C#'s complexity and variety. Advanced subjects include design patterns, sophisticated data structures, networking, web programming with ASP.NET, and other topics still available for exploration and learning. To learn more about C# and its many features, check out the Microsoft documentation, online lessons, and programming-specific publications.