Generics, Dictionaries, and More - 4.12 Initializing Generic Variables to Their Default Values
(Page 4 of 4 )
Problem
You have a generic class that contains a variable of the same type as the type parameter defined by the class itself. Upon construction of your generic object, you want that variable to be initialized to its default value.
Solution
Simply use the default keyword to initialize that variable to its default value:
public class DefaultValueExample<T>
{
T data = default(T);
public bool IsDefaultData()
{
T temp = default(T);
if (temp.Equals(data))
{
return (true);
}
else
{
return (false);
}
}
public void SetData(T val)
{
data = val;
}
}
The code to use this class is shown here:
public static void ShowSettingFieldsToDefaults()
{
DefaultValueExample<int> dv = new DefaultValueExample<int>();
// Check if the data is set to its default value; true is returned.
bool isDefault = dv.IsDefaultData();
Console.WriteLine("Initial data: " + isDefault);
// Set data.
dv.SetData(100);
// Check again, this time a false is returned.
isDefault = dv.IsDefaultData();
Console.WriteLine("Set data: " + isDefault);
}
The first call toIsDefaultDatareturnstrue, while the second returnsfalse. The output is shown here:
Initial data: True
Set data: False
Discussion
When initializing a variable of the same type parameter as the generic class, you cannot just set that variable to null. What if the type parameter is a value type such as an int or char? This will not work because value types cannot be null. You may be thinking that anullabletype such aslong?orNullable<long>can be set tonull(see Recipe 4.6 for more on nullable types). However, the compiler has no way of knowing what type argument the user will use to construct the type.
Thedefaultkeyword allows you to tell the compiler that at compile time the default value of this variable should be used. If the type argument supplied is a numeric value (e.g.,int,long,decimal), then the default value is zero. If the type argument supplied is a reference type, then the default value isnull. If the type argument supplied is astruct, then the default value of thestruct is determined by initializing each member field to its default value.
See Also
Recipe 4.6, and the “default Keyword in Generic Code” topic in the MSDN documentation.
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |
|
This article is excerpted from chapter four of the C# 3.0 Cookbook, Third Edition, written by Jay Hilyard and Stephen Teilhet (O'Reilly, 2008; ISBN: 059651610X). Check it out today at your favorite bookstore. Buy this book now.
|
|