Linked Questions
31 questions linked to/from Does using "new" on a struct allocate it on the heap or stack?
5 votes
4 answers
7k views
when is a struct too big? [duplicate]
Possible Duplicate: Structure Vs Class in C# Sorry if this is a open ended question, but I just want to know if my struct is too big. The reason why i want to use a struct is because I've learned ...
0 votes
2 answers
1k views
Struct on the heap? [duplicate]
So I have this: class SimpleDateStructureDemo { struct Date { public int year; public int month; public int day; } static void Main() { ... } } And then I see an ...
342 votes
16 answers
126k views
When should I use a struct instead of a class?
MSDN says that you should use structs when you need lightweight objects. Are there any other scenarios when a struct is preferable over a class? Some people might have forgotten that: structs can ...
309 votes
13 answers
154k views
Why can't I define a default constructor for a struct in .NET?
In .NET, a value type (C# struct) can't have a constructor with no parameters. According to this post this is mandated by the CLI specification. What happens is that for every value-type a default ...
59 votes
17 answers
26k views
Is everything in .NET an object?
Please help us settle the controversy of "Nearly" everything is an object (an answer to Stack Overflow question As a novice, is there anything I should beware of before learning C#?). I thought that ...
64 votes
6 answers
47k views
Fixed point math in C#
Are there some good resources for fixed point math in C#? I've seen things like this (http://2ddev.72dpiarmy.com/viewtopic.php?id=156) and this (What's the best way to do fixed-point math?), and a ...
24 votes
3 answers
61k views
C#, struct vs class, faster? [duplicate]
Possible Duplicate: Which is best for data store Struct/Classes? Consider the example where I have an Employee object with attributes like age, name, gender, title, salary. I now have a list i ...
54 votes
3 answers
4k views
Can DateTime tear in a 64 bit environment?
In C# setting a value to a variable is atomic as long as its size is at most native int (i.e. 4 bytes in a 32-bit runtime environment and 8 bytes on a 64-bit one). In a 64-bit environment that ...
29 votes
1 answer
10k views
Parameterless constructors in structs for C# 6
My understanding is that Parameterless constructors in structs are now allowed. But the following gives me a compile error in VS 2015 Community public struct Person { public string Name { get; ...
22 votes
4 answers
8k views
Does it make sense to define a struct with a reference type member?
Is there any sense in defining a struct with a reference type member (and not defining it as a class)? For example, to define this struct: public struct SomeStruct { string name; Int32 place;...
12 votes
5 answers
3k views
Dilemma with using value types with `new` operator in C#
When operator new() is used with reference type, space for the instance is allocated on the heap and reference variable itself is placed on the stack. Besides that, everything within the instance of ...
17 votes
2 answers
13k views
Memory usage in .NET when creating a new class or struct
Int has a size of 4 bytes, if I create a new Int in my program will incresse its memory consumption by 4 bytes. Right? But if I have this class public class Dummy{ private int; } How much memory ...
18 votes
5 answers
1k views
Unintuitive behaviour with struct initialization and default arguments
public struct Test { public double Val; public Test(double val = double.NaN) { Val = val; } public bool IsValid { get { return !double.IsNaN(Val); } } } Test myTest = new Test(); bool ...
6 votes
1 answer
4k views
CS8983 A 'struct' with field initializers must include an explicitly declared constructor
struct vvvv { public int j = 8; //public vvvv() { } error } class cccc { public int f = 8; } In the struct if I comment out the constructor the compiler tells me that the field j ...
4 votes
3 answers
929 views
How can i retrieve the default value of a given structure in VB.net?
If I execute the following statement: dim defaultDate as Date = Nothing defaultDate contains the default value of the Date structure I suspect there is a clean way to retreive this value, like some ...