/* C#: The Complete Reference by Herbert Schildt Publisher: Osborne/McGraw-Hill (March 8, 2002) ISBN: 0072134852 */ // Demonstrate ToString() using System; class MyClass { static int count = 0; int id; public MyClass() { id = count; count++; } public override string ToString() { return "MyClass object #" + id; } } public class Test { public static void Main() { MyClass ob1 = new MyClass(); MyClass ob2 = new MyClass(); MyClass ob3 = new MyClass(); Console.WriteLine(ob1); Console.WriteLine(ob2); Console.WriteLine(ob3); } }
Month: February 2011
Illustrates how to override the ToString() method
/* Mastering Visual C# .NET by Jason Price, Mike Gunderloy Publisher: Sybex; ISBN: 0782129110 */ /* Example7_7.cs illustrates how to override the ToString() method */ using System; // declare the Car class class Car { // declare the fields public string make; public string model; // define a constructor public Car(string make, string model) { this.make = make; this.model = model; } // override the ToString() method public override string ToString() { return make + " " + model; } } public class Example7_7 { public static void Main() { // create Car objects Console.WriteLine("Creating Car objects"); Car myCar = new Car("Toyota", "MR2"); Car myOtherCar = new Car("Porsche", "Boxter"); // call the ToString() method for the Car objects Console.WriteLine("myCar.ToString() = " + myCar.ToString()); Console.WriteLine("myOtherCar.ToString() = " + myOtherCar.ToString()); } }
demonstrates overriding the ToString() method to provide a custom string output
/* C# Programming Tips & Techniques by Charles Wright, Kris Jamsa Publisher: Osborne/McGraw-Hill (December 28, 2001) ISBN: 0072193794 */ // tm1.cs - demonstrates overriding the ToString() method to provide a custom // string output. // // Compile this program using the following command line: // D:>csc tm1.cs // namespace nsStructure { using System; using System.Globalization; public struct tm { public int tm_sec; // Seconds after the minute public int tm_min; // Minutes after the hour public int tm_hour; // Hours since midnight public int tm_mday; // The day of the month public int tm_mon; // The month (January = 0) public int tm_year; // The year (00 = 1900) public int tm_wday; // The day of the week (Sunday = 0) public int tm_yday; // The day of the year (Jan. 1 = 1) public int tm_isdst; // Flag to indicate if DST is in effect public override string ToString() { const string wDays = "SunMonTueWedThuFriSat"; const string months = "JanFebMarAprMayJunJulAugSepOctNovDec"; return (String.Format ("{0} {1} {2,2:00} " + "{3,2:00}:{4,2:00}:{5,2:00} {6} ", wDays.Substring (3 * tm_wday, 3), months.Substring (3 * tm_mon, 3), tm_mday, tm_hour, tm_min, tm_sec, tm_year + 1900)); } } public class tm1 { static public void Main() { DateTime timeVal = DateTime.Now; tm tmNow = LocalTime (timeVal); Console.WriteLine (tmNow); } static public tm LocalTime(DateTime tmVal) { tm time; time.tm_sec = tmVal.Second; time.tm_min = tmVal.Minute; time.tm_hour = tmVal.Hour; time.tm_mday = tmVal.Day; time.tm_mon = tmVal.Month - 1; time.tm_year = tmVal.Year - 1900; time.tm_wday = (int) tmVal.DayOfWeek; time.tm_yday = tmVal.DayOfYear; TimeZone tz = TimeZone.CurrentTimeZone; time.tm_isdst = tz.IsDaylightSavingTime (tmVal) == true ? 1 : 0; return (time); } } }
Demonstrates using the this intrinsic variable, which allows a class instance to identify itself
/* C# Programming Tips & Techniques by Charles Wright, Kris Jamsa Publisher: Osborne/McGraw-Hill (December 28, 2001) ISBN: 0072193794 */ // // This.cs -- Demonstrates using the this intrinsic variable, which // allows a class instance to identify itself // // Compile this program with the following command line: // C:>csc this.cs // namespace nsThis { using System; public class ThisclsMain { static public void Main () { // Declare an array of classes clsThis [] arr = new clsThis[] { new clsThis(), new clsThis(), new clsThis(), new clsThis(), new clsThis(), new clsThis() }; Console.WriteLine ("{0} instances were created", arr[0].m_Instance); // Ask each instance in the array to identify itself foreach (clsThis inst in arr) { Console.WriteLine ("This is instance Number " + inst.Identify().Instance); } } } internal class clsThis { public clsThis () { m_Instance = ++Count; } private static int Count = 0; public int Instance { get {return (m_Instance);} } internal int m_Instance; public clsThis Identify () { // Return this instance of the class return (this); } } }
Illustrates the use of the this object reference
/* Mastering Visual C# .NET by Jason Price, Mike Gunderloy Publisher: Sybex; ISBN: 0782129110 */ /* Example5_5.cs illustrates the use of the this object reference */ // declare the Car class class Car { public int yearBuilt; public void SetYearBuilt(int yearBuilt) { // the yearBuilt parameter hides the // the yearBuilt field this.yearBuilt = yearBuilt; } } public class Example5_5 { public static void Main() { // create a Car object Car myCar = new Car(); myCar.SetYearBuilt(2000); System.Console.WriteLine("myCar.yearBuilt = " + myCar.yearBuilt); } }
Using the this Object Reference
public class Product { public int yearBuilt; public void SetYearBuilt(int yearBuilt) { this.yearBuilt = yearBuilt; } } class MainClass{ public static void Main() { Product myProduct = new Product(); myProduct.SetYearBuilt(2000); System.Console.WriteLine("myProduct.yearBuilt = " + myProduct.yearBuilt); } }
Conversions Between Structs 2
/* A Programmer's Introduction to C# (Second Edition) by Eric Gunnerson Publisher: Apress L.P. ISBN: 1-893115-62-3 */ // 24 - User-Defined ConversionsConversions Between Structs // copyright 2000 Eric Gunnerson using System; using System.Text; struct RomanNumeral { public RomanNumeral(short value) { if (value > 5000) throw(new ArgumentOutOfRangeException()); this.value = value; } public static explicit operator RomanNumeral( short value) { RomanNumeral retval; retval = new RomanNumeral(value); return(retval); } public static implicit operator short( RomanNumeral roman) { return(roman.value); } static string NumberString( ref int value, int magnitude, char letter) { StringBuilder numberString = new StringBuilder(); while (value >= magnitude) { value -= magnitude; numberString.Append(letter); } return(numberString.ToString()); } public static implicit operator string( RomanNumeral roman) { int temp = roman.value; StringBuilder retval = new StringBuilder(); retval.Append(RomanNumeral.NumberString(ref temp, 1000, 'M')); retval.Append(RomanNumeral.NumberString(ref temp, 500, 'D')); retval.Append(RomanNumeral.NumberString(ref temp, 100, 'C')); retval.Append(RomanNumeral.NumberString(ref temp, 50, 'L')); retval.Append(RomanNumeral.NumberString(ref temp, 10, 'X')); retval.Append(RomanNumeral.NumberString(ref temp, 5, 'V')); retval.Append(RomanNumeral.NumberString(ref temp, 1, 'I')); return(retval.ToString()); } private short value; } struct BinaryNumeral { public BinaryNumeral(int value) { this.value = value; } public static implicit operator BinaryNumeral( int value) { BinaryNumeral retval = new BinaryNumeral(value); return(retval); } public static implicit operator int( BinaryNumeral binary) { return(binary.value); } public static implicit operator string( BinaryNumeral binary) { StringBuilder retval = new StringBuilder(); return(retval.ToString()); } private int value; } public class ConversionsBetweenStructs1 { public static void Main() { RomanNumeral roman = new RomanNumeral(12); BinaryNumeral binary; binary = (BinaryNumeral)(int)roman; } }