Difference between ref and out parameters




Difference between ref and out parameters

Ref:
The ref keyword is used for passing parameter by reference
Need to be initialized before passing

Out:
The out paramter is used when method want to return multiple values
It is not mandatory to initialize paramter before passing

Ref and out in method overloading

Both ref and out cannot be used in method overloading simultaneously. However, ref and out are treated differently at run-time but they are treated same at compile time. Hence methods cannot be overloaded when one method takes a ref parameter and other method takes an out parameter. The following two methods are identical in terms of compilation.




class MyClass
{
public void Test(out int a) // compiler error “cannot define overloaded”
{
// method that differ only on ref and out”
}
public void Test(ref int a)
{
// method that differ only on ref and out”
}
}

However, method overloading can be done, if one method takes a ref or out argument and the other method takes simple argument. The following example is perfectly valid to be overloaded.

class MyClass
{
public void Test(int a)
{

}
public void Test(out int a)
{
// method differ in signature.
}
}




Design Patterns

Design patterns:
Design patterns represent the best practices used by experienced object-oriented software developers. Design patterns are solutions to general problems that software developers faced during software development.

Gang of Four (GOF)
Design patterns gained popularity in computer science after the book Design Patterns: Elements of Reusable Object-Oriented Software was published in 1994 by the so-called “Gang of Four” (Gamma et al.), which is frequently abbreviated as “GoF”.

As per them..

  • Program to an interface not an implementation
  • Favor object composition over inheritance

Types of Design Patterns

  1. Creational Patterns:
    These design patterns provide a way to create objects while hiding the creation logic, rather than instantiating objects directly using new opreator. This gives program more flexibility in deciding which objects need to be created for a given use case.
  2. Structural Patterns:
    These design patterns concern class and object composition. Concept of inheritance is used to compose interfaces and define ways to compose objects to obtain new functionalities.
  3. Behavioral Patterns:
    These design patterns are specifically concerned with communication between objects.

Creational Patterns
Singleton
Factory method
Abstract factory
Builder
Prototype
Object Pool
Lazy initialization
Multiton
Prototype

Structural patterns
Adapter
Facade
Bridge
Composite
Decorator
FlyWeight
Proxy
Marker
Module
Twin

Behavioral patterns
Strategy
State
Observer or Publish/Subscribe
Chain of responsibility
Command
Interpreter
Iterator
Mediator
Null Object
TemplateMethod
Visitor
Blackboard
Memento
Servant
Specification

Details to be continued…

Citibank interview questions for experienced – dotnet

This questions were asked in Citibank Pune 2016.

  1. Tell me about yourself?
  2. Why to use abstract classes?
  3. What are interfaces?
  4. What is polymorphism?
  5. ref vs out
  6. Why c# supports single inheritance
  7. Why to use Var
  8. Design patterns used in project
  9. How to create singleton class?
  10. Is it thread safe?
  11. Why to use object for lock? Can we use integer class?
  12. lock vs Monitor vs Mutex?
  13. Explain MVVM pattern
  14. Prism Framework
  15. How to get second higest salary of an employee
  16. Real time example of Mutex?

Varian Medical System Interview Questions for experienced – Dotnet

1.  What is difference between Abstract class and Interfaces
2. What is virtual and override
3. What is Dependency injection? Use in your project
4. What is prism? Any other framework that you used like prism.
5. What is Extensible framework?
6. How to load assembly without using reflection
7. Have you created any custom controls? How to create them?
8. What are Dependency Properties?
9. How the hierarchy/Precedence is picked.. Say Style has background color = Red and control background color is set to white.
10. What are content template?
11. How to add edit button inside List Box?

Cognizant Interview Questions for experienced – Dotnet

Cognizant follows systematic interview process. They calls candidate between required experience range for dotnet, java and other technologies.
Process:

  1. Once you reach there office. They collect your resume along with Photo.
  2. You need to mention technology.
  3. Calls candidate based on in time.
  4. 1-2 rounds of technical interview.

Interview questions for dotnet candidate:

They divided the interview into 4 categories:

  1. OOPS
  2. .NetFramework and C#
  3. WPF
  4. DataBase and Agile

OOPs
Difference between Abtraction vs Encapsulation
What is Polymorphism
Interface vs Abstract.When to use what
Same method in interface, how to implement and call it.
Use of static constructor?
virtual and override keyword

C#
what is use of new keyword
when to use yield
Delegates VS Events
what are Generics
Extensionmethod – with example
Finalize vs Dispose- which handles managed/unmanaged code.

WPF
What are Dependency Properties?
Attached properties
Converters
Static resource vs Dynamic resource
There is tabcontrol and we have 2 different controls inside it. Now how to load control at run-time. -DataTemplate Selector
DataTemplete vs Control Template
Type of Bindings

Agile and Database
What is Scrum
Estimation
Retrospective meeting
SCrum of Scrum?

What is Self Join? SQL query for it.

Interfaces in C#

Interfaces:

  • Interfaces are contracts that defines the signature of functionality.
  • Interfaces are useful when creating hierarchy of unrelated classes.

Interface Example:


public interface InterfaceA
{
void Display();
}

public class DerivedA : InterfaceA
{
  public void Display()
 {
    Console.WriteLine("Display Method DerivedA");
 }

 void InterfaceA.Display()
 {
   Console.WriteLine("Display Method InterfaceA DerivedA");
 }
}

There is one Display method and other Display method is explicitly implemented. This works fine and does not throw error.

Two interfaces with same method name:

public interface InterfaceA
{
void Display();
}

public interface InterfaceB
{
void Display();
}

In such case we need to explicitly define the method

public class DerivedA : InterfaceA, InterfaceB
{
public void Display()
{
Console.WriteLine("Display Method DerivedA");
}

void InterfaceA.Display()
{
  Console.WriteLine("Display Method InterfaceA DerivedA");
}

void InterfaceB.Display()
{
  Console.WriteLine("Display Method InterfaceB DerivedA");
}
}

Main Method:

static void Main(string[] args)
{

InterfaceA AObj = new DerivedA();
AObj.Display();

InterfaceB BObj = new DerivedA();
BObj.Display();

Console.ReadLine();
}

Result:

Display Method InterfaceA DerivedA
Display Method InterfaceB DerivedA

Modifying interface/Adding new method to interface:

Ideally interface should not change after it is used. But if there is situation where we need to add new method to interface as example below.  One of the way is mentioned below:

Let say there is interfaceA and we want add new method Print()

public interface InterfaceA
{
void Display();
}

InterfaceA is being used. User wants to add new method to it.

Create new interface with method to be implemented and inherit it from original interface.

public interface InterfaceB : InterfaceA
{
void Print();
}

Now use newly created interface were needed. Don’t change other class implementation.

When to use interfaces:

  • If the functionality you are creating will be useful across a wide range of disparate objects, use an interface.
  • If you are designing small, concise bits of functionality, use interfaces.
  • Interfaces cannot be changed once created. If a new version of an interface is required, you must create a whole new interface.

Polymorphism Example

Inheritance without polymorphism

public class BaseA
{
	public void Display()
	{
	 Console.WriteLine('Display Method BaseA');	 
	}
}

public class DerivedA : BaseA
{
//Warning InheritanceExample.DerivedA.Display()' hides inherited member 'InheritanceExample.BaseA.Display()'.
//Use the new keyword if hiding was intended.
	public void Display()
	{
	Console.WriteLine('Display Method DerivedA');
	}
}

public class DerivedB : BaseA
{
	public void Display()
	{
	Console.WriteLine('Display Method DerivedB');
	}
}

Main Method

static void Main(string[] args)
{
	BaseA AObj = new BaseA();
	AObj.Display();

	DerivedA ADerObj = new DerivedA();
	ADerObj.Display();

	BaseA AObj1 = new DerivedA();
	AObj1.Display();
	Console.ReadLine();
}


<strong>Output</strong>

Display Method BaseA
Display Method DerivedA
Display Method BaseA


//Error 1 Cannot implicitly convert type BaseA' to DerivedA'.
//An explicit conversion exists (are you missing a cast?)

DerivedA AObj2 = new BaseA();
AObj2.Display();

//Unable to cast object of type BaseA' to type DerivedA'.

DerivedA AObj2 = (DerivedA)new BaseA();
AObj2.Display();

Using new keyboard:

public class BaseA
{
public virtual void Display()
	{
	Console.WriteLine('Display Method BaseA');
	}
}

public class DerivedA : BaseA
{
	public new virtual void Display()
	{
	Console.WriteLine('Display Method DerivedA');
	}
}

public class DerivedB : BaseA
{
	public new virtual void Display()
	{
	  Console.WriteLine('Display Method DerivedB');
	}
}

Inheritance using polymorphism

public class BaseA
{
	public virtual void Display()
	{
	  Console.WriteLine('Display Method BaseA');
	}
}

public class DerivedA : BaseA
{
	public override void Display()
	{
	Console.WriteLine('Display Method DerivedA');
	}
}

public class DerivedB : BaseA
{
	public override void Display()
	{
	Console.WriteLine('Display Method DerivedB');
	}
}

static void Main(string[] args)
{
	BaseA AObj = new BaseA();
	AObj.Display();

	DerivedA ADerObj = new DerivedA();
	ADerObj.Display();

	BaseA AObj1 = new DerivedA();
	AObj1.Display();

	BaseA AObj2 = new DerivedB();
	AObj2.Display();

	//DerivedA AObj3 = (DerivedA)new BaseA(); Casting error
	// AObj2.Display();
	Console.ReadLine();
}

Output

Display Method BaseA
Display Method DerivedA

Display Method DerivedA
Display Method DerivedB

Object Oriented Programming (OOPs)

Pillars of Object Oriented programming

  • Abstraction
  • Encapsulation
  • Inheritance
  • Polymorphism

What Is Abstraction?

  • Abstraction is showing only the essential details.
  • In real life we have example like mobile, TV where we show only what is required. For mobile there is screen, port for charging and headset and buttons. For TV we have Screen, On-off button and ports for connection.
  • In terms of Application, We only expose the API’s that other user needs. Internally which other API is used, what is sequence that is hidden from user.
  • Abstraction is achieved using Abstract class and Interface.

What is Encapsulation?

  • Encapsulation is hiding the unessential details. Encapsulation is also called data hiding.
  • Encapsulation is a process of binding or wrapping the data and the functions that operates on the data into a single entity. This keeps the data safe from outside interface and misuse.
  • One way to think about encapsulation is as a protective wrapper that prevents code and data from being arbitrarily accessed by other code defined outside the wrapper.
  • One example of Encapsulation is private methods; clients don’t care about it, You can change, amend or even remove that method  if that method is not encapsulated and it were public all your clients would have been affected.
  • In term of real life example, for mobile how the button functionality is implemented, how validation is done and where data is stored is hidden from user.
  • In term of Application we decide how that API works internally.
  • Encapsulation is achieved using Private modifier.

Example of Abstraction and Encapsulation

Calculator

Add method

  • ValidateNumbers
  • SaveNumbers
  • AddNumbers
  • Result

Abstraction

  • Now you need to decide which API to expose. Whether 1 API is sufficient or multiple API’s will be required.

Encapsulation

  • How that API works is hidden from user.
  • Whether that API validates records first or not
  • How database connection is done
  • How saving of those records is done

What is Inheritance?

  • Inheritance is a way by which user can create a new class called derived class from Base class.
  • Derived class inherits the properties and functions of base class.
  • Inheritance is a way by which we achieve code reusability.
  • For example in real word we have Vehicle class or Shapes class.
  • Vehicle will have Body, Engine, Seats, wheels etc. Common functionality can be kept in base class and derived class will have required functionality.
  • It is moving from generalization to Specialization.

What is Polymorphism?

  • Polymorphism means one name many forms.
  • In other words, “Many forms of a single object is called Polymorphism.”
  • There are 2 types of polymorphism:
    • Static Polymorphism
      • Function Overloading can be done by having
        • Different number of parameters.
        • Different sequence of parameters.
        • Different type of parameters.
      • Operator Overloading
        • Giving different meaning to operator. + operator is used for addition. Same can be used for concatenation.
  • Run-time Polymorphism
    • Achieved using virtual and override keyword.
    • The base class method is overridden in derived class using override keyword.

Polymorphism examples:

Example-1:

  • A Teacher behaves to student.
  • A Teacher behaves to his/her seniors.

Example-3:

Your mobile phone, one name but many forms

  • As phone
  • As camera
  • As mp3 player
  • As radio

Dot Net Interview Questions for Experienced 8+ years

.Net Interview Questions for Experienced(8 + years)

Interview of experienced candidate is based upon requirement and candidate experience. Either the requirement is for web projects or desktop projects. As experience grows the expectation grows and Expertise level grows. Interviewer expects in-depth knowledge about same topics that are asked in any interviews.

Common requirements:

Questions on object oriented programming
Questions on .Net framework.
Questions on C#.Net
Questions on database
Questions on WCF and Web API
Questions on Design Patterns
Questions on Entity framework

For Web projects questions on :

  1. ASP.Net
  2. MVC3
  3. Javascrip
  4. JQuery
  5. JSON

For Windows Applications questions on :

  1. WPF
  2. Winforms
  3. Threading

Dot Net Interview Questions for Experienced up to 7 years

.Net Interview Questions for Experienced( 5 -7 years).

Interview of experienced candidate is based upon requirement and candidate experience. Either the requirement is for web projects or desktop projects.

Common requirements:

  1. Questions on object oriented programming
  2. Questions on .Net framework.
  3. Questions on C#.Net
  4. Questions on database
  5. Questions on WCF and Web API

For Web projects questions on :

  1. ASP.Net
  2. MVC3
  3. Javascrip
  4. JQuery

For Windows Applications questions on :

  1. WPF
  2. Winforms
  3. Threading