Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Some points for discussion related to C# 1

Status
Not open for further replies.
Apr 11, 2002
193
IN
Hi,

I have some points for discussion which are bothering me since long.

1. Can private class variable be inherited.

2. C# provides a default constructor. I have written a constructor that takes a string as a parameter but want to keep the number parameter as 1. I am confused about how to write the constructor.

3. When do i need to declare a class as an abstract instread of a interface.

4. Why access modifiers cant be specified for methods inside the interface.

5. If a base class has a bunch of overloaded constructors and inherited class has another bunch of overloaded constructors can i enforce a call from a inherited constructor from arbitrary based constructor.

6. Whats the implicit name of the parameter dat gets passed into the class set method.

If you can tell me about these then it would be a great help for me.

Thanks,
Manish
 
1. No. When a class derives from a base class, the private members of the base class are not directly accessible through the derived class. However, the private class members are in fact inherited from the base class to the derived class. You can write public or protected members of the base class to manipulate or access the private members of the base class.

2. I'm not sure if I understand your question. You are overloading the default constructor. Fine. When you call it, be sure to pass the expected parms otherwise the default constructor will be called.

3. When you need to define a class that will act as a base class that instantiated classes can inherit from.

4. Don't understand your question. An interface can provide methods, write-only properties, and properties with goth get and set accessors.

5. Sure. Either through an overloaded constructor or use the fully qualified name for the base class constructor you're trying to use.

6. "value"

Good luck!
 
Hi,

Thanks for the reply.

In case of the 4th point my question is Why cant i use access modifiers(like public, private or protected) with respect to method declaration.
For Example
interface ITest
{
Private void Test();
}

It gives me a compile time error saying private cannot be used.

In case of 6th point i didnt understand what does the word "value" mean there.

I also wanted to know the actual difference between System.String class and System.text.StringBuilder class.

Thank you very much for the reply.

Manish
 
You can't declare scope in an interface because everything *must* be public to be implemented. Since an interface can only have stubs, it can't be inherited from, only implemented.

Private and protected members can't be accessed because they are class/inheritance specific and an interface is not a class.

Internal members are arguable, but their implementation would be too confusing. Should they be internal to the assembly containing the interface, or internal to the assembly containing the class that implements the interface? There are rational arguments for both ways.

The only modifier left is public. If you need to implement either protected or internal members, you should use an abstract class. Forcing a private member is useless as only the defining class would have knowledge of or access to the member.

I spent a lot of time griping about the same things, but the more I thought about what the interface is intended to do, the more I realized how well adapted it is.
 
To address the differences between string and StringBuilder...

The System.String is "immutable", meaning that when you create a string (e.g., string hello = "hello world";) a fixed length memory structure is created to hold that string. That value cannot be changed. If you manipulate a string, the original is destroyed and a new one created to contain the new value.

The StringBuilder, by contrast, sets aside memory to contain string data. When you make changes, the memory is manipulated. It requires more memory than a System.String, but it is *much* more efficient at manipulating strings. A StringBuilder can be orders of magnitude faster than a System.String when large numbers string manipulations are performed (no destroy/recreate for each manipulation).
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top