What is object-oriented programming (OOP)?
OOP
is a technique to develop logical modules, such as classes that contain
properties, methods, fields, and events. An object is created in the
program to represent a class. Therefore, an object encapsulates all the
features, such as data and behavior that are associated to a class. OOP
allows developers to develop modular programs and assemble them as
software. Objects are used to access data and behaviors of different
software modules, such as classes, namespaces, and sharable assemblies.
What is C#?
C# is an object oriented, type
safe and managed language that is compiled by .Net framework to generate
Microsoft Intermediate Language.Why to use “using” in C#?
“Using” statement calls – “dispose” method internally, whenever any exception occurred in any method call and in “Using” statement objects are read only and cannot be reassignable or modifiable.
---------------------------------- namespace ----------------------------------
Explain namespaces in C#?
Namespaces are containers for the classes. We will use namespaces for grouping the related classes in C#. “Using” keyword can be used for using the namespace in other namespace.
Mention the assembly name where System namespace lies in C#?
Assembly Name – mscorlib.dll
---------------------------------- Class ----------------------------------
Explain sealed class in C#?
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.
---------------------------------- Object ----------------------------------
What is an object?
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.
Explain object pool in C#?
Object pool is used to track the objects which are being used in the code. So object pool reduces the object creation overhead.
Explain object pool in C#?
Object pool is used to track the objects which are being used in the code. So object pool reduces the object creation overhead.
---------------------------------- Array ----------------------------------
What is an Array?
An array is a collection of related instance
either value or reference types. Array posses an immutable structure in
which the number of dimensions and size of the array are fixed at
instantiation.
C# Supports Single, Mult dimensional and Jagged Array.
Single Dimensional Array: it is sometimes called vector array consists of single row.
Multi-Dimensional Array: are rectangular & consists of rows and columns.
Jagged Array: also consists of rows & columns but in irregular shaped (like row 1 has 3 column and row 2 has 5 column). 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.
What is an ArrayList?
An arraylist is similar to an array but it doesn’t have a fixed size.
What is BitArray?
The BitArray collection is a composite of bit
values. It stores 1 or 0 where 1 is true and 0 is false. This collection
provides an efficient means of storing and retrieving bit values.---------------------------------- Hashtable ----------------------------------
Explain Hashtable in C#?
It is used to store the key/value pairs based on hash code of the key. Key will be used to access the element in the collection. For example,
Hashtable myHashtbl = new Hashtable();
myHashtbl.Add("1", "TestValue1");
myHashtbl.Add("2", "TestValue2");
---------------------------------- Try, Catch, Finally ----------------------------------
Why to use “finally” block in C#?
“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.
Can we have only “try” block without “catch” block in C#?
Yes we can have only try block without catch block.
Can multiple catch blocks be executed?
No, Multiple catch blocks can’t be executed. Once the proper catch code executed, the control is transferred to the finally block and then the code that follows the finally block gets executed.
Do we get error while executing “finally” block in C#?
Yes. We may get error in finally block.
What is the difference between “throw ex” and “throw” methods in C#?
- “throw ex” will replace the stack trace of the exception with stack trace info of re throw point.
- “throw” will preserve the original stack trace info
In try block if we add return statement whether finally block is executed in C#?
Yes. Finally block will still be executed in presence of return statement in try block.
---------------------------------- static, public, void, protected, private ----------------------------------
What are the differences between static, public and void in C#?
- Static classes/methods/variables are accessible throughout the application without creating instance. Compiler will store the method address as an entry point.
- Public methods or variables are accessible throughout the application.
- Void is used for the methods to indicate it will not return any value.
Explain “static” keyword in C#?
“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.
Explain access modifier – “protected internal” in C#?
“protected internal” can be accessed in the same assembly and the child classes can also access these methods.
No. We can’t use “this” in static method.
Can a private virtual method be overridden?
No, because they are not accessible outside the class.
---------------------------------- delegate ----------------------------------
What you mean by delegate in C#?
Delegates in c# are type safe objects, which are used to hold reference of one or more methods. Delegate is used to represent the reference of the methods of same return type and parameters.
Whenever we want to create delegate methods we need to declare with delegate keyword and delegate methods signature should match exactly with the methods which we are going to hold like same return types and same parameters otherwise delegate functionality won’t work if signature not match with methods.
Single Cast Delegates
public delegate int DelegatSample(int a,int b);
public class Sampleclass
{
public int Add(int x, int y)
{
return x + y;
}
public int Sub(int x, int y)
{
return x - y;
}
}
class Program
{
static void Main(string[] args)
{
Sampleclass sc=new Sampleclass();
DelegatSample delgate1 = sc.Add;
int i = delgate1(10, 20);
Console.WriteLine(i);
DelegatSample delgate2 = sc.Sub;
int j = delgate2(20, 10);
Console.WriteLine(j);
}
}Output:
Add Result : 30
Sub Result : 10Multi Cast Delegates:
Multi
cast delegate is used to hold address of multiple methods in single delegate. To
hold multiple addresses with delegate we will use overloaded += operator and if
you want remove addresses from delegate we need to use overloaded operator -=
public delegate void MultiDelegate(int a,int b);
public class Sampleclass
{
public static void Add(int x, int y)
{
Console.WriteLine("Addition
Value: "+(x + y));
}
public static void Sub(int x, int y)
{
Console.WriteLine("Subtraction
Value: " + (x - y));
}
public static void Mul(int x, int y)
{
Console.WriteLine("Multiply
Value: " + (x * y));
}
}
class Program
{
static void Main(string[] args)
{
Sampleclass sc=new Sampleclass();
MultiDelegate del = Sampleclass.Add;
del += Sampleclass.Sub;
del += Sampleclass.Mul;
del(10, 5);
Console.ReadLine();
}
}Output:
Addition Value : 15
Subtraction Value : 5
Multiply Value : 50
What is the use of Delegates?
What are the types of delegates in C#?
Below are the uses of delegates in C# -
- Single Delegate
- Multicast Delegate
- Generic Delegate
What are the three types of Generic delegates in C#?
Below are the three types of generic delegates in C# -
- Func
- Action
- Predicate
---------------------------------- Variable Types ----------------------------------
The variables in C#, are categorized into the following types:
- Value types
- Reference types
- Pointer types
Value Type:Value type variables can be assigned a value directly. They are derived from the class System.ValueType.
When you declare an int type, the system allocates memory to store the value.
Examples are bool, byte, int, char, decimal, double, long, sbyte, short, uint, ulong, ushort and float, which stores numbers, alphabets, and floating point numbers, respectively.
Reference Type:
The reference types do not contain the actual data stored in a variable, but they contain a reference to the variables.
Example of built-in reference types are: object, dynamic, and string.
User-defined reference types are: class, interface, or delegate.
Object Type:
The Object Type is the ultimate base class for all data types in C# Common Type System (CTS). Object is an alias for System.Object class. The object types can be assigned values of any other types, value types, reference types, predefined or user-defined types. However, before assigning values, it needs type conversion.
Dynamic Type:
You can store any type of value in the dynamic data type variable. Type checking for these types of variables takes place at run-time.
Syntax:
dynamic <variable_name> = value;
For example,
dynamic d = 20;
Dynamic types are similar to object types except that type checking for object type variables takes place at compile time, whereas that for the dynamic type variables takes place at run time.
Pointer Type:
Pointer type variables store the memory address of another type.
Syntax:
type* identifier;
For example:
char* cptr;
int* iptr;
---------------------------------- boxing, unboxing ----------------------------------
When a value type is converted to object type, it is called boxing
when an object type is converted to a value type, it is called unboxing.
object obj;
obj = 100; // this is boxing
---------------------------------- Type conversion / Type Casting ----------------------------------
Type conversion is converting one type of data to another type. It is also known as Type Casting. In C#, type casting has two forms:
- Implicit type conversion - These conversions are performed by C# in a type-safe manner. For example, conversions from smaller to larger integral types and conversions from derived classes to base classes.
- Explicit type conversion - These conversions are done explicitly by users using the pre-defined functions. Explicit conversions require a cast operator.
---------------------------------- Other ----------------------------------
Explain the types of comments in C#?
Below are the types of comments in C# -
- Single Line Comment Eg : //
- Multiline Comments Eg: /* */
- XML Comments Eg : ///
What are value types in C#?
Below are the list of value types in C# -
- decimal
- int
- byte
- enum
- double
- long
- float
What are reference types in C#?
Below are the list of reference types in C# -
- class
- string
- interface
- object
What is the difference between “constant” and “readonly” variables in C#?
- “Const” keyword is used for making an entity constant. We cannot
modify the value later in the code. Value assigning is mandatory to
constant variables.
Eg: const string _name = "Test"; - “readonly” variable value can be changed during runtime and value to readonly variables can be assigned in the constructor or at the time of declaration.
What is the difference between “dispose” and “finalize” variables in C#?
- Dispose - This method uses interface – “IDisposable” interface and it will free up both managed and unmanaged codes like – database connection, files etc.
- Finalize - This method is called internally unlike Dispose method which is called explicitly. It is called by garbage collector and can’t be called from the code.
What are circular references?
What is lock statement in C#?
Lock ensures that one thread does not enter a
critical section of code while another thread is in the critical
section. If another thread attempts to enter a locked code, it will
wait, block, until the object is released.- Structure is stored within stack memory and Class is stored within heap memory.
- In Structure we can not define parameter less constructor but in class can do it.
- In structure we can not initialize the data member at declaration but in class can do it.
- In structure inheritance is not possible(one structure can not inherited to another structure )but in class it is possible.
- We can not declare virtual override and abstract keyword with structure but in class it is possible.
- Class is Reference type but structure is value type.
- We can use Destructor in Class but not in Structure.
No comments:
Post a Comment