473,549 Members | 2,935 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

C# confusion

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?

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?

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?

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?

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?

[/questions]

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.
Dec 12 '05 #1
4 1428
Jesika <Je****@discuss ions.microsoft. com> 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 - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Dec 12 '05 #2
"Jesika" <Je****@discuss ions.microsoft. com> a écrit dans le message de news:
D8************* *************** **...icrosof t.com...

| 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
Dec 12 '05 #3

Jesika 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?
You have to distinguish here between three things. First, you are
essentially correct (with caveats following): if you never say "new
MyClass" then you will never create an instance of MyClass on the heap,
so any instance members you declare in MyClass will never exist. That
said, you could declare some things in MyClass as "static", meaning
that there is only one copy for MyClass as a whole, shared amongst all
instances. I believe that static members are stored in a separate area
of memory: not the stack and not the heap, since they are brought into
existence the first time anything in MyClass is referenced by your
program, and are never garbage collected. Ever. Third, remember that
classes consist of data and code, so the _code_ associated with MyClass
will in fact take up some memory, but again it is static in nature so
it lives neither on the stack nor on the heap.

The memory allocated for instance members of classes is always stored
in the heap. Local variables and parameters to methods are stored on
the stack.
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 in the heap copies of the instance members you declared
for MyClass, yes. Note that the code for MyClass any any static members
are not copies over and over again on the heap. They reside in static
memory.
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. The compiler won't let you declare something that you can't
use.
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?
If you never reference anything about MyClass, I believe that the code
still occupies memory space. I don't know whether the IL (Intermediate
Language) is JITted (just-in-time compiled) or not. Anyone? At what
granulatiry does the JITter work? Does it always compile entire
assemblies? Or just entire classes? Or method-by-method as needed?

However, you are essentially correct: if you never make any reference
to MyClass, even its static members will never be initialized. If you
never say "new MyClass", then MyClass will never occupy any heap space.
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?


Semantically, "static" provides a way to associate a variable with the
class as a whole, rather than a separate copy of the variable for each
instance. A classic application of this is the Singleton design
pattern. C also has a "static" keyword which, although it doesn't
translate directly, had the same sort of memory behaviour: static
variables persisted across method calls, and so were stored neither on
the heap nor on the stack.

Static is sort of like having a global variable, but it is more
controlled and safer. For one thing, you can make it local to your
class, strictly for internal use, so it's "global" to the class, but
not global to your program. It's also necessary in certain
circumstances, for example creating a cache to speed up certain methods
in your class. You want the cache to be shared amongst all instances of
the class rather than having each instance have its own cache.

However, "static" is _not_ like passing pointers around in C / C++.
Passing pointers as arguments means putting the pointer on the stack.
Static members live in static memory, not stack memory, so they're very
different from pointer arguments.

Dec 12 '05 #4
"Joanna Carter [TeamB]" wrote:
"Jesika" <Je****@discuss ions.microsoft. com> a écrit dans le message de news:
D8************* *************** **...icrosof t.com...

| 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.


not true. it is not the 'new' keyword that decides what goes where.

read Jon's article for more information
http://www.yoda.arachsys.com/csharp/memory.html
Dec 12 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
3149
by: Doug Farrell | last post by:
Hi all, I'm trying to do the following from within a code module: import re # text to match text = "Good morning x something /x, how are you today x something else /x"
1
1988
by: Mathias Mamsch | last post by:
Hi, I have some confusion concerning the weakref module. I am trying to save a weak reference to a bound member function of a class instance for using it as a callback function. But I always get dead references, when I try to create a reference to a bound member function. It seems as if an instance of a class owns no reference to its...
38
3638
by: Grant Edwards | last post by:
In an interview at http://acmqueue.com/modules.php?name=Content&pa=showpage&pid=273 Alan Kay said something I really liked, and I think it applies equally well to Python as well as the languages mentioned: I characterized one way of looking at languages in this way: a lot of them are either the agglutination of features or they're a...
10
1926
by: Kamilche | last post by:
I'm trying to pack two characters into a single byte, and the shifting in Python has me confused. Essentially, it should be possible to use a 'packed string' format in Python, where as long as the characters you're sending are in the ASCII range 0 to 127, two will fit in a byte. Here's the code. Can you tell what I'm doing wrong? ...
4
2535
by: JMCN | last post by:
object invalid or no longer set - confusion of the recordset in access 2003. i am currently converting from access 97 to access 2003. majority of the codes converted over perfectly fine, though my recordset did not. i read from pasts posts that some references will not convert. so i tried to change the dao 3.51 reference to dao 3.6 in...
0
1391
by: i_have_control | last post by:
I'd be grateful for any input on this one: I have three web domains. The destinations of two are set to folders on the first, though that fact is transparent to the user (i.e: it does not REDIRECT to the first domain). Example: http://www.maindomain.com (main hosted package) http://www.subdomain1.com -> www.maindomain.com/subdomain1/...
13
2079
by: Steve | last post by:
I have a form with a dataset and a datagrid. I created a dataview on this dataset. When the user modifies the datagrid, I look up this record in the dataview to make sure it is unique. Here is the confusion......... I thought that the DataView is the view from the dataset, but it seems that the dataview has the records that are in the...
10
3632
by: joelagnel | last post by:
hi friends, i've been having this confusion for about a year, i want to know the exact difference between text and binary files. using the fwrite function in c, i wrote 2 bytes of integers in binary mode. according to me, notepad opens files and each byte of the file read, it converts that byte from ascii to its correct character and...
1
3156
by: Richard Lewis Haggard | last post by:
I'm having a problem with what appears to be some sort of confusion with references. I have a single solution with a dozen projects which has been working quite nicely for a while. The references between projects in the solution were established through project references, not by browsing to an assembly DLL. All of the projects are strongly...
2
1528
by: Riaaaa | last post by:
Hello, We are doing the project in VB.Net. We had a great confusion for ASP.Net and VB.Net. Is VB.Net project performed in Microsoft Visual Studio 2005 ?? We have created forms in ASP.Net Website selecting the language as Visual Basic in Visual 2005. We want to do the VB.Net project for the central server...
0
7518
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7446
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7715
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7956
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7469
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
1
5368
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5087
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3480
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
757
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.