For properties that are value types, passing a null reference (
Nothing in Visual Basic) for the value parameter sets it to its default value.
For example, the following code shows that setting the Count field to a null reference (
Nothing in Visual Basic) causes it to be set to 0, the default value for the Int32 value type.
[C#]
using System;
using System.Reflection;
namespace Samples
{
public class Foo
{
public Foo()
{
Count = 10;
}
public int Count
{
get;
set;
}
}
class Program
{
static void Main(string[] args)
{
PropertyInfo property = typeof(Foo).GetProperty("Count");
Foo foo = new Foo();
Console.WriteLine("Count Before SetValue: " + foo.Count);
property.SetValue(foo, null);
Console.WriteLine("Count After SetValue: " + foo.Count);
}
}
}
The above code outputs the following:
Count Before SetValue: 10
Count After SetValue: 0