Re: C# confusion
- From: Jon Skeet [C# MVP] <skeet@xxxxxxxxx>
- Date: Mon, 12 Dec 2005 19:14:50 -0000
Jesika <Jesika@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote:
> I'm coming from a 3 year C background with some basic knowlegde on the Win32
> API. I just started learning C# a day ago and I have a couple questions that
> I need cleared up that just have me very confused.
>
> [quesitons]
>
> 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?
No instances are created automatically, but the type itself has data
associated with it on the heap (including static variables).
> 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?
You're creating instances of the type, not copies of the type itself.
So for each instance you effectively have a new set of the instance
variables, but there's only one set of the static variables however
many instances you have.
> 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?
That depends on various things. For the most part, types are only
loaded when they're first used, but they may be loaded when they
*might* be used.
> 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?
Static members are associated with the type itself rather than with any
particular instance of the type.
It's really worth reading a book or a tutorial on this - object
orientation isn't a good thing to learn from scratch via newsgroups,
I'm afraid.
You might want to have a look at
http://www.jaggersoft.com/csharp_course/index.html
I haven't looked at it in detail, but its author is someone I very much
respect...
> 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.
It sounds like you're really just up against object orientation more
than C# itself.
--
Jon Skeet - <skeet@xxxxxxxxx>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
.
- Prev by Date: Re: Volatile fields
- Next by Date: Re: C# confusion
- Previous by thread: DataSet Instantiation Detail
- Next by thread: Re: C# confusion
- Index(es):
Relevant Pages
|
Loading