Below are some of the features supported in C# -
An object is an instance of a class through which we access the methods of that class.
"New" keyword is used to create an object.
A class that creates an object in memory will contain the information about the methods, variables and behavior of that class.
In simple Words A class is a concept.
public class TechTrick
{
}
TechTrick TT = new TechTrick();
1.Encapsulation is a process of binding or wrapping the data and the codes that operates on the data into a single entity. This keeps the data safe from outside interface and misuse.
2. Abstraction is the concept of hiding irrelevant details. In other words make complex system simple by hiding the unnecessary detail from the user.
Abstraction solves the problem in the design level. Where as Encapsulation solves the problem in the implementation level.
The method of constructing one class from another is called Inheritance. The derived class inherits all the properties and methods from the base class and it can add its own methods also.
public class A
{
}
public class B : A
{
}
The word Polymorphism means having many forms. Generally, polymorphism occurs when there is a hierarchy of classes and they are related by inheritance.
In other words, "Many forms of a single object is called Polymorphism."
There are two types of Polymorphism:
int arr[] = new int[10]
ArrayList arrList = new ArrayList();
The array which has elements of type array is called jagged array. The elements can be of different dimensions and sizes. We can also call jagged array as Array of arrays.
int[][] Marks = new int[5][];
Marks[0] = new int[2];
Marks[1] = new int[4];
Marks[2] = new int[5];
Marks[3] = new int[8];
Marks[4] = new int[10];
Marks[0] = new int[] { 4, 5, };
Marks[1] = new int[] { 2, 0, 2, 4};
Marks[2] = new int[] { 1,2, 0, 2, 4};
Marks[3] = new int[] { 1, 0, 2, 4, 6, 45, 67, 78 };
Marks[4] = new int[] { 34, 54, 67, 87, 78 ,1, 0, 2, 4, 6};
const int RollNo = 11;
readonly int RollNo = 11;
"Static" keyword can be used for declaring a static member. If the class is made static then all the members of the class are also made static. If the variable is made static then it will have a single instance and the value change is updated in this instance.
Sealed class is used to prevent the class from being inherited from other classes. So "sealed" modifier also can be used with methods to avoid the methods to override in the child classes.
sealed class SealedClass
{
}
Structs are value-type variables and classes are reference types. Structs stored on the stack, causes additional overhead but faster retrieval. Structs cannot be inherited.
struct TechTrick
{
string MyProperty { get; set; }
}
class TechTrick
{
string MyProperty { get; set; }
}
string s = string.Empty;
StringBuilder sb = new StringBuilder ();
if (someObject is StringBuilder)
StringBuilder b = someObject as StringBuilder;
"out" parameter can be passed to a method and it need not be initialized where as "ref" parameter has to be initialized before it is used.
int x;
TechTrick(out x); // OK
int y;
TechTrick(ref y); // Error: y should be initialized before calling the method
enum Days { Sun, Mon, tue, Wed, thu, Fri, Sat };
Partial classes concept added in .Net Framework 2.0 and it allows us to split the business logic in multiple files with the same class name along with "partial" keyword.
public interface ISignup
{
//ISignup related function
}
public interface ISignIn
{
//ISignIn related function
}
//UserSignup.cs file
public partial User : ISignup, ISignIn
{
//implements ISignup interface
}
//UserSignIn.cs file
public partial User
{
//implements SignIn interface
}
When we want to give permission to a derived class to override a method in base class, Virtual keyword is used.
public virtual void show()
{
Console.WriteLine("Hello: Base Class!");
Console.ReadLine();
}
A constructor is a member function with the same name as its class. The constructor is invoked whenever an object of its associated class is created.It is called constructor because it constructs the values of data members of the class.
class Sum
{
int a, b;
public Sum() //default contructor
{
a = 50;
b = 25;
}
}
Destructor is a special method that get invoked/called automatically whenever an object of a given class gets destroyed. Main idea behind using destructor is to free the memory used by the object.
class destructor
{
~destructor(){}
}
Garbage Collection is a process of releasing memory automatically occupied by objects which are no longer accessible.
Early binding and late binding are the concept of polymorphism. There are two types of polymorphism in C#.
A base class is a class that is used to create, or derive other classes. Classes derived from a base class are called child classes, subclasses or derived classes. A base class does not inherit from any other class and is considered parent of a derived class.
The abstract keyword is used to declare abstract class.
Example:public abstract class Shape
{
public abstract void draw();
}
public interface Drawable
{
void draw();
}
"Finally" block will be executed irrespective of exception. So while executing the code in try block when exception is occurred, control is returned to catch block and at last "finally" block will be executed. So closing connection to database / releasing the file handlers can be kept in "finally" block.
Variable types does not hold null values so to hold the null values we have to use nullable types. So nullable types can have values either null or other values as well.
int? num1 = null;
int? num2 = 45;
double? num3 = new double?();
Nullable Coalescing Operator can be used with reference types and nullable value types. So if the first operand of the expression is null then the value of second operand is assigned to the variable. For example,
double? myFirstno = null;
doudoubleble mySecno;
mySecno = myFirstno ?? 10.11;
Method overloading is creating multiple methods with the same name with unique signatures in the same class. When we compile, the compiler uses overload resolution to determine the specific method to be invoke.
static void Foo(int x)
Console.WriteLine("Foo(int x)");
}
static void Foo(string y)
{
Console.WriteLine("Foo(string y)");
}
static void Main()
Foo("text");
}
Delegates are type safe pointers unlike function pointers as in C++. Delegate is used to represent the reference of the methods of some return type and parameters
Below are the list of uses of delegates in C# -
Thread is an execution path of a program. Thread is used to define the different or unique flow of control. If our application involves some time consuming processes then it’s better to use Multithreading,which involves multiple threads.
When we want to transport an object through network then we have to convert the object into a stream of bytes. The process of converting an object into a stream of bytes is called Serialization. For an object to be serializable, it should implement ISerialize Interface. De-serialization is the reverse process of creating an object from a stream of bytes