class human { public int age; public string sex; public human(int a, string s) {age=a;sex=s;} public string isex { get { return sex; } set { sex = value; } } } human James = new human(28,"male"); //James is an object of class human
James.isex = "female"; //set sex to female System.Console.Write(James.isex); //female
human Mary = new human(26,"female"); human[] arr = new human[] {James,Mary}; //---------------------------------- using System.Collections.Generic; List<human> lst = new List<human> {James,Mary};
public struct classmate { public string name; public string sex; public string knowsex { get { return sex;} set { sex = value;} } public string classmate(int n, string s) { name = n; sex = s; } } classmate Jacobs = new classmate("Jacobs","male");
Although both struct object and class object can be created by new operator, because structs are value types, a new struct object can also be created by copy. While class objects are reference types, the copy of a class object has a reference to the same address as the original one. Which means if you change the property of the copy object, the property value of the original object will be changed too.
classmate Jacobs = new classmate("Jacobs","male"); //Jacobs is a struct object classmate Sophia = Jacobs; Sophia.knowsex = "female"; System.Console.WriteLine(Jacobs.sex); //male, unchanged //------------------------------------- human James = new human(28,"male"); //James is an object of class human human Mary = James; Mary.isex = "female"; System.Console.WriteLine(James.sex); //female, changed