.NET interview questions: – What is constructor and mention its uses?

Constructor is a special type of method which will automatically get invoked when object is initialized.

Main uses of constructor: –

Initializing data with default (or some specified values) values.

I think we can achieve the same thing with functions also then why constructor?

Functions define the behavior of a class (or object). It may use the existing data and may modify them.
There is a difference between initializing and assigning. Initializing means setting the initial values (at the time of creation) whereas assigning may means changing some existing value also. For Initializing we use constructor. It’s a best practice not a fixed rule.

What is the difference between property and Variable in terms of usage?

Variable contains data. They can define as a temporary memory location which holds data. properties are encapsulation over data and thus protects data. Internally when we create properties it gets converted to one variable and two functions (one set and one get).

Let’s say I want to create a function having only one parameter. Can I use property instead of function for this purpose?

Yes you can but, it’s not preferred. Properties are meant to be data and functions for behavior. Again it’s a best practice.

Hope you enjoyed reading this and learned something. Your comments and votes are always welcome.

If you are looking for some hard core live training on MVC, WCF, Design Patterns, BI, WPF, TFS, HTML5 visit www.sukesh-Marla.com or contact SukeshMarla@Gmail.com.

Also see following .NET interview questions video on private constructor in C#: –

Advertisements

About c# and .NET Interview questions

This blog is for developers who want to crack .NET and C# interviews. It has all tips and tricks needed to crack .NET interviews , C# interview , SQL Server interview , Java interview , WCF Interview , Silverlight interview , WPF interview , LINQ interview , Entity framework Interview. Do not forget to watch our Learn step by step video series. Learn MVC in 16 hours:- https://www.youtube.com/watch?v=Lp7nSImO5vk Learn AngularJS Step by Step:- https://www.youtube.com/watch?v=0kmdjqgO9IY Learn Design Pattern in 8 hours:- https://www.youtube.com/watch?v=YDobmucohqk Learn C# and .NET in 60 days:- https://www.youtube.com/watch?v=yh2SrzCkNQA Learn MSBI in 32 hours:- https://www.youtube.com/watch?v=mGPJx3ocFgg Learn SharePoint Step by Step in 8 hours:- https://youtu.be/C2fW76SwJNU
This entry was posted in Uncategorized and tagged , , , , , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s