C# Dynamic Type: Var vs dynamic keywords in C#

The dynamic keyword is used to define dynamic types. Dynamic type is used to avoid compile-time type checking. The compiler does not type-check dynamic type variables at compile time, instead, the compiler check the type at the run time. If the syntax of the statement is invalid, an error will be thrown at runtime.

Dynamic types are similar to object types, despite the fact that type checking for object type variables occurs at compile time rather than at runtime for dynamic type variables.

csharp dynamic type
C# dynamic type

Read more >>

C# Unsafe Code: Why do we need unsafe code in C#?

Unsafe code in C#

In general, the code written in C# is safe code that doesn`t directly access memory using pointers. It also doesn’t allocate raw memory. Instead, managed objects are created.
On the other hand, unsafe code in C# usually refers to a section of code that is not processed by the CLR (Common Language Runtime) and directly access memory using pointers.

Since pointers are complex and might result in memory-related errors like stack overflow, overwritten system memory, etc., extra caution must be taken while handling unsafe code.
By default, pointers are not supported in C#. However, the use of pointer variables is permitted by the unsafe keyword.

charp unsafe code

Read more >>

C# Events: Top 5 Differences between delegates and events in C#

C# Events: The Events enable a class or object to notify other classes or objects when some interesting thing happens to the object. A class that sends or raises the event is called the publisher, and the classes that receive or handle the event are referred to as subscribers.

There can be multiple subscribers to a single event. Typically, when an action takes place, a publisher raises an event. The subscribers who want to be notified when an action takes place should register or subscribe to handle specific events.

The following diagram shows the event in C#.

csharp events
C# event model

Read more >>

Sealed Class in C# with examples

csharp-sealed-class
sealed class in C#

What exactly is a sealed class?

A sealed class is a class that cannot be inherited by any class but can be instantiated. A sealed class can’t have any derived classes. A common use for a sealed class is to prevent a class from being accidentally inherited.

The purpose of a sealed class is to show that it is highly specialized and does not need to extend to add new functionality through inheritance. This is because it modifies or overrides its behavior.

Read more >>

C# Arrays: Everything You Need to Know About Array in C#

C# Arrays: An Array is used to store multiple values in a single variable, rather than declaring a separate variable for each element. It is a fixed-sized sequential collection of elements of the same type.
The data types of the elements in an array can be int, char, float, string, and so on, and they are stored in a contiguous location.

C# Arrays
C# Arrays

Read more >>

Comments in C#: Single Line, Multiline and XML Comments

Comments-in-C-Sharp
Comments in C#

Introduction: Comments in C#

Comments are used in a program to make a section of code easier to understand. They’re used to make the code more readable and to provide a formal description of the code and how it works. The compiler completely ignores comments, so you may put them wherever in a program and not worry about them breaking it.

Read more >>