Re: C# confusion
- From: "Joanna Carter [TeamB]" <joanna@xxxxxxxxxxxx>
- Date: Mon, 12 Dec 2005 19:16:13 -0000
"Jesika" <Jesika@xxxxxxxxxxxxxxxxxxxxxxxxx> a écrit dans le message de news:
D81CAC70-9D57-4CA5-B153-C8FF50FAE130@xxxxxxxxxxxxxxxx
| 1.) Whenever you declare a class - MyClass - is the class alive in memory
| for the whole duration of the program? If so, then, since it was not
created
| via the "new" keyword (like an instance is), is it on the heap or stack?
In C#, only structs whose data is all public and other value types are
created on the stack everything else is created on the heap via the new
call. No call to new, no valid object, just a reference.
| 2.) If you create instances of MyClass, you're really just creating copies
| of MyClass, like you would create instances of a struct (non-pointer
structs)
| in C/C++, correct?
No, you are allocating an area of memory for the data segment for each
instance of your class, but you are using a common memory area allocated for
the code for the class.
| 3.) When Myclass is not declared inside another class, or namespace for
that
| matter, why is it that you cannot make MyClass private? Is it because it
| would make the class totally useless since you would not be able to create
| any instances of it or use it via the MyClass type?
Exactly.
| 4.) Still pertaining to the above; if in the programs duration, you never
| invoke MyClass or create any instances of it, is it still created in
memory
| or is it *only* created when you create an instance of it or try to access
it
| via the MyClass type? if it's the latter, would it make any difference if
any
| member in the class or the class itself was made static?
Instances are created in memory, classes don't consume memory unless static
data members are initialised upon first reference of the class.
| 4.) static keyword; This one has been hitting me like a hammer on the
head.
| Ok, from what I can derive from my reading so far is that the concept of
the
| static keyword on members is like using global variables or passing
pointers
| around to functions in C/C++, Am I correct? Is that its main and/or sole
| purpose?
A static property/method/field is accessible without having to create an
instance of the class. They can be used as "global" variables/methods, but
you have to pass all data required to the method that you are calling unless
you use the data stored in static fields of the class.
| Knowing C, I think C# isn't too difficult. It seemed pretty straight
forward
| until I came upon classes and static versus non-static members. Anyway, I
| hope someone can enlighten me on the above questions; Thanks in advance.
C# is very much like a mixture of C++ and Java, but you do have to be aware
of "faux amis" which don't work the same :-)
Joanna
--
Joanna Carter [TeamB]
Consultant Software Engineer
.
- Prev by Date: Re: C# confusion
- Next by Date: Re: C# confusion
- Previous by thread: Re: C# confusion
- Next by thread: Re: C# confusion
- Index(es):
Relevant Pages
|