Wednesday, November 4, 2009

Structs and Stack Allocation

C# reference types get allocated on the heap just like Java. Unlike Java, however, you have the option of using the struct value type like all other value types (int, bool), on the stack.

Here's more detail on memory usage/location:

Sample Stack allocated struct:
public struct myReadResult {
public int BytesRead;
public bool Success;
}
...
public void ReadComplete(String theLine) {
ReadResult br; //Notice, no assignment or new keyword
br.BytesRead = theLine.Length;
br.Success = true;
return br;
}

No comments:

Post a Comment