using System; class ContainedValue { public ContainedValue(int count) { this.count = count; } public int count; } class MyObject: ICloneable { public MyObject(int count) { this.contained = new ContainedValue(count); } public object Clone() { Console.WriteLine("Clone"); return(new MyObject(this.contained.count)); } public ContainedValue contained; } public class SystemArrayandtheCollectionClassesICloneable { public static void Main() { MyObject my = new MyObject(33); MyObject myClone = (MyObject) my.Clone(); Console.WriteLine( "Values: {0} {1}", my.contained.count, myClone.contained.count); myClone.contained.count = 15; Console.WriteLine( "Values: {0} {1}", my.contained.count, myClone.contained.count); } }
Class Interface
Demonstrate ICloneable
/* C#: The Complete Reference by Herbert Schildt Publisher: Osborne/McGraw-Hill (March 8, 2002) ISBN: 0072134852 */ // Demonstrate ICloneable. using System; class X { public int a; public X(int x) { a = x; } } class Test : ICloneable { public X o; public int b; public Test(int x, int y) { o = new X(x); b = y; } public void show(string name) { Console.Write(name + " values are "); Console.WriteLine("o.a: {0}, b: {1}", o.a, b); } // Make a deep copy of the invoking object. public object Clone() { Test temp = new Test(o.a, b); return temp; } } public class CloneDemo { public static void Main() { Test ob1 = new Test(10, 20); ob1.show("ob1"); Console.WriteLine("Make ob2 a clone of ob1."); Test ob2 = (Test) ob1.Clone(); ob2.show("ob2"); Console.WriteLine("Changing ob1.o.a to 99 and ob1.b to 88."); ob1.o.a = 99; ob1.b = 88; ob1.show("ob1"); ob2.show("ob2"); } }
Copy a class
/* C#: The Complete Reference by Herbert Schildt Publisher: Osborne/McGraw-Hill (March 8, 2002) ISBN: 0072134852 */ // Copy a class. using System; // Define a structure. class MyClass { public int x; } // Now show a class object assignment. public class ClassAssignment { public static void Main() { MyClass a = new MyClass(); MyClass b = new MyClass(); a.x = 10; b.x = 20; Console.WriteLine("a.x {0}, b.x {1}", a.x, b.x); a = b; b.x = 30; Console.WriteLine("a.x {0}, b.x {1}", a.x, b.x); } }
declare a class Address containing the data members to describe a US address along with the member functions
using System; class MainClass { public static void Main(string[] args) { Address addr = new Address(); addr.SetStreet(123, "My Street"); addr.SetCity("A", "X", 123456); Console.WriteLine(addr.GetStreetString()); Console.WriteLine(addr.GetCityString()); addr.Output(); } } class Address { public int nAddressNumberPart; public string sAddressNamePart; public string sCity; public string sState; public int nPostalCode; public void SetStreet(int nNumber, string sName) { nAddressNumberPart = nNumber; sAddressNamePart = sName; } public string GetStreetString() { return nAddressNumberPart + " " + sAddressNamePart; } public void SetCity(string sCityIn, string sStateIn, int nPostalCodeIn) { sCity = sCityIn; sState = sStateIn; nPostalCode = nPostalCodeIn; } public string GetCityString() { return sCity + ", " + sState + " " + nPostalCode; } public string GetAddressString() { return GetStreetString() + " " + GetCityString(); } public void Output() { Console.WriteLine(GetAddressString()); } }
Static class variable
/* Learning C# by Jesse Liberty Publisher: O'Reilly ISBN: 0596003765 */ using System; namespace Test_Console_App_3 { // declare a Cat class // stripped down class Cat { // a private static member to keep // track of how many Cat objects have // been created private static int instances = 0; private int weight; private String name; // cat constructor // increments the count of Cats public Cat(String name, int weight) { instances++; this.name = name; this.weight = weight; } // Static method to retrieve // the current number of Cats public static void HowManyCats() { Console.WriteLine("{0} cats adopted", instances); } public void TellWeight() { Console.WriteLine("{0} is {1} pounds", name, weight); } } public class StaticInClassTester { public void Run() { Cat.HowManyCats(); Cat frisky = new Cat("Frisky", 5); frisky.TellWeight(); Cat.HowManyCats(); Cat whiskers = new Cat("Whisky", 7); whiskers.TellWeight(); Cat.HowManyCats(); } static void Main() { StaticInClassTester t = new StaticInClassTester(); t.Run(); } } }
Class variables with default value
/* Learning C# by Jesse Liberty Publisher: O'Reilly ISBN: 0596003765 */ using System; public class MyTimeWithDefaultValue { // private member variables int year; int month; int date; int hour; int minute; int second = 30; // public method public void DisplayCurrentMyTimeWithDefaultValue() { System.Console.WriteLine("{0}/{1}/{2} {3}:{4}:{5}", month, date, year, hour, minute, second); } // constructor public MyTimeWithDefaultValue(int theYear, int theMonth, int theDate, int theHour, int theMinute) { year = theYear; month = theMonth; date = theDate; hour = theHour; minute = theMinute; } } public class Tester { static void Main() { MyTimeWithDefaultValue timeObject = new MyTimeWithDefaultValue(2005,3,25,9,35); timeObject.DisplayCurrentMyTimeWithDefaultValue(); } }
A Simple Class and Objects
public class Product { public string make; public string model; public string color; public int yearBuilt; public void Start() { System.Console.WriteLine(model + " started"); } public void Stop() { System.Console.WriteLine(model + " stopped"); } } class MainClass{ public static void Main() { Product myProduct; myProduct = new Product(); myProduct.make = "Toyota"; myProduct.model = "MR2"; myProduct.color = "black"; myProduct.yearBuilt = 1995; System.Console.WriteLine("myProduct details:"); System.Console.WriteLine("myProduct.make = " + myProduct.make); System.Console.WriteLine("myProduct.model = " + myProduct.model); System.Console.WriteLine("myProduct.color = " + myProduct.color); System.Console.WriteLine("myProduct.yearBuilt = " + myProduct.yearBuilt); myProduct.Start(); myProduct.Stop(); Product redPorsche = new Product(); redPorsche.make = "Porsche"; redPorsche.model = "Boxster"; redPorsche.color = "red"; redPorsche.yearBuilt = 2000; System.Console.WriteLine( "redPorsche is a " + redPorsche.model); System.Console.WriteLine("Assigning redPorsche to myProduct"); myProduct = redPorsche; System.Console.WriteLine("myProduct details:"); System.Console.WriteLine("myProduct.make = " + myProduct.make); System.Console.WriteLine("myProduct.model = " + myProduct.model); System.Console.WriteLine("myProduct.color = " + myProduct.color); System.Console.WriteLine("myProduct.yearBuilt = " + myProduct.yearBuilt); } }