C # Learning Record 4: Set, comparison, and conversion

Source: Internet
Author: User

1. Collection Introduction: a class System. Collections. ArrayList in the System. Collections namespace also implements IList, ICollection, and IEnumerable. This class can be used to represent a list of items with variable sizes. My understanding: ArrayList can be an image that represents a variable Array, while Array that represents an immutable Array. For example, this is array [csharp] Animal [] animalArray = new ANimal [2]; Cow myCowl = new Cow ("xiaoyuan"); animalArray [0] = myCowl; animalArray [1] = new Chicken ("ken"); [csharp] <p> foreach (Animal myAnimal in animalArray) </p> <p >{</p> <p> Consale. writeLine ("New {0} object added to Array collection," + "Name = {1}", myAnimal. toString (), myAnimal. name); </p> <p >}</p> the following example: ArrayList [csharp] ArrayList animalArrayList = new ArrayList (); Cow myCow2 = new Cow ("Hayley"); animalArrayList. add (myCow2); [csharp] animalArrayList. removeAt (1); Understanding collections and arrays can be better understood than variable arrays and arrays in objective-c. 2. Comparison is Operator: Operation Syntax: <operand> is <type> determines whether an object belongs to a certain type. IComparable and IComparer interfaces IComparable: You can compare this object and another object in the object class to be compared. IComparer: Implemented in a separate class. You can compare any two objects. Generally, we use IComparable to give the default comparison code of the class, and use non-default comparison Code provided by other classes. IComparable provides the CompareTo () method (). This method accepts an object, so it can be implemented to send the Person object to him, indicating whether the Person is younger or older than the current Person. This method returns an int. if (person1.ComparteTo (person2) [this method can be directly implemented in the class] {} ICompare provides a Compare (), and can accept two objects if (personCompare. compare (person1, person2) = 0 ){.....} 3. The conversion can be implicitly converted. This can be analogous to the objective-C as operator using the following syntax to convert a type to a specified reference type: <operand> as <type>: the <operand> type is <type> type <operand> can be implicitly converted to <type> type <operand> can be boxed to the type <type> if it cannot be converted from <operand> <type>, the expression returns null. for example: ClassA obj1 = new ClassA (); ClassD obj2 = obj1 as ClassD;

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.