Win Surprise Gifts!!!
Post New Question
Subscribe to Interview Questions
Is it possible to return a value from constructor?
Posted By :
Posted Date :
March 03, 2011
No. It is not possible.
You can also find related Interview Question to
Is it possible to return a value from constructor?
Difference between type constructor and instance constructor? What is static constructor, when it will be fired? And what is its use?
(Class constructor method is also known as type constructor or type initializer)
Instance constructor is executed when a new instance of type is created and the class constructor is executed after the type is loaded and before any one of the type members is accessed. (It will get executed only 1st time, when we call any static methods/fields in the same class.) Class constructors are used for static field initialization. Only one class constructor per type is permitted, and it cannot use the vararg (variable argument) calling convention.
What is Private Constructor? and it's use? Can you create instance of a class which has Private Constructor?
When a class declares only private instance constructors, it is not possible for classes outside the program to derive from the class or to directly create instances of it. (Except Nested classes)
Make a constructor private if:
1) You want it to be available only to the class itself. For example, you might have a special constructor used only in the implementation of your class' Clone method.
2) You do not want instances of your component to be created. For example, you may have a class containing nothing but Shared utility functions, and no instance data. Creating instances of the class would waste memory.
C# provides a default constructor for me. I write a constructor that takes a string as a parameter, but want to keep the no parameter one. How many constructors should I write?
The answer is TWO of the above questions
What is return type of Executenonquery() ?
int will be returned when you call ExecuteNonQuery() in ADO.NET
Constructor and Destructor
1. The Constructor is the first method that is run when an instance of a type is created. In visual basic a constructor is always Sub new ().
2. Constructor are use to initialize class and structure data before use. Constructor never returns a value and can be overridden to provide custom initialization functionality.
3. The constructor provides a way to set default values for data or perform other necessary functions before the object available for use.
Destructors are called just before an object is destroyed and can be used to run clean-up code. You can't control when a destructor is called.
What is a private constructor? Where will you use it?
When you declare a Constructor with Private access modifier then it is called Private Constructor. We can use the private constructor in singleton pattern.
If you declare a Constructor as private then it doesn't allow to create object for its derived class, i.e you loose inherent facility for that class.
How to deal with return value of stored procedure when you are using DataReader ?
To deal with such scenario, you can find "ParameterDirection.ReturnValue" in .net
You can use this parameter direction to read the return value of the stored procedure.
Following example can make it more clearer.
In case, you have following or similar type of return statement somewhere in your stored procedure.
To read this value, following code works,
Dim result As Integer = 0
Dim retValParam As New SqlClient.SqlParameter("@RETURN_VALUE", SqlDbType.Int)
retValParam.Direction = ParameterDirection.ReturnValue
result = retValParam.Value ' here the result must contain the returned value. In this case, it will be -999
If I return out of a try/finally in C#, does the code in the finally-clause run?
Yes. The code in the finally always runs.
Why do I get an error (CS1006) when trying to declare a method without specifying a return type?
If you leave off the return type on a method declaration, the compiler thinks you are trying to declare a constructor. So if you are trying to declare a method that returns nothing, use void. The following is an example: // This results in a CS1006 error public static staticMethod (mainStatic obj) // This will work as wanted public static void staticMethod (mainStatic obj)
What is Static Constructor?
It is invoked when the instance of the class is created.
What exactly a Constructor is doing ?
Constructor Creates as well as Initialized the objects.
What is the work of a constructor?
Constructor creates and initialises the objects in an application.
A constructor can be private. True or False
False. A constructor should not be private.
Static datamembers should be initialized inside the constructor. True or False.
False. Static datamembers should not be initialised inside constructor.
Quick Links For Interview Questions Categories:
Find questions, FAQ's and their answers related to .NET, C#, Vb.Net, Sql Server and many more.
Now you can find lots of .NET, C#, Vb.Net, SQL Server,Windows, ASP.Net related Questions and their Answers here at www.dotnetspark.com. Our aim is to help you pass your certification Exams (MCP, MCSD, MCAD etc.,) with flying scores and get good name in your company.
So, Start looking our
section daily and improve your .NET Skills. You can also help others by posting Interview Questions and their Answers in this section.
Hall of Fame
Terms of Service
Tell A Friend