I am a newbie and need a clarification on some question:
1Q.What does new operator do?
Ans:Am not sure whst does new operator do other than intialization, memory allocation and ???
2Q. How to Allocate a large amount of memory using new operator?
3Q.An object contains 2 buffers of fixed size. Can we initialize the object by doing ZeroMemory in the constructor?
4Q. Can we have a static consture?
5Q. Can we a non-parameterized constructor in the struct declaration?
6Q. Can we have a private constructor?
Mamta MPosted Nov 4, 2008, 2:14 AM
You are welcome, Sangeetha.
The new keyword can be used as a modifier to explicitly hide a member inherited from a base class. When you declare an inherited member in the derived class with the same name as the base class member, and modify it with the new modifier, it will hide the inherited member.
Consider the following example:
public class Parent
{
. . .
public void Display()
{
....
}
}
Declaring a member with the name Display and keyword new in the derived class will hide the method Display in the base class.
public class Child : Parent
{
new public void Display()
{
.....
}
}
Regards,
Mamta
Sangeetha HaudakariPosted Nov 4, 2008, 12:34 AM
Thank you so much Mamta.
Could please elobarte lilttle more on new Keyword/constructor and modifier?
I appreciate your time
Mamta MPosted Nov 4, 2008, 12:20 AM
Hi Sangeetha,
I will answer the questions that I know:
1Q.What does new operator do?
Answer:
First, you need to be clear whether you are talking about the new keyword strictly as an operator or as a modifier, because when used as a modifier, new has a different meaning.
The new operator as you mentioned is used to create new instances of a type such as a class, and allocating memory to it.
2Q. How to Allocate a large amount of memory using new operator?
Answer:
Am not sure about this, all I know is that when the new operator fails to allocate memory, it throws an OutofMemoryException.
3Q.An object contains 2 buffers of fixed size. Can we initialize the object by doing ZeroMemory in the constructor?
This question is not clear at all.
4Q. Can we have a static consture?
Answer:
If you mean constructor, yes you can have static constructors. C# allows that.
5Q. Can we a non-parameterized constructor in the struct declaration?
Answer:
No, the C# compiler does not allow parameterless (default) constructors for structs.
6Q. Can we have a private constructor?
Answer:
Yes, C# allows you to define and use private constructors.
Hope those answers helped you.
Regards,
Mamta