QuestPond’s real life examples on Extension methods and nullable types in C#.NET

What are extension method?

It seems very simple question but it’s not. One time I have been asked this question by one of the interviewer. My answer was simply “Extension method will let us create new methods for a class without touching that class”.

With experience I got more matured and I learned many things. I realized following points will impact more during interviews,

  • Extension methods are mainly useful when we are dealing with Third party libraries. We don’t have the source code of the class but with Extension methods we will be able to add new member functions into that class.
  • It make us obey OCP – Open closed principle, which says “software entities should be closed for modifications but open for extensions. With extension methods we add new methods to existing classes without modifying it.

Above you read is an example for extension methods and for its .NET interview questions explanation read complete extension methods in C#. We also have a video for it with scenarios and where to use it: -

What are nullable types?

“Let us store null values inside value type variable.” Normally value type won’t allow us to store null values.

Example : we cannot say
intmyVar; myVar=null; // It will fail because value types won’t allow null values and “int” is value type.
string myVar2; myVar2=null; It will work because string is reverence type.

Now the question why we need value types?

Normally Nullabledatatypes introduced for dealing with issues in two datatypes.

      1) DateTime
      2) Boolean

One of the two possibilities are there with these variables

     1) It may contain some values
     2) It will contain a default value.

Confused? Every value type is associated with some default value for an instance for Boolean it is false and for datetime its Datetime.Min that is 1/1/0001

Let’s talk about a scenario,

  • You have retrieved some value from database
  • You convert that value into boolean and store in some variable of type boolean.
  • Database may contain null values but your traditional value type cant.
  • In case null values your variable will contain default values.

It may leads to incorrect operations. Ultimate solution will be using Nullable types which may contain one of the three values,

      1) No Value
      2) True
      3) False

For technical trainings on various topics like WCF (Windows Communication Foundation), MVC (Model View Controller), Business Intelligence, Design Patterns, WPF, TFS and Basic fundamentals feel free to contact SukeshMarla@Gmail.com or visit www.sukesh-marla.com

For more stuff like this, click here. Subscribe to article updates or follow at twitter @SukeshMarla

Posted in Uncategorized | Tagged , , , , , , , | Leave a comment

QuestPond’s Interview Questions & Answers on Design Patterns in .NET

Explain Design Pattern or What are Design Patterns?

This is the one liner definition for it -

Design Patterns are recurring solution to recurring problems in software architecture.

We have complete article for Design Pattern in .NET

Learning Design Pattern:

Design patterns are documented tried and tested solutions for recurring problems in a given context. So basically you have a problem context and the proposed solution for the same. Design patterns existed in some or other form right from the inception stage of software development. Let’s say if you want to implement a sorting algorithm the first thing comes to mind is bubble sort. So the problem is sorting and solution is bubble sort. Same holds true for design patterns.

Which design patterns are you familiar with?

This question is completely left up to you, pick any two to three Design Patterns which you have used in your project and talk about it.

Mediator Pattern defines simplified communication between classes. View detailed explained video Mediator Pattern

Also here we have put three videos explained by QuestPond on Design Pattern: -

Composite Pattern: It is a structural pattern and it is a tree structure of simple and composite objects.

Factory Design Pattern: It is a creational pattern and it creates an instance of several derived classes.

Builder Design Pattern: It is a creational pattern and it separates object construction from its representation.

Posted in Uncategorized | Tagged , , , , , | Leave a comment

QuestPond’s Interview Questions & Answers on .NET

Explain different types of level exceptions?

There are two types of exception (a) system level exceptions and (b) application level exceptions

Exceptions that are thrown by .NET framework are called as system exceptions. These errors are non-recoverable or fatal errors like ArgumentOutOfRangeException, IndexOutOfRangeException, StackOverflowException etc.

Application exceptions are custom exceptions created for the application. Application exceptions are created by deriving from “ApplicationException” class as shown below. You can then create the object of the below exception and throw the same from the code and catch the same on the client side.

public class MyOwnException : ApplicationException
{
  private string messageDetails = String.Empty;
  public DateTime ErrorTimeStamp {get; set;}
  public string CauseOfError {get; set;}

  public MyOwnException(){}
  public MyOwnException(string message,
    string cause, DateTime time)
  {
    messageDetails = message;
    CauseOfError = cause;
    ErrorTimeStamp = time;
  }

  // Override the Exception.Message property.
  public override string Message
  {
    get
    {
      return string.Format("This is my own error message");
    }
  }
}

How can we handle exceptions in .NET?

Exceptions are handled by “System.Exception” base class. If you want to raise an error from source you need to create the exception object with below code snippet.

throw new Exception("Customer code cannot be more than 10");

Once the exception is raised if you want to catch the same you need to use the try catch block as shown below.

try
{
// This section will have the code which
// which can throw exceptions.
}
catch(Exception e)
{
// Handle what you want to
// do with the exception
label.text = e.Message;
}

See the following video on Exception Error Handling: -

Click and see here for more step by step training in .NET

Posted in Uncategorized | Tagged , , , , , , | Leave a comment

QuestPond’s Interview Questions & Answers on .NET

What is WPF and SilverLight?

WPF (Windows Presentation foundation) is a graphical subsystem for displaying user interfaces, documents, images, movies etc. It uses XAML which is a XML descriptive language to represent UI elements.

See following video on WPF(Windows Presentation foundation): -

SilverLight is a web browser plug-in by which we can enable animations, graphics and audio video. You can compare silver light with flash. We can view animations with flash and it’s installed as a plug-in in the browser. 

See following video on SilverLight: -

What is LINQ and Entity framework?

LINQ is a uniform programming model for any kind of data access. It is also an OR mapper which helps us to expedite our business object creation process.

See following video on explaining LINQ: -

Entity Framework auto-generates code for the Model (middle layer), Data Access Layer, and mapping code, thus reducing a lot of development time.

ADO.NET entity is an ORM (object relational mapping) which creates a higher abstract object model over ADO.NET components. So rather than getting into dataset, datatables, command, and connection objects as shown in the below code, you work on higher level domain objects like customers, suppliers, etc.

In other words the complete middle tier development is expedited using entity framework.

See following video on Insert, Update and Delete using Entity Framework: -

Click and see here for more training on .NET

 

Posted in Uncategorized | Tagged , , , , , , , , , , | Leave a comment

QuestPond’s Interview Questions & Answers on ASP.NET

What is a SESSION?

Session object store information between HTTP requests for a particular user.

Which are the various modes of storing ASP.NET session?

  • InProc: - In this mode Session, state is stored in the memory space of the Aspnet_wp.exe process. This is the default setting. If the IIS reboots or web application restarts then session state is lost.
  • StateServer: – In this mode Session state is serialized and stored in a separate process (Aspnet_state.exe); therefore, the state can be stored on a separate computer(a state server).
  • SQL SERVER: - In this mode Session, state is serialized and stored in a SQL Server database.

Session state can be specified in <sessionState> element of application configuration file. Using State Server and SQL SERVER session state can be shared across web farms but note this comes at speed cost as ASP.NET needs to serialize and deserialize data over network repeatedly.  

Does session use cookies?

Yes, session use cookies. The session id value is stored in the browser client temp directory. This id is used to get the session data from the server in every post back.

Is Session_End event supported in all session modes?

Session_End event occurs only in “Inproc mode”. “State Server” and “SQL SERVER” do not have Session_End event.

How can we kill a user session?

Session.abandon

See following video on importance of session and viewstate: -

Click and see here for more ASP.NET training.

   

 

Posted in Uncategorized | Tagged , , , , , , , , | Leave a comment

QuestPond’s Interview Questions & Answers on basics of .NET

What are abstract classes?

Abstract class is a half defined parent class. The full implementation of abstract class is defined by the child classes.

For example below code snippet shows a simple abstract class / half defined class called “DatabaseCommon” and later the concrete classes i.e. “SQLServer” and “Oracle” inherit and define a complete implementation for the same.

To define an abstract class we need to use the abstract keyword.

public abstract class DatabaseCommon  
}
public class SQLServer : DatabaseCommon  
}
public class Oracle : DatabaseCommon
{
}

What are abstract methods?

Abstract classes can have abstract methods. Abstract methods when defined in a parent class have to be implemented in the child classes. If abstract methods are not implemented it will throw a error.

Can we create an object of abstract class or an interface?

No.

An abstract with only abstract method, how is it different from interfaces?

If you define all methods, function, properties as abstract in abstract class it inhibits the same behavior as an interface. 

What is difference between abstract classes and interfaces?

See following video on defining abstract class as a static: -

Click and see here for more learning basics of .NET.


 

Posted in Uncategorized | Tagged , , , , , , , , , | Leave a comment

QuestPond’s Interview Questions & Answers on Design Pattern in .NET

What is a Composite Pattern?

During Programming there are times when we have a collection of objects with us and we want to treat collection of objects and individual objects uniformly.

Example:

  • Let’s say you are creating a complex user controls.
    Now the best part is controls like textbox, button and label should be treated in the same manner we treat controls like panel which in turn may consist of many controls.
    Here Composite pattern comes to picture.
  • Let’s say you are working on a logic which consist of working with file system. In this case you will be mainly working with files. Files may indicate simple flat files or folders which contain one or more files internally.
    We implement Composite pattern in this situation
    .

How to implement Composite Pattern?

Let’s talk more about the control creation demo discussed above and let see how to implement composite pattern.

1. When it comes to implementing Composite pattern first thing we will do is identify all
the container (control which can contain more controls) and individual self-contained
controls.

Let’s say individual controls are Button and textbox. Composite control is Panel.

public class Panel{}
public class Button{}
public class Textbox{}

2. We want to treat both of them uniformly. Is simple words these two should be
belonging to same family and for that we need an interface containing
Render Method

public interface IControl{void Render();}

3. Implement the interface in all the classes and implement Render Method.

public class Textbox:IControl
{
Public void Render()
{
 //Render Textbox
	}
}
public class Button:IControl
{
Public void Render()
{
 //Render Button
	}
}
public class Panel:IControl
{
public ListChildControls{get;set;}
Public void Render()
{
	//Render Panel Start
	foreach(IControli in ChildControls)
	{
		i.Render();
		}
	//Render Panel End
	}
}

That’s it this is the composite pattern. A group of object and individual object will be treated same now.

For technical trainings on various topics like WCF, MVC, Business Intelligence, Design Patterns, WPF, TFS and Basic fundamentals feel free to contact SukeshMarla@Gmail.com or visit www.sukesh-marla.com

For more stuff like this, click here. Subscribe to article updates or follow at twitter @SukeshMarla

See following video on Composite Pattern: -

Click and see here for more training on Design Pattern in .NET.

Posted in Uncategorized | Tagged , , , , , , , | Leave a comment

QuestPond’s Interview Questions & Answers on learning authentication in ASP.NET

What are the various ways of authentication techniques in ASP.NET?

There are 3 major ways of doing authentication and authorization:

  • Windows: – In this mode the users are stored in windows local user groups.
  • Forms: – In this mode we create a login screen and use the forms authentication class to do validations. It’s a ticket based authentication.
  • Passport: -In this mode the users are validated from Microsoft sites like hotmail , devhood , MSN etc , ticket is generated and that ticket can be used to do authentication and authorization in your web application.

How do I sign out in forms authentication?

FormsAuthentication.Signout()

If cookies are disabled how will forms authentication work?

It will pass data through query strings.

How do we implement windows authentication?

  • Create users in your local windows user group.
  • In web.config file set the authentication mode to windows.
  • In web.config file set <deny users=”?”/>.

Once you do the above three steps your asp.net pages will authenticated and authorized from the users stored in windows local user group.

How can we do single sign-on in ASP.NET?  

For single sign-on we need to use forms authentication and put the same machine key in the web.config files of all web applications who will participate in single sign on.

See following video on implementing ASP.NET Forms Authentication: -

Click and see here for more learning tutorials on ASP.NET.


Posted in Uncategorized | Tagged , , , , , , , , , , | Leave a comment

QuestPond: Interview Questions and Answers on learning ADO.NET

How can we fine-tune the command object when we are expecting a single row?

Again, CommandBehaviour enumeration provides two values Single Result and Single Row. If you are expecting a single value then pass “CommandBehaviour.SingleResult” and the query is optimized accordingly, if you are expecting single row then pass “CommandBehaviour.SingleRow” and query is optimized according to single row.

Which is the best place to store connection string in .NET projects?

Config files are the best places to store connection strings. If it is a web-based application “Web.config” file will be used and if it is a windows application “App.config” files will be used.

How do you fill the dataset? 

Create object ofdata adapterand call the fill command method of the adapter.

SqlDataAdapter objAdapter = new SqlDataAdapter(objCommand); 
DataSet objDataset = new DataSet(); 
objAdapter.Fill(objDataset);

What are the various methods provided by the dataset object to generate XML?

  •  ReadXML
                Read’s a XML document in to Dataset.
  • GetXML
               This is a function, which returns the string containing XML document.
  • Writexml
               This writes a XML data to disk.

How can we save all data from dataset?

Dataset has “AcceptChanges” method, which commits all the changes since last time “Acceptchanges” has been executed.

See following video on dataset is a disconnect while datareader is connected: -

Click and see here for more stuffs step by step learning ADO.NET.


Posted in Uncategorized | Tagged , , , , , , , , , | Leave a comment

QuestPond: Interview Questions/Answers on basics of .NET

What is an IL code?

It’s a CPU independent partially compiled code.

Note: - Half compiled means this code is not yet compiled to
machine/CPU specific instructions.

Why IL code is not fully compiled?

We do not know in what kind of environment.NET code will run. In other words we do not know what can be the end operating system, CPU configuration, machine configuration, security configuration etc. So the IL code is half compiled and on runtime this code is compiled to machine specific using the environmental properties (CPU, OS, machine configuration etc).

Who compiles the IL code and how does it work?

IL code is compiled by JIT (Just in time compiler).

Is it possible to view the IL code?

Yes by using ILDASM simple tool we can view an IL code of a DLL or EXE. In order to view IL code using ILDASM, go to visual studio command prompt and run “ILDASM.EXE”. Once ILDASM is running you view the IL code.

See following video on IL code, CLR, CTS, CAS: -

Click and see here for tutorials on learning basic of .NET.

 

Posted in Uncategorized | Tagged , , , , , , , , , | Leave a comment