This article has been included in the "C # Illustrated Tutorial" Reading Notes directory sticker, click to access the directory for more content.
One, the interface that little thing
(1) What is an interface?
A set of function members that are not implemented by reference types . Only classes and structs can implement interfaces.
(2) See the interface instance from the IComparable interface:
Suppose you have the following code, which uses a static method of the array class to sort an unordered array of type int and output the sorted result.
Using System;class program{ static void Main () { var myInt = new[] {4, 9, 2}; Create an array of ints. Array.Sort (myInt); Sort elements by magnitude. foreach (var i in myInt) //Print them out. Console.Write ("{0}", i); }}
The sort method does a good job of sorting an array of type int, but if we try to use it on a custom class, an exception will occur, such as the following MyClass class.
Class myclass{public int thevalue;}
Sort why the MyClass cannot be sorted because it does not know how to compare custom objects and how to sort them. The sort method of the array class actually relies on an IComparable interface, which is declared in the BCL and contains a unique CompareTo method. It takes a parameter of type object and can match any reference type.
public interface icomparable{ int CompareTo (object obj);}
Now, we know that the int type implements the IComparable interface by default, and our MyClass does not. Therefore, we need to implement this IComparable interface with MyClass.
Class myclass:icomparable {public int thevalue; public int CompareTo (object obj) { MyClass mc = (MyClass) obj; if (this. Thevalue < MC. thevalue) return-1; if (this. Thevalue > MC. Thevalue) return 1; return 0;} }
Now, the MyClass class implements the IComparable interface, which can be used for the sort method.
Class program{ static void PrintOut (string s, myclass[] mc) { Console.Write (s); foreach (var m in MC) Console.Write ("{0}", m.thevalue); Console.WriteLine (""); } static void Main () { var myInt = new[] {4, 9, 2}; myclass[] Mcarr = new Myclass[5]; for (int i = 0; i < 5; i++) { Mcarr[i] = new MyClass (); Mcarr[i]. Thevalue = Myint[i]; } PrintOut ("Initial Order:", Mcarr); Array.Sort (Mcarr); PrintOut ("Sorted Order: ", Mcarr); }}
Now, a complete interface instance has been completed.
(3) Precautions for using the interface:
① declaration interface: cannot contain: data member, static member; only: Method, property, event, indexer;
TIP: The interface allows any access modifiers, but the interface members are implicitly public and do not allow any access modifiers, including public.
② when implementing an interface: include the interface name in the base class list, and implement the interface for each member of the interface;
(4) An interface is a reference type : We cannot access the interface directly through the members of the class or object, however, we can get a reference to the interface by converting the class object into an interface type. Once a reference to the interface is available, we can use the dot number to invoke the method of the interface.
Using System;interface iifc1{ void PrintOut (string s);} Class myclass:iifc1{public void PrintOut (string s) { Console.WriteLine ("Calling through: {0}", s); C6/>}}class program{ static void Main () { MyClass mc = new MyClass (); Mc. PrintOut ("Object"); IIFC1 IFC = (IIFC1) MC; Ifc. PrintOut ("interface");} }
Let's look at the allocation of the above code in memory:
(5) interface and as Operator = = Natural pair
When using interface references in the past, we tend to use coercion type conversions, but forcing type conversions throws an exception (an exception is an unexpected error in your code that can severely degrade code speed). How to avoid this problem, we can use the AS operator, no exception is thrown when the class object does not implement the interface, and only null is returned.
Two, see me 72 change: conversion
(1) Essence: The process of accepting a value of a type and using it as another type of value ;
(2) Conversion classification:
① pre-defined conversions: numbers, boxing/unpacking, reference conversions;
Conversions for numeric types are described in:
Packing/Unpacking is a more important point, now let's take a look at:
Boxing (boxing) is a reference type, value type, which is essentially a replica creation . Boxing is an implicit conversion that takes a value of a value type, creates a complete reference type object on the heap and returns an object reference based on that value.
A unboxing (unboxing) is a value type, a reference type, that essentially converts a boxed object back to a value type. The unboxing is the display transformation.
② user-defined conversions: implicit and displayed custom transformations;
View Code
(3) is operator:
Some conversions are unsuccessful during the conversion process, and a InvalidCastException exception is thrown at run time. We can use the IS operator to check if the conversion succeeds, thus avoiding blindly trying the conversion .
Mind Map of this chapter
Attachment
Mind Mapping (JPG, PDF, and mmap source files) Download: Http://pan.baidu.com/s/1qWNOGGW
Zhou Xurong
Source: http://www.cnblogs.com/edisonchou/
The copyright of this article is owned by the author and the blog Park, welcome reprint, but without the consent of the author must retain this paragraph, and in the article page obvious location to give the original link.
C # Schematic tutorial Six reading notes: Interfaces and transformations