Posts

Showing posts from April, 2021

Day 23 : Boxing and Unboxing

Image
 In this blog we are going to understand the concept of boxing and unboxing. Boxing : Boxing is the process of converting a value-type to a reference-type. When CLR(Common Language Runtime) boxes a value-type, it wraps a value inside System.Object instance and stores it on managed heap. In simple words, Boxing is used to store the value type in the garbage collected heap. Boxing is an implicit conversion of value type to object. Example :                                       In above example, 'a' is a int(value-type) have value '200'. After that object 'o' copies value from 'i' i.e boxing. The result of this statement is, it creates object reference 'o' on stack and that references value of type int('i') on heap. We can also perform explicit boxing but it is not required. Example :                       ...

Day 22 : Garbage Collector

Image
  Before going to garbage collector we are going to understand some other concepts : Memory Leak : If a program does not return or release allocated  memory  space, even after the execution is done then it results slower or unresponsive system behavior which called as Memory Leak . A  memory leak  may also happen when an object is stored in  memory  but cannot be accessed by the running code. Even though we have a GC(Garbage Collector), memory leak easily happens. Some common reasons of memory leaks are :       1. Events :  The .NET event listeners are the most common memory leak reason. We create a event and forget to remove it. We can avoid this by unsubscribe the subscribed event, use weak-handlers etc.        2. Static : Static objects live at GC root and they never collected by the collector.  As we all know the static instances stay forever in memory which causes memory leak.       3. ...

Day 21 : Stack and Heap in C#

Image
  Value Type and Reference Type :                               Value types and Reference type are the types of data type which are divided on basis of how they store their values in memory. Value Type : A data type is said to be value type if it holds a data value in its own memory. Variables of these data type directly contains values. The data types which are value type are : bool, byte, int, char, float, enum, short, long, struct,decimal, double etc. If a variable is passed by value type then the system creates another copy for that variable. Because of this if value got changed for one variable it won't affect another. It uses stack for storing data. Reference Type : Reference type doesn't store it's value directly. It stores the address(stack) where the value(heap) is being stored. A reference type contains pointer which points to another memory location where the data is stored. It uses stack as well ...

Day 20 : Casting and Conversion in C#

Image
  Casting :           Casting means copying or converting a value from one data type to another data type. Casting is divided into two types : 1) Implicit casting :  Implicit casting is done automatically by compiler when we pass smaller size type value to a larger size type. If we assign larger data type to smaller data type then it will won't work here. Smaller to larger data type : byte > short > int  > long > float > double. Also before converting compiler checks the compatibility i.e only numeric data types are compatible with each other but no conversion is their between numeric to char or boolean. Also char and boolean are not compatible with each other. Their are many compilation error(missing cast) when types are not compatible with each other. Example : 2) Explicit casting :  If we want to assign a value of larger data type  to smaller data type we perform explicit casting. The explicit casting is done manually ...

Day 20 : Data Types in C#

Image
  C# Data Types :                     As we all know C# is a strongly-typed language. Means we must declare the type of variable which indicates what type of value it going to store such as int, char, float etc. C# is mainly divided data types in two types: 1) Value types includes simple type(like int, float, char and bool), enum types, struct types. 2) Reference types includes class types, interface types, array types. Numeric data types : The numeric data type or numbers are divide into two types based on their size in the memory and capacity to store numbers : integer-type, floating-point type. Integer type numbers are the whole numbers without decimal points. Also they can be negative or positive. Valid types in integer type are byte, sbyte, short, ushort, int, uint, long, ulong. Floating-point type numbers have a one or more decimal points. Also they can be negative or positive. Valid types in floating-point type are float, d...

Day 19 : Class Libraries in C#

Image
  In this blog we are going to understand the concept of class library and how to build a class library. Class Library : A class library is a collection of class definition contained in .dll or .exe file. Both dll(Dynamic Link Library) and exe(Executable) files are executable program modules but the difference is that we cannot execute dll files directly. The dll contains program code, data that can be used by other programs and easily implemented into other VS projects also. In order to use class library we must add reference to the library. Now we are going to understand how to build a class library :- 1) Build a class library : Open VS 2019 and click on create new project tab Select Class Library(.NET Core) and press next button. Give a project name and press create button. Our class library has only one Class1.cs file which contains methods (like multiply) for API.                                ...