You can use the new
modifier like this:
public class BaseClass
{
public virtual int customerid {get; set;}
public void printname()
{
customerid = 1;
customerid.Dump();
}
}
public class DerivedClass : BaseClass
{
public new string customerid {get; set;}
public void PrintCustomerID()
{
customerid = "jshwedeX";
customerid.Dump();
}
}
This will give you the desired results, but it will also hide the property on the base class. If you referenced an instance of DerivedClass
as a BaseClass
variable, you would only be able to reference the property on the base class; not the derived class.
In other words:
BaseClass instance = new DerivedClass();
string customerId = instance.customerid; // <- this won't compile
An alternative would be to use explicit interface implementations:
public interface IBase
{
int customerid { get; set; }
}
public interface IDerived
{
string customerid { get; set; }
}
public class Derived : IBase, IDerived
{
int IBase.customerid { get; set; }
string IDerived.customerid { get; set; }
}
When your instance of Derived
is stored in a variable of type IBase
, customerid
will resolve to the int
version, and when it is stored in a variable of type IDerived
, it will resolve to the string
version:
var derived = new Derived();
IBase ibase = derived;
IDerived iderived = derived;
int id1 = ibase.customerid; // <- compiles just fine
string id2 = iderived.customerid; // <- compiles just fine
You could also use casting:
var instance = new Derived();
int id1 = ((IBase)instance).customerid;
string id2 = ((IDerived)instance).customerid;
Keep in mind that explicit interface implementations cause the implemented members to not be visible unless the variable is of the interface type:
var instance = new Derived();
var customerid = instance.customerid; // <- this won't compile
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…