A Class is an encapsulation of properties(member variables) and methods that are used to represent a entity. Class is a data structure that brings all the member variables together in a single unit.
An Object in an instance of a Class. Technically, it is just a block of memory allocated that can be stored in the form of Variables, Array or a Collection.
C# is a programming language developed by Microsoft, it is a general-purpose, multi-paradigm programming language with features of strong typing, imperative, declarative, functional, generic, object-oriented, and component-oriented programming disciplines.
The following are the fundamental concepts of Object Oriented Programming:
- Encapsulation â€“ It is process of binding data and functions(methods) within on unit. It also hide the internal state of an object from outside.
- Abstraction â€“ It is a process of displaying only essential information and hide the irrelevant characteristics.
- Inheritance â€“ It is a mechanism where one class acquire the properties and behaviour of another class to achieve reusability.
- Polymorphism â€“ Polymorphism means more than one forms. It is achieved by having multiple methods with the same name but different signature.
Code which is executed by CLR (ommon Language Runtime) in .NET Framework is called managed code because CLR handle security, manage unused memory ,debugging and so on. The code which is developed by other Framework not .NET Framework is called unmanaged code.
An Interface is a class with no implementation. The only thing that it contains is the declaration of methods, properties, and events.
The different types of class in C# are:
Partial class – Partial class is special features of c#,it provides ability to implement a single class functionality to multiple .cs files. Partial class is create by Partial keyword.
Sealed class – Sealed class is a class that cannot be inherited by other classes. The sealed modifier is used to define a class as sealed in c#.
Abstract class – Abstract class cannot be instantiated. Abstract class can only be inherited. It should contain at least one method. It is denoted by the keyword abstract.
Static class – Static class can not be instantiated.Its member variables and methods can be static only. It is defined using static keywords.
C# compiler compiles the C# code into MSIL code (Microsoft Intermediate Language). MSIL code can not executed directly by CPU so CLR Compiles the MSIL code to native code by using the Just-In-Time (JIT) compiler (or JITter).
Virtual methods have method definition and derived classes have option of overriding it, but Abstract methods do not have implementation and derived classes have to override the methods.
Exception handling in C#:
- try – Contains a block of code that can throw an exception.
- catch – catch block handle when an exception is thrown.
- finally – Finally block get executed whether exception occurred or not.
- throw – Throw is a keyword used to trigger an exception when a error occurs in program.
There are three ways to pass parameters to a method −
Value Parameters − The argument value are copy to formal parameters of function. There are no effect on value of argument if change the value of function parameter inside function body.
Reference Parameters − Copy the reference of an argument into the formal parameter.In this case if function parameters value will change inside function body ,argument value will change.
Output Parameters − It helps in returning more than one value.
Boxing- When a value type converted to object type is called boxing.
Unboxing- When a object type converted to value type is called unboxing.
Reflection provides metadata (assemblies, modules and types) of objects type. Reflection helps to dynamically create an instance of a type, bind the type to an existing object, or get the type from an existing object and invoke its methods or access its fields(name etc) and properties.
public class MyClass
public virtual int Add(int numb1, int numb2)
return numb1 + numb2;
public virtual int Subtract(int numb1, int numb2)
return numb1 - numb2;
static void Main(string args)
MyClass oMyClass = new MyClass();
Type oMyType = oMyClass.GetType();
MethodInfo oMyMethodInfo = oMyType.GetMethod("Subtract");
Console.WriteLine("nType information:" + oMyType.FullName);
Console.WriteLine("nMethod info:" + oMyMethodInfo.Name);
Uses of Reflection in C#-
- To detect dependencies of an assembly.
- Determining the contents of an unknown assembly or object.
- Debugging - exploring a list of the loaded assemblies, their references, current methods etc.
- In serialization, it is used to serialize and de-serialize objects
- To confirm location types such as an interface, derive from base, abstract class search member by attributes.
Generics are the most powerful features of c#. Generic allows programmer to write reusable code and methods for all data types. It improve type safety and performance.
Difference between String and Stringbuilder:
- Defined in System namespace.
- Immutable(readonly) instance.
- Performance degrades when continuous change of value occures.
- Thread safe.
- Defined in System.Text namespace.
- Mutable instance.
- Btter perfomance because new changes are made to existing instance.
- Not thread sefe.
The following are the IDEâ€™s used for C# development â€“
- Visual Studio Express (VCE)
- Visual Studio (VS)
- Visual Web Developer
Sealed class is a class that cannot be inherited by other classes. The sealed modifier is used to define a class as sealed in c#.
- Array stores the values or elements of homogenous data type but arraylist stores values or elements of heterogeneous datatypes.
- Array uses the fixed length but arraylist does not uses fixed length like array.
- Dispose method uses interface IDisposable and it will free up both managed and unmanaged codes like – database connection, files etc.
- Finalize method is called by garbage collector automatically and cannot be called from the code.
Yes we can have only try block without catch block but we have to have finally block.