TEchMyEducation

easy tech learning...

.Net interview question and answers

No comments

.Net interview question and answers

Today we will learn about the most important dot net interview questions and answers.
Oops is a programming model where programs are organized as objects and data.


Constant
Constant variables must have to be defined at declaration time and after declaring any value to constant field it can not be modified. Constant is static by default so it can not be defined static manually by the programmer.
Assigning value at declaration is a must else it will give a compile-time error. This is the reason it is also called a compile-time constant. After declaring the value of const at declaration the value of const cant be changed or it is unmodified or unchanged in the whole program.

Points to Remember
  • Can't be declared static
  • Can't be modified or changed
  • Can be of any type of Access Modifier
  • Declared at the time of declaration
Readonly
A Readonly field can be initialized at declaration time or under the constructor of the same class. The value of Readonly can also be changed at runtime or assign a value to it at runtime (but in a non-static constructor only).
Points to Remember
  • Generally public

Static 
The static keyword is used to declare a static member. If we are declaring a class as a static class then, in this case, all the class members must be static too. The static keyword mostly used with classes, fields, operators, events, methods.
Points to Remember
  • By default it is private
  • Can be parameterized or public too
  • If it's applied to a class then all the class members need to be static
--------------------------------------------------------------------------------------------------------------------------
2. Sealed Class is used to restrict the inheritance feature from Oops. Once the class is defined as sealed class it can not be inherited.

//Sealed class
                     sealed class SealedclassTest
                     {
                         //
                      }
--------------------------------------------------------------------------------------------------------------------------

  • Yealed keyword: Yealed keyword is used for custom iteration without temp collection.

And yealed keyword is also used for stateful iteration i.e it can preserve the previous state.

IEnumerable and IQueryable are stateless.
--------------------------------------------------------------------------------------------------------------------------

  • ==  vs .Equals() :

== compares if the object reference is the same.
.Equals method compares if the contents are the same.

Example:
class Program
{
    static void Main(string[] args)
    {
        Object o = '.Net Interview Questions';
        Object o1 = o;
        console.WriteLine(o == o1);
        console.WriteLine(o.Equals(o1));
        console.ReadLine();
        /* 
            Output:
            True
            True
        */
    }
}

class Program
{
    static void Main(string[] args)
    {
        Object o = '.Net Interview Questions';
        Object o1 = new string('.Net Interview Questions');
        console.WriteLine(o == o1);
        console.WriteLine(o.Equals(o1));
        console.ReadLine();
        /* 
            Output:
            False
            True
        */
    }
}
--------------------------------------------------------------------------------------------------------------------------
3. Q. Can you store different types in an array in C#?
 Answer: Yes if you create an object array in C# because object type is base type of all the types in .Net.
         ex. object[] array = new object[2];
                        array[0]=5;
                        array[1]="string value";

=> Another alternative is to use the ArrayList class that is present in System. Collections namespace.
          ex. ArrayList array = new ArrayList();
                       array.add(1);
                       array.add("string value");
--------------------------------------------------------------------------------------------------------------------------
4. Jagged Array: An Array of the array is called a jagged array.
                             ex. string[][] jaggedarray = new string[][];
--------------------------------------------------------------------------------------------------------------------------
5. What we do in C# if we want to restrict instantiate/ create the object of the class.
    (i)  We create a class as an Abstract class
    (ii)  We add a private constructor in class so class can't be instantiated.
--------------------------------------------------------------------------------------------------------------------------
6. Abstract class: In short, we would create an abstract class, when we want to move the common functionality of 2 or more related classes into a base class and when we don't want that base class to be instantiated.
--------------------------------------------------------------------------------------------------------------------------
7. Serialization: Serialization is the process of converting an object into a stream of bytes to store the object in a file, database.
it can be transferred across the network. Its main purpose is to save the state of the object so that it can be recreated when needed

Types 
  1. Binary Serialization
  2. XML Serialization
  3. JSON Serialization
--------------------------------------------------------------------------------------------------------------------------
Var vs dynamic

There are two types of language statically and dynamically.
A static type language validates for syntax or any errors at compile time while dynamically typed language validates for any syntax or error at runtime,
for example, c# and java are statically typed languages and javascript is dynamically typed language.

C# was previously considered as a static language because all the code written in c# was validated at compile time itself. But with the introduction of dynamic keywords in .Net Framework 4.0, it becomes a dynamically typed language.

Difference
Type inference of variables
Var is a statically typed variable which means the data type of these variables checks at compile time or it validates the syntax or checks for any errors at compile time while dynamic type validates for syntax and checks for errors at runtime.

Introduced
Var was introduced in c# 3.0
the dynamic was introduced in c# 4.0

Initialization of variables
var type of variables is required to be initialized at the time of declaration or else they encounter a compile-time error.
the dynamic type of variable is not required to be initialized at the time declaration.

Changing the type of value assigned
var does not allow changing the type of value assigned to be changed after it is assigned.
This means if we assign an integer value to var then we cannot assign a string value to it because on assigning the integer value it will be treated as an integer type.

ex. var amount=100
amount="Hundred" //this will give compile-time error

dynamic allows a type of value to be changed initially.
ex. dynamic Dynamicamount=100;
Dynamicamount="Hundred"


usage
a dynamic variable can be used to create properties and return values from a function.
var variable cannot be used for properties. They can only be used as a local variable in a function.

--------------------------------------------------------------------------------------------------------------------------
Difference between Abstract Class and Interface
ABSTRACT CLASS
INTERFACE
It contains both declaration and definition part.
It contains only a declaration part.
Multiple inheritances are not achieved by an abstract class.
Multiple inheritances are achieved by interface.
It contains constructor.
It does not contain a constructor.
It can contain static members.
It does not contain static members.
It can contain different types of access modifiers like public, private, protected, etc.
It only contains public access modifier because everything in the interface is public.
A class can only use one abstract class.
A class can use multiple interface.
An abstract class can contain methods, fields, constants, etc.
The interface can only contain methods .

--------------------------------------------------------------------------------------------------------------------------



. C# Multiple Inheritance: C# doesn't allow multiple inheritances to avoid the ambiguity caused by it.
One of the examples of such a problem is the diamond problem that occurs in multiple inheritances.
We will discuss this problem with the help of the above diagram: which shows multiple inheritances as class D extends both classes B&C.
Now let's assume we have a method in Class A and Class B, and Class C overrides that method in their own way, Wait here the problem comes because Class D is inheriting both Classes B&C , So if D wants to use the same method which method would be called (the overridden method of Class B or the overridden method of Class C.
The ambiguity that's the main reason why c# doesn't support multiple inheritances.



No comments :

Post a Comment