I have a class that inherits from X509Certificate2. I want the NotAfter property to be in UTC rather than local time. I\'m pretty new to C# and was wondering if what I have
Methods/Properties in C# are not virtual by default (like they are in Java). Are you sure your NotAfter property is defined as virtual in the base class?
No, this way you are just hiding the property with a new one.
Please use "override" instead of "new". See here for details on the "new" keyword as modifier: http://msdn.microsoft.com/en-us/library/435f1dw2.aspx
EDIT: As mentionned in the comment below, the property in X509Certificate2 is not marked as virtual, so overriding it is not possible in this case.
What you have done there is member hiding. If the class you are deriving from has marked the property as virtual
, or is overriding it from it's base (if it has one) you use the override
keyword:
public override DateTime NotAfter
The member hiding can be used when the base class has marked it virtual
, however if someone cast a reference of your class into the base class and accessed the member, they would bypass your new
hiding. With true inheritance using override
, this problem does not occur.
As has been noted by someone, this property is not marked virtual
:
http://msdn.microsoft.com/en-us/library/system.security.cryptography.x509certificates.x509certificate2.notafter.aspx
Member hiding will allow you to get around this if people use your class directly, but the moment someone casts your class back to a base type, they get the base value:
class MyClass : Cert...
MyClass c = new MyClass();
DateTime foo = c.NotAfter; // Your newly specified property.
Cert cBase = (Cert)c;
foo = cBase.NotAfter; // Oops, base value. Inheritance cures this, but only with virtual members.