public static void Main() { MyBaseClass myBase = new MyBaseClass(); MyDerivedClass myDerived = new MyDerivedClass(); object o = myDerived; //MyBaseClass b = myDerived; var b = myDerived; Console.WriteLine("myBase: Type is {0}", myBase.GetType()); Console.WriteLine("myDerived: Type is {0}", myDerived.GetType()); Console.WriteLine("object o = myDerived: Type is {0}", o.GetType()); Console.WriteLine("MyBaseClass b = myDerived: Type is {0}", b.GetType()); Console.WriteLine("-----------------------------------------"); int n1 = 12; int n2 = 82; long n3 = 12; Console.WriteLine("n1 and n2 are the same type: {0}", Object.ReferenceEquals(n1.GetType(), n2.GetType())); Console.WriteLine("n1 and n3 are the same type: {0}", Object.ReferenceEquals(n1.GetType(), n3.GetType())); Console.WriteLine("-----------------------------------------"); object[] values = { (int)12, (long)10653, (byte)12, (sbyte)-5, 16.3, "string" }; foreach (var value in values) { Type t = value.GetType(); if (t.Equals(typeof(byte))) { Console.WriteLine("The value: {0} is an unsigned byte.", value); } else if (t.Equals(typeof(sbyte))) { Console.WriteLine("The value: {0} is a signed byte.", value); } else if (t.Equals(typeof(int))) { Console.WriteLine("The value: {0} is a 32-bit integer.", value); } else if (t.Equals(typeof(long))) { Console.WriteLine("The value: {0} is a 32-bit integer.", value); } else if (t.Equals(typeof(double))) { Console.WriteLine("The value: {0} is a double-precision floating point.", value); } else { Console.WriteLine("The value: '{0}' is another data type.", value); } } }
public static void Main() { MyBaseClass myBase = new MyBaseClass(); MyDerivedClass myDerived = new MyDerivedClass(); object o = myDerived; MyBaseClass b = myDerived; Console.WriteLine("mybase: Type is {0}", myBase.GetType()); Console.WriteLine("myDerived: Type is {0}", myDerived.GetType()); Console.WriteLine("object o = myDerived: Type is {0}", o.GetType()); Console.WriteLine("MyBaseClass b = myDerived: Type is {0}", b.GetType()); }
public static void Retrieve(this MyBaseClass entity, int ID) { string className = entity.GetType().Name; Database db = DatabaseFactory.CreateDatabase(); DbCommand dbCommand = db.GetStoredProcCommand(String.Format("{0}s_Retrieve_{0}", className)); db.AddInParameter(dbCommand, String.Format("@{0}ID", className), DbType.Int32, ID); using (IDataReader dr = db.ExecuteReader(dbCommand)) { if (dr.Read()) { BOLoader.LoadDataToProps(this, dr); } } }
public override int CompareTo(MyBaseClass other) { if (other == null) { return(1); } var typedOther = other as MyBaseClass; if (typedOther != null) { return(this.SomeValue.CompareTo(typedOther.SomeValue)); } else { return(GetType().FullName.CompareTo(other.GetType().FullName)); } }
public override int CompareTo(MyBaseClass other) { if (other == null) { // every instance comes after null, cf. docs return(1); } var typedOther = other as MyBaseClass; if (typedOther != null) { // other instance of same type; compare by custom sorting criteria return(this.SomeValue.CompareTo(typedOther.SomeValue)); } else { // other instance of different type; make sure different types are always sorted in the same order return(GetType().FullName.CompareTo(other.GetType().FullName)); } }
static void Main(string[] args) { MyBaseClass myBase = new MyBaseClass(); MyDerivedClass myDerived = new MyDerivedClass(); object o = myDerived; MyBaseClass b = myDerived; Console.WriteLine("mybase: Type is {0}", myBase.GetType()); Console.WriteLine("myDerived: Type is {0}", myDerived.GetType()); Console.WriteLine("object o = myDerived: Type is {0}", o.GetType()); // Class Type不會因此被覆蓋 Console.WriteLine("MyBaseClass b = myDerived: Type is {0}", b.GetType()); int n1 = 12; int n2 = 82; long n3 = 12; Console.WriteLine("n1 and n2 are the same type: {0}", Object.ReferenceEquals(n1.GetType(), n2.GetType())); Console.WriteLine("n1 and n3 are the same type: {0}", Object.ReferenceEquals(n1.GetType(), n3.GetType())); Console.ReadLine(); }