Click here to Skip to main content
15,881,812 members
Home / Discussions / C#
   

C#

 
GeneralRe: Few question about static class c# Pin
Tridip Bhattacharjee25-Mar-13 9:53
professionalTridip Bhattacharjee25-Mar-13 9:53 
GeneralRe: Few question about static class c# Pin
Marco Bertschi25-Mar-13 10:16
protectorMarco Bertschi25-Mar-13 10:16 
GeneralRe: Few question about static class c# Pin
Tridip Bhattacharjee25-Mar-13 21:31
professionalTridip Bhattacharjee25-Mar-13 21:31 
GeneralRe: Few question about static class c# Pin
Marco Bertschi25-Mar-13 22:20
protectorMarco Bertschi25-Mar-13 22:20 
AnswerRe: Few question about static class c# Pin
DaveyM6925-Mar-13 9:45
professionalDaveyM6925-Mar-13 9:45 
AnswerRe: Few question about static class c# Pin
V.25-Mar-13 23:04
professionalV.25-Mar-13 23:04 
Questionhow memory is allocated for class, class data member and member function & interface and class instance ? Pin
Tridip Bhattacharjee25-Mar-13 4:59
professionalTridip Bhattacharjee25-Mar-13 4:59 
AnswerRe: how memory is allocated for class, class data member and member function & interface and class instance ? Pin
Paulo Zemek25-Mar-13 5:28
mvaPaulo Zemek25-Mar-13 5:28 
I think there are many questions and some of them are not a simple answer, so I will try to resume.

Any code that should be loaded (like a method) will eventually use memory. I say eventually because it may not have been jitted yet, but when it is, it will use memory. But that will happen only once. It is not important if you have 1 instance or 1000 instances. The method is only once on memory.

So, for instance, only the field sizes occupy memory. An int is 4 bytes (so a class with 2 ints will occupy 8 bytes + the instance "header"). Such header is variable and I am not sure on the size... I think it is 12 bytes on 32-bit computers and 16 on 64-bit computers.
Any reference (the "pointer") is 4 bytes on 32-computers and 8 bytes on 64-bit computers.

But when you have a string field, in you class you will have 4 or 8 bytes for the reference (a null reference still uses such bytes, as zeros). Then, if you have the string filled, you will have: 4 or 8 bytes to reference a string.
Such string will have 12/16 bytes of its header + the length of the string * 2 (each character is 2 bytes) + some extra info on the string itself (I think the hashcode, for example, is always stored).


Also, virtual methods (and interfaces are included) occupy memory for their vtable... again, it is once for the class, it is not important if you have 1 instance or 1 million.


About stack and heap... in fact, any value type can be stored in the stack, and the reference can also be stored on stack... but when you already have an objet with an int, such in is not on the stack... it is already on the heap. So that means: value types and the reference themselves are kept on the "actual" location (stack or heap) while the created reference type object is always on the heap, at another location.


About the static classes, there are some scenarios.
First, a static class can never be instantiated. It looks better to create a static class than to create a normal class with a private constructor to avoid it from being instantiated.
Second, extension methods require static classes. You can't put them on non-static classes, so you may have such need.

Aside from that, there is no real difference. A static method on a normal class is as static as a static method on a static class. It is simple a matter of design choices.
GeneralRe: how memory is allocated for class, class data member and member function & interface and class instance ? Pin
Tridip Bhattacharjee25-Mar-13 9:26
professionalTridip Bhattacharjee25-Mar-13 9:26 
GeneralRe: how memory is allocated for class, class data member and member function & interface and class instance ? Pin
Paulo Zemek25-Mar-13 9:46
mvaPaulo Zemek25-Mar-13 9:46 
QuestionAny C# library for Forward error correction. Pin
iamraghusunkara25-Mar-13 4:33
iamraghusunkara25-Mar-13 4:33 
AnswerRe: Any C# library for Forward error correction. Pin
Marco Bertschi25-Mar-13 6:17
protectorMarco Bertschi25-Mar-13 6:17 
AnswerRe: Any C# library for Forward error correction. Pin
unclepaul25-Mar-13 10:44
unclepaul25-Mar-13 10:44 
GeneralRe: Any C# library for Forward error correction. Pin
iamraghusunkara25-Mar-13 19:55
iamraghusunkara25-Mar-13 19:55 
QuestionRemote desktop using VNC Pin
superselector25-Mar-13 1:27
superselector25-Mar-13 1:27 
AnswerRe: Remote desktop using VNC Pin
Richard MacCutchan25-Mar-13 1:35
mveRichard MacCutchan25-Mar-13 1:35 
AnswerRe: Remote desktop using VNC Pin
Eddy Vluggen25-Mar-13 2:03
professionalEddy Vluggen25-Mar-13 2:03 
AnswerRe: Remote desktop using VNC Pin
Marco Bertschi25-Mar-13 6:22
protectorMarco Bertschi25-Mar-13 6:22 
Questiondelegate difficulty Pin
Member 993942324-Mar-13 21:45
Member 993942324-Mar-13 21:45 
AnswerRe: delegate difficulty Pin
Abhinav S24-Mar-13 22:29
Abhinav S24-Mar-13 22:29 
GeneralRe: delegate difficulty Pin
Member 993942325-Mar-13 3:42
Member 993942325-Mar-13 3:42 
GeneralRe: delegate difficulty Pin
PIEBALDconsult25-Mar-13 5:01
mvePIEBALDconsult25-Mar-13 5:01 
AnswerRe: delegate difficulty Pin
Paulo Zemek25-Mar-13 5:34
mvaPaulo Zemek25-Mar-13 5:34 
QuestionHow to use databindings class in web application? Pin
behrad kiani24-Mar-13 6:56
behrad kiani24-Mar-13 6:56 
AnswerRe: How to use databindings class in web application? Pin
Dave Kreskowiak24-Mar-13 7:33
mveDave Kreskowiak24-Mar-13 7:33 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.