Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Thursday, September 2, 2010 1:37 PM | 1 vote
Hi all
Almost every time I've thought "oh, that's a good place to use a struct instead of a class", I've realised that I require specific default values for the members.
So beacuse you cannot provide custom default values for any struct members with the default constructor, I seems to never really have any situations where I can use them.
Can anyone give me some tips on structs - because currently I just don't seem to be using them, and feel like perhaps I should be.
Is there any actual reason why you can't provide default values? I know it's probably because you can create a struct without using the "new" keyword... but can't it just force the struct to set the default values of its members on creation?
Thanks!
All replies (7)
Thursday, September 2, 2010 2:10 PM âś…Answered
Firstly you are talking about structs but what you really mean are value types. C# treats a struct as a value type but other languages don't (C++ comes to mind). Hence the following discussion applies to value types of which C# structs are just one implementation.
Actually structs are not very common in .NET. They are ideal when you just need to group some fields together but the restrictions on them make them generally unuseful. You do gain a slight performance increase when working with structs (along with automatic, inefficient, copying and equality functionality) but they are still generally not used. The general recommendation is to start with a class and then step back to a struct if you have some pretty specific requirements such as: value semantics, immutable and performance.
In almost all cases if you decide to go the struct route you want to ensure the struct is immutable (including its children), specifically implements equality and does little beyond store values. If you can't ensure these things then a struct is not appropriate. In the framework itself (excluding the primitives) only a few types are structs. DateTime and TimeSpan come to mind. Refer to the book Effective C# by Bill Wagner for the guidelines for structs. You can also look at the Framework Design Guidelines for more information.
Michael Taylor - 9/2/2010
http://msmvps.com/blogs/p3net
Thursday, September 2, 2010 2:13 PM
Is there any actual reason why you can't provide default values? I know it's probably because you can create a struct without using the "new" keyword... but can't it just force the struct to set the default values of its members on creation?
In addition, because a struct is a value type, Each value type has an implicit default constructor that initializes the default value of that type.
http://msdn.microsoft.com/en-us/library/s1ax56ch(v=VS.71).aspx
Thursday, September 2, 2010 2:27 PM
Actually structs are not very common in .NET. They are ideal when you just need to group some fields together but the restrictions on them make them generally unuseful. You do gain a slight performance increase when working with structs (along with automatic, inefficient, copying and equality functionality) but they are still generally not used.
Ah, thank you - that answers my question nicely :)
So I'm quite right to not be using them, excellent.
Thanks!
Thursday, September 2, 2010 2:28 PM
try like this
public struct struMyClsPosition
{
public bool IsValid;
public bool IsEditable;
public struMyClsPosition(object _object)
{
IsValid = false;
IsEditable = false;
}
}
Thursday, September 2, 2010 2:48 PM
C# treats a struct as a value type but other languages don't (C++ comes to mind).
Unmanaged C++ certainly treats structs (and classes) as value types. C++ doesn't know what a reference type is.
Thursday, September 2, 2010 2:50 PM
I was referring to C++/CLI. In C++/CLI a class and struct can be either a value or a reference type. Unlike C# it is not based strictly on whether it is a struct or class.
Michael Taylor - 9/2/2010
http://msmvps.com/blogs/p3net
Thursday, September 2, 2010 3:55 PM
Either way, I think using the word "struct" in a C# forum is totally unambiguous.