I need use an empty value for an struct.
I don't know which is the best option to do it. I was thinking on use an static readonly field on the struct for it, but I'm not sure if it's the best option to do it or not. Maybe a method on a common class, that returns the empty, is a better option.
I'm using the struct for performance reasons, so change it to a class is not an opt开发者_Python百科ion. And I don't sure if I use the static field, the size of each struct instance will be increased.
Any ideas?
Are there any values for the struct which would naturally be invalid? In particular, you can always write:
MyStruct x = new MyStruct();
which will end up with all the bits being 0. If that's not a valid state for your struct for some reason (e.g. you've got an integer value which is always set to non-zero in the constructor) then that's great - use that as your "empty" value. You can create a static field with that value if you want:
public static readonly MyStruct Empty = new MyStruct();
Note that adding a static field will not add any overhead to instances of your struct.
If every possible bit pattern in your struct is already a valid value, however, you'll need a different approach - either adding an extra field to your struct, or adding an extra field to any pieces of code which use your struct and may need to represent a "missing" value.
You say that using a class isn't an option "for performance reasons" - is this because you've performed rigorous benchmarking of the difference in your situation?
The common idiom in .net framework is to have a static readonly field named "Empty" like Size.Empty
.
I know it's quite a late reply, but for what it's worth here is my implementation.
You will have to:
- Create a private
bool
property calledisEmpty
- Create a private constructor with a
bool
argument that assigns it toisEmpty
- Create a public static getter property called
Empty
which returns an instance of your object and calls the private constructor passing atrue
argument - Overload two operators
==
and!=
to check ifisEmpty
is true on both
Example
Let's say I have a class called ExampleClass
:
public class Example
{
public string Message;
private bool isEmpty;
public Example(string Message)
{
this.Message = Message;
isEmpty = false;
}
}
The bool
property called isEmpty
has been defined, as well as a string property and a basic Constructor to assign to the string.
A private constructor also needs to be defined which takes one boolean argument and assigns it to isEmpty
.
private Example(bool isEmpty)
{
this.isEmpty = isEmpty;
}
You will then need to define a static getter, which in this example returns a new instance of Example
by calling the private constructor and assigning isEmpty
to true.
public static Example Empty
{
get
{
return new Example(isEmpty: true);
}
}
Once you've done that, you need to overload two operators. The equals and not equals. If you don't overload these two operators you may get an error when you try to check if an instance is equal to Example.Empty
.
public static bool operator ==(Example eg1, Example eg2)
{
if (eg1.isEmpty == true && eg2.isEmpty == true) return true;
else return Example.Equals(eg1, eg2);
}
public static bool operator !=(Example eg1, eg2)
{
if (eg1.isEmpty != eg2.isEmpty) return true;
else return !Example.Equals(eg1,eg2);
}
I like CodyManix's point. Here's what I wrote:
public struct ConfCode
{
public String Agency;
public String CodeText;
public String ToolTip;
public Boolean IsActive;
public Boolean IsDefault;
public Boolean IsEmpty()
{
if (Agency == null & CodeText == null & ToolTip == null)
{ return true; }
else
{ return false; }
}
}
精彩评论