Key Point of C#
Key Point of C#
Reference Sites:
https://www.completecsharptutorial.com/basic/
https://www.tutorialsteacher.com/csharp/csharp-tutorials
https://www.javatpoint.com/c-sharp-tutorial
CLR:
Common Language Runtime
.NET CLR is a run-time environment that manages and executes the code written in
any .NET programming language. It converts code into native code which further can be
executed by the CPU.
To write the code using any dot net supported programming languages such as C#, VB,
J#, etc. Then the respective language compiler will compile the program code and
generate something called Microsoft Intermediate language (MSIL) or Intermediate
language (IL) code. For example, if the programming language is C#, then the compiler
is CSC and if the programming language is VB, then the compiler will be VBC.
This Intermediate Language (IL) code is half compiled code i.e. partially compiled code
and cannot be executed directly by the Operating System. To execute this Microsoft
Intermediate language (MSIL) or Intermediate language (IL) code on your machine,
the .NET Framework provides called Common Language Runtime (CLR) which takes
the responsibility to execute your Microsoft Intermediate language (MSIL) or
Intermediate language (IL) Code.
The CLR takes the IL (Intermediate Language) code and gives it to something called JIT
(Just-in-Time) Compiler. The JIT compiler reads each and every line of the IL code and
converts it to machine-specific instructions (i.e. into binary format) which can be
executed by the underlying Operating System.
1. Managed Code: The MSIL code which is managed by the CLR is known as the
Managed Code. For managed code CLR provides three .NET facilities:
2. Unmanaged Code: Before .NET development the programming language like .COM
Components & Win32 API do not generate the MSIL code. So these are not
managed by CLR rather managed by Operating System.
1. Value Types: Value Types will store the value directly into the memory location.
These types work with stack mechanism only. CLR allows memory for these at
Compile Time.
2. Reference Types: Reference Types will contain a memory address of value
because the reference types won’t store the variable value directly in memory. These
types work with Heap mechanism. CLR allots memory for these at Runtime.
Garbage Collector:
It is used to provide the Automatic Memory Management feature. If there was no
garbage collector, programmers would have to write the memory management codes
which will be a kind of overhead on programmers.
Data Types:
C# is a strongly typed language. It means, that you cannot use variable without data
types. Data types tell the compiler that which type of data is used for processing.
C# provides two types of data types:
1. Value types
2. Reference types.
Value Types:
Value type data type stores copy of the value, data type is a value type if it holds a data
value within its own memory space. It means the variables of these data types directly
contain values.
bool
byte
char
decimal
double
enum
float
int
long
sbyte
short
struct
uint
ulong
ushort
Reference types.
Reference type data types stores the address of the value, unlike value types, a
reference type doesn't store its value directly. Instead, it stores the address where the
value is being stored. In other words, a reference type contains a pointer to another
memory location that holds the data.
Variable:
A variable refers to the memory address. When you create variable, it creates holds
space in the memory that is used for storing temporary data. As you know about c# data
types, each data type has predefined size.
boxing:
Boxing is the process of converting a value type to the object type or any interface type
implemented by this value type. Boxing is implicit.
All the reference types stored on heap where it contains the address of the value and
value type is just an actual value stored on the stack.
Example: int i is assigned to object o. Object o must be an address and not a value
itself. So, the CLR boxes the value type by creating a new System. Object on the heap
and wraps the value of i in it and then assigns an address of that object to o. So,
because the CLR creates a box on the heap that stores the value, the whole process is
called 'Boxing'.
Unboxing:
object o = 10;
int i = (int)o; //performs unboxing
Invalid Conversion
int i = 10;
object o = i; // boxing
double d = (double)o; // runtime exception
Valid Conversion
int i = 10;
object o = i; // boxing
double d = (double)(int)o; // valid
Encapsulation: Encapsulation is the process of hiding irrelevant data from the user,
It is hide unwanted code within a capsule, Encapsulation is used Access Specifiers to
hide its members from outside class or interface, those are five
1. Public
2. Private
3. Internal
4. Protected
5. protected internal
Public: it can be accessed by other class members that are initialized outside the class
and even outside the namespace
Private: Restrict the member variable or function to be called outside of the parent
class you can store or retrieve the value from private access modifiers using get the set
property on public access modifier.
Protected: The protected access specifier hides its member variables and functions
from other classes and objects. This type of variable or function can only be accessed in
child class. It becomes very important while implementing inheritance.
Internal: Internal access specifier allows a class to expose its member variables and
member functions to other functions and objects in the current assembly. The variable or
classes that are declared with internal can be access by any member within application.
It is the default access specifiers for a class in C# programming.
Protected internal:
Private protected:
Method hiding: you can hide the implementation of the methods of a base class from
the derived class using the new keyword.
Interface:
Interface can have methods, properties, events, and indexers as its members. But
interfaces will contain only the declaration of the members. The implementation of the
interface’s members will be given by class who implements the interface implicitly or
explicitly.
In C# doesn’t support multiple inheritance, but by using this interface we can overcome
that problems.
By default all the members of Interface are public and abstract.
Constructor:
Reference: https://dotnettutorials.net/lesson/constructors-csharp/
Types of Constructor:
Types of classes:
Abstract class
Partial class
Sealed class
Static class
Reference:
https://tutorialslink.com/Articles/Different-types-of-classes-in-c-programming/1167
Reference:
https://dotnettutorials.net/course/solid-design-principles/
Object Pooling:
Reference: https://www.codeguru.com/csharp/object-pooling-in-c/
https://www.c-sharpcorner.com/article/object-pooling-in-net/
Reference: https://www.c-sharpcorner.com/UploadFile/0c1bb2/ienumerable-
interface-in-C-Sharp/
https://www.c-sharpcorner.com/UploadFile/219d4d/ienumerable-vs-ienumerator-
in-C-Sharp/
https://www.c-sharpcorner.com/forums/ienumerable-vs-list
yield keyword:
https://www.c-sharpcorner.com/UploadFile/5ef30d/understanding-yield-return-in-
C-Sharp/
Dependency inversion
Reference: https://dotnettutorials.net/lesson/dependency-inversion-principle/
What is dependency injection and what are the types? This is design pattern
Constructor injection
Property injection
Method injection
https://dotnettutorials.net/lesson/dependency-injection-design-pattern-csharp/
https://dotnettutorials.net/lesson/shallow-copy-and-deep-copy/