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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

C++ features in C#

Status
Not open for further replies.

Dario1984

Technical User
Aug 23, 2005
12
0
0
AU
Is there any eqivalent of S.T.L. sets in C# ?

Also, I read you can't have a static variable that is shared between all instances of a class. Is there a simple way to do it ?
 
The STL equivalent in C# world is the .NET Framework itself; it is the big big library.
Whoever told you about static members doesn’t have the foggiest clue what he is talking about. Static members are shared between all instances and non-instance too.


Walid Magd (MCP)

The primary challenge of every software development team is to engineer the illusion of simplicity in the face of essential complexity.
-Grady Booch
 
Keep in mind that static variables are the equivalent to global variables - which are very very very bad!

I know a lot of C++ developers who use global variables regularily and it is hard for them to maintain their code. There are better ways to have access to variables/data without being global.
 
Keep in mind that static variables are the equivalent to global variables - which are very very very bad!

I am afraid that is totally untrue. Static variables are not global, they can’t be used without reference to their class. They also can be made private for internal use only.
Static field are allocated once and shared between all instance because of their very own nature which was observed by the designer that they are static, meaning there is no need for each instance to maintain its own copy. This is not to say they are constants, the value of static member may or may not change during the life time of a program but the concept is; even if the value was changes, all remaining instance need to see the “same value”. Consider the type BankAccount which has – for the sake of argument – a static member GetCurrentInterestRate; if for any reason the GetCurrentInterestRate changed in one instance, all the others need to be informed with that change.
A more realistic example that we used befor is to maintain a shared variable between all instance that is incremented when a new object get created and decremented when any instance dies; this way, we can query any – still alive object or just the class name - for how many instance still alive. If we don’t have a mechanism to share this info between instance and non instances, we would need a higher level object just to maintain this information for us.

These were example of static members with dynamic nature, that is they are changing during their life time. Think of Int32.MaxValue as an example of a shared variable that is constant by nature (at least, on specific platform and microprocessor). You can argue that it is enough to define it constant. In this case you will loose the main advantage static keyword gives, you that is it is shared. If you have an array of one thousand integers, you will have one thousand 4 bytes (or 8000 or whatever happen to be the capacity of this type on a specific platform) instead of only 4 reserved for this MaxValue’s value and shared between all the thousand instances. That is not terribly efficient. Can you think of another easy way to solve this design problem without using the mighty key word static?



Walid Magd (MCP)

The primary challenge of every software development team is to engineer the illusion of simplicity in the face of essential complexity.
-Grady Booch
 
Static variables are not global
True. They are only global to their AppDomain, and not to the entire system.

Can you think of another easy way to solve this design problem without using the mighty key word static?

You could use const, but the compiler will inline those values in dependent assemblies, so if it changes in a future release, the other assemblies will still have the old value.

Other techniques include using the readonly keyword, but you still need an instance to access it (which may not be what you want). Plus readonly properties, which are nice from a code maintenance standpoint, but again require an instance (unless you mark the property static, which sort of defeats the purpose of this exercise!).

Chip H.


____________________________________________________________________
If you want to get the best response to a question, please read FAQ222-2244 first
 
Thanks for the discussion. I think everyone missed my original question about the set though.

Do you remember #include <set> ?

I just wanted something to insert values into and to have them automatically rejected, if they were not unique. Is this available in C# ?
 
Unfortunately there is no native support for the Set data structure in the .NET Framework 1.1. Very disappointing I know, but it very easy to wire one fast. Read this short article for a quick and easy implementation.


Walid Magd (MCP)

The primary challenge of every software development team is to engineer the illusion of simplicity in the face of essential complexity.
-Grady Booch
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top