Dotnet
Dotnet
net Framework
It is used to develop applications for web, Windows, phone. Moreover, it provides a broad range
of functionalities and support.
The .NET Framework has four main components:
It is a program execution engine that loads and executes the program. It converts the program
into native code which further can be executed by the CPU.. It acts as an interface between the
framework and operating system
Managed Code
“Managed code is the code that is developed using the .NET framework and its supported
programming languages such as C# or VB.NET.
Managed code is directly executed by the Common Language Runtime (CLR or Runtime) and
its lifecycle including object creation, memory allocation, and object disposal is managed by the
Runtime. Any language that is written in .NET Framework is managed code".
Unmanaged Code
The code that is developed outside of the .NET framework is known as unmanaged code.
“Applications that do not run under the control of the CLR are said to be unmanaged.
Languages such as C or C++ or Visual Basic are unmanaged.
The object creation, execution, and disposal of unmanaged code is directly managed by the
programmers. If programmers write bad code, it may lead to memory leaks
What is C#
applications:
Window applications
Web applications
Distributed applications
The using keyword is used for including the namespaces in the program
Namespace
Namespaces in C# are used to organize too many classes so that it can be easy to handle the
application.
C# Object
Object is an instance of a class. All the members of the class can be accessed through object.
example
Student s1 = new Student();
Student(); is constructor;
C# Class
Methods
Every C# program has at least one class with a method named Main.
C# Constructor
In C#, constructor is a special method with same name as class used to initialize the data
members of new object.
Default constructor
A constructor which has no argument is known as default constructor. It is invoked at the time of
creating object.
Parameterized constructor
Copy Constructor
the purpose of copy constructor is to initialize a new instance by using values of existing
intance.
class Test
{
public Test(int i)
{ console.writeline("paraneterized constructor is called"); }
public Test(Test obj)
{Console.Writeline("Copy constructor is called");}
public static void main()
{
Test t = new Test(20); // Prametrized constructor
Test t2 = new Test(t); // copy constructor
}
}
Static Constructor
static constructor is responsible for initializing static variables.
if class contains static varibles then only the static constructor will implicitly present.else we
have to define explicitly.
static constructor is implicitly called .
static constructor can not be parameterized and overloaded.
static constructor is first to execute in any class.
access modifier not allowed
class Test
{
static Test()
{Console.writeline("Static constructor");}
}
Q. Why the need of explicit Constructor when every class has its default constructor for
variable initialization?
=> default constructor initialize the variable with same value even if we create multiple insrances
of class.
if we define explicit parameterized constructor then every time we create new instance we get
chance to initialize variable with new value.
destructor
Distructor is special method which has same name as class and starts with ~ symbol before the
class name.
eg.
Variable
1) Local Variable
A variable declared inside the body of the method is called local variable. You can use this
variable only within that method.
2) Instance Variable
A variable declared inside the class but outside the body of the method, is called an instance
variable. It is not declared as static
It is called an instance variable because its value is instance-specific and is not shared among
instances.
3) Static variable
class Test
{
int num2;
static int num;
static void main()
{
Test t1 = new Test();
Test t2 = new Test();
Console.Writeline(num);
Console.Writeline(t1.num2);
}
}
static constructor is implicitly called imediately after class execution is started.then the
code inside the main method will execute.hence no required to create an instance to call
constructor explicitly for variable inialization for static members.
4. Constant Variables
constant variable behaves same as static variable as they dont required instance creation for
accessing the variable and memory allocation happens only once through out class lifecycle.
The variable declared with readonly keyword is known as read only variable.
the read only variable behaves like non static variable as it requires instance creation for
accessing and initializtion of variable. only defference is value cant modified after initialization.
C# Data Types
A data type specifies the type of data that a variable can store such as integer, floating,
character etc.
The reference data types do not contain the actual data stored in a variable, but they contain a
reference to the variables.change in reference will also change the actual data stored
pointer type
Datatypes
Value Data Type short, int, char, float, double etc
Reference Data Type String, Class, Object and Interface
Object Datatype
object is the datatype from system.object class. it can store any type of value.
here we cannot do mathmatical operations on object type data type unless we provide type
conversion explicitly.
here every time for storing value boxing and unboxing is done.
Encapsulation
encapsulation is the process of wrapping the data in single unit also known as data hiding.
it collects the the data members and memberfunctions into single unit called class.
the main purpose of the encapsulaton is prevent the alteration of data from outside.
Access Specifier
public
private
protected
only accessible within the class and child class of that class... non child class cannot access(by
creating instance).
internal
only accesible within the same project cannot access outside the project
protected internal
Variable or function declared protected internal can be accessed in the project in which it is
declared. It can also be accessed within a derived class in another project.
Array
array in C# is a group of similar types of elements that have contiguous memory location. In C#,
array is an object of base type System.Array.
In C#, array index starts from 0.
We can store only fixed set of elements in C# array.
single dimentional
multi dimentional
The multidimensional array is also known as rectangular arrays in C#. It can be two dimensional
or three dimensional. The data is stored in tabular form (row * column) which is also known as
matrix.
jagged array
In C#, jagged array is also known as "array of arrays" because its elements are arrays. The
element size of jagged array can be different.
arr[0]=new int[3]{2,3,4};
}
}
Collection inC#
limitation on Array
if we want to we have to create new array and copy the All values in that new Array,
collections
collection represent the group of object.by using collection we can perform various
opertions on object such as store ,delete, update ,retrive ,search and sort the object
1.ArrayList
2.Hashtable
Arraylist stores value in key/value cobination but keys are predefined or index.and it is hard to
remember the keys for each value.
to overcome this limitation hashtable is used which also stores value in key/value combination
but here the keys are user defined.
3. SortedList
A sorted list is a combination of an arraylist and a hash table. It contains a list of items that can
be accessed using a key or an index.
4. Stack
when we want to store and access the item in last in - first out manner stack is used.
push method is to add the item;
pop .method is used to access and remove last item
peek method is used to access last item without removing
5.Queue
when we want to store and access the item in first- in - first out manner queue is used.
enqueue method is to add the item;
dequeue method is used to remove and access first item from queue.
Generic collection
array are type safe means only specified type of values can be stored in arrays.but array have
fixed length.
and collections have variable length but they are not type safe.any type of value they can store.
to perform combination behavior of both like type safe like a array and auto resizing like a
collection c# provided a functionality known as Generic collection.
Type safe
Non generic collection takes type of values to be store as object.when we provide an value type
datatype it gets converted into object means boxing operation is performed.at the time of retrival
the object type get converted in value type means unboxing operation is performed.
in generic collection ,collections are type safe means no boxing and unboxing is required .hence
more efficient and faster than non genetic.
C# Generics
Generic is a concept that allows us to define classes and methods with placeholder. C#
compiler replaces these placeholders with specified type at compile time. The concept of
generics is used to create general purpose classes and methods.
to define generic class, we must use angle <> brackets. The angle brackets are used to declare
a class or method as generic type.
Property
Property is data member of a class like a field with get and set accessors.
if we declare variable as a public we do not have any control on this variable anyone can get
value and anyone can set value.
to overcome this problem we use property with get and set accessors.
we can only get value ,we can only set value aur both at same time.
it is the mechanism in which one class can consume all the members of another class except
private members by establishing parent- child relationship between the classes.
Parent class constructor must be accesible to child class .otherwise inheritance will not possible
because child class calls the parent class constructor implicitly.
in inheritance child class can access parent class members but parent class can not access
purely child class members.
we can initilize parent class variable by using child class instance.both will share same
memory .it is only pointer to child class.
System.object class is the default parent class for all the class.the members of object class
gettype(),gethashcode(),equals(),ToString() are accessible in any class
if constructor of parent class is parameterized then child class fails to call constructor implicitly
we have to called explicitly by using base keyword.
types of inheritance-
1. Single Level- one class inherites another class
3. Hirarchical- when more than one class inherites from single class
4.multiple -one class inherites from more than one class. // not supported
Polymorphism
Method overloading
if class define more than one method with same name but different parameter or number of
parameter it called as Method overloading.
method overloading
1.changing number of parameter
2.changing type of parameter
3 changing order of parameter.
Operator overloading
Operator overloading gives the ability to use the same operator to do various operations.
The + symbol between two integer values adds the values likewise the same symbol between
the to string objects concanates the string here the same symbol doing the different operations
is known as operator overloading.
It provides additional capabilities to C# operators when they are applied to user-defined data
types.
Method overidding
if a derrived class defines a same method as defined in its base class .it is known as
Method overidding.
method overriding is the approach of re-implementing the base class method in child class.
for overriding the base class method it needs permission from base class .(virtual keyword)
we need to use virtual keyword with base class method and override keyword with child class
method.
Method hiding
it is also approach of re-implementing the base class method in child class.but it not required a
permission from base class to implement the method of base class.
the methods which are are not written with virtual keyword can be re-implemented.
Abstraction
Abstraction is the prrocess of hiding the implementation details and showing only functionality to
the user.
it is done by using abstract class and interfaces.
Abstract class
the class declared with abstract keyword is called as abstract class.
Abstract class can contain abstract methods as well as non abstract methods.
to access non- abstract methods of abstract class all the abstract methods should be
implemented first in child class
abstract method -
the method without method body is called as abstract method.
parent class references which is created using child class instance can consume the overriden
members of child class but not purely child class members .
Interface-
interface is the way to achieve Abstraction.
interface is a also user defined type which contains only abstract methods .and every abstract
method should be implemented by the child class of that interface.
Exception Handling
types of errors
1.compile time
2.Run time
Errors occured at runtime due to various reasons ,like wrong inputs, change in datasource etc.
What is Exception ?
Exception is the object thrown at runtime because of runtime errors due to which abnormal
termination of the program occurs.
Exception Handling
it is the process of handling the runtime errors to prevent the abnormal termination of program
and maintain the normal flow of program.
We can dispay the user friendly error msg to end users to describe about the errors.
try block
{ statements that will cause the exception,
}
catch(<Exception class name> variable name)
{
statements that to be executed when there is error
)
Finally
{ statements that are must to be executed whether the exception in handled or not or
error occured or not
}
C# Break Statement
The C# break is used to break loop or switch statement. It breaks the current flow of the
program at the given condition. In case of inner loop, it breaks only inner loop.
C# Continue Statement
The C# continue statement is used to continue loop. It continues the current flow of the program
and skips the remaining code at specified condition. In case of inner loop, it continues only inner
loop.
C# Goto Statement
The C# goto statement is also known jump statement. It is used to transfer control to the other
part of the program. It unconditionally jumps to the specified label.
C# Comments
The C# comments are statements that are not executed by the compiler.
The comments in C# programming can be used to provide explanation of the code, variable,
method or class.
By the help of comments, you can hide the program code also.
C# Base
In C#, base keyword is used to access fields, constructors and methods of base class.
You can't use it inside the static method.
C# Sealed
We can't use 'this' in a static method because the keyword 'this' returns a reference to the
current instance of the class containing it. Static methods (or any static member) do not belong
to a particular instance. They exist without creating an instance of the class and are called with
the name of a class, not by instance, so we can’t use this keyword in the body of static Methods.
C# Strings
In C#, string is an object of System.String class that represent sequence of characters. We can
perform many operations on strings such as concatenation, comparision, getting substring,
search, trim, replacement etc.
Strings are immutable. we cannot modify the string. if we want to then every time it creates a
new copy of string with specified operation.
Structs
Structs is also user defined type like a class.but struct are use to represent entity with smaller
volume of data.
as struct is value type than reference type the memory allocation done on stack memory.
structs can not be inherited,or cannot be inherit from another class.
It is useful if you have data that is not intended to be modified after creation of struct.
we can not initialize variable at the time of declaration.we can initialize thru constructor or by
reffering it thru instance of struct.
default constructor can not be define explicitly it is always implicit what we can define is
parameterized constructor.
eg. All objects in c# libraries which are reference type are classes. e.g String ,Object
and all comes inside value type are structs.e.g Int ,float, Bool.
enums
it is also user defined type but it is value type.it can be declared in namespace as well as inside
class and struct also.
when we want to user to select only specified values or names from the list we use the enum to
store the values or elements.
e.g
if we want change the background color of console application then it supports only few colors
as there are many colors are present.
so enum is used here to specify user these few colors which are supported that user can select
other than writing any other colors name.
Console.BackgroundColor = ConsoleColor.Red;
Indexers
if we define indexers in class the class will starts behaving like a virtual array we can access the
members of the class using index position.
inderxer perform a combination behavior of array and and properties.here the index can user
defined
{ get{}set{}}
Delegates
delegate hold the reference of method and then calls method for execution.
delegate is also user define type of reference type.that can be declare inside namespace as
well as class.
the return type and parameters of delegate should match the return type and parameters of
methods which is to be bound with delegate.
both are used for pass the variable to the method by reference.change in reference of variable
will also modify the actual value.
only deffrence is ref parameter is must be initialize before passing it to the method.
and out parameter is need not to be initialize before passing to method as it is initialize
is and as keyword
as keyword is used when we want to typecast one object into another type.
string s = obj as string;
. Difference between the Equality Operator (==) and Equals() Method in C#?
Assembly is the single unit of deployment of a .net application. It can be a dll or an exe.
Private Assembly: The dll or exe which is sole property of one application only. It is generally
stored in application root folder
GAC is simply C:\Windows\Assembly folder where you can find the public assemblies/dlls of all
the softwares installed in your PC.
Satellite Assembly.
A Satellite Assembly contains only static objects like images and other non-executable files
required by the application.
in C#, reflection is a process to get metadata of a type at runtime. The System.Reflection
namespace contains required classes for reflection
Metadata is information about the assemblies, modules, and types that constitute .NET
programs