C # covariant and invert,

Source: Internet
Author: User

C # covariant and invert,

The conversion from a subclass to a parent class is a covariant, identified by the out keyword. The conversion from a parent class to a subclass is an inversion, and the in keyword is used.
 

Application of covariant and Inverter

 

I. array covariant

 

Animal[] animalArray =  new  Dog[]{};  Note: The declared array data type is Animal, but the Dog array is actually assigned when the value is assigned. Every Dog object can be safely converted to Animal. The transformation from Dog to Animal method changes upwards along the inheritance chain, so it is a covariant. Ii. Coordination and inverter in Delegation 1. Change in Delegation   C # code Replication
// The Return Value of the delegate definition is Animal. The type is the parent class public delegate Animal GetAnimal (); // the return value in the delegate method implementation is Dog, and the return value is the subclass static Dog GetDog () {return new Dog ();} // The returned value of GetDog is Dog, and Dog is a subclass of Animal. Returning a Dog is equivalent to returning an Animal; therefore, the value assigned to the delegate is valid GetAnimal getMethod = GetDog;

 

2. Inverter in Delegation

C # code Replication
// The parameter type defined in the delegate is Dogpublic delegate void FeedDog (Dog target); // The parameter type in the actual method is Animalstatic void FeedAnimal (Animal target) {} // FeedAnimal is a valid method for FeedDog delegation, because the parameter type accepted by the delegation is Dog, while FeedAnimal accepts animal, and Dog can be implicitly converted to Animal, therefore, the delegate can safely perform type conversion and correctly execute the delegate method; FeedDog feedDogMethod = FeedAnimal; // defines the parameter when the delegate is a subclass, in fact, the parameter of the delegate method is a broader parent class Animal, which changes the parent class to the subclass direction and is an inverter.

 

Iii. Coordination and inverter of generic delegation

 

1. Inverter in generic delegation

C # code Replication
// Delegate statement: public delegate void Feed <in T> (T target); // The Feed delegate accepts a generic type T, note that there is an in keyword in the angle brackets of the generic type, the function of this keyword is to tell the compiler that the type T may be converted to an inverter when assigning a value to the delegate // declare a delegate Feed whose T is Animal <Animal> feedAnimalMethod = a => Console. writeLine ("Feed animal lambda"); // assign the T-as-Animal Delegate to the T-as-Dog delegate variable. This is legal because the in keyword is used to define a generic delegate, if the in keyword is removed, the compiler considers it illegal to Feed <Dog> feedDogMethod = feedAnimalMethod;

 

2. covariant in generic delegation

C # code Replication
// Delegate Declaration public delegate T Find <out T> (); // The Find delegate returns an instance of the generic type T, which has an out keyword in the angle brackets of the generic type, this keyword indicates that the T type may need to be changed. // declare the Find <Dog> delegate Find <Dog> findDog = () => new Dog (); // declare the Find <Animal> delegate and assign the findDog value to findAnimal. The type T is converted from Dog to Animal and the type T is changed to Find <Animal> findAnimal = findDog;

 

Iv. Coordination and inverter in generic Interfaces

 

1. Inverter in generic Interfaces

C # code Replication
// Interface Definition: public interface IFeedable <in T> {void Feed (T t);} // an in keyword exists before the interface generic T, to indicate that this generic interface may be used as an inverter // the following generic type FeedImp <T> to implement the above generic interface; note that the key word inpublic class FeedImp <T>: IFeedable <T> {public void Feed (T t T) {Console. writeLine ("Feed Animal"); }}// use the API for inverter: IFeedable <Dog> feedDog = new FeedImp <Animal> (); // The above Code assigns the FeedImp <Animal> type to the IFeedable <Dog> variable. The Animal is changed to Dog, so it is an inverter.

 

2. covariant in generic Interfaces

C # code Replication
// Interface Definition: public interface IFinder <out T> {T Find ();} // The out keyword is used before the generic T of the generic interface to indicate that this interface may be subject to covariant. the following generic interface implementation class public class Finder <T>: IFinder <T> where T: new () {public T Find () {return new T () ;}// use covariant. The generic type of IFinder is Animal, however, because of the out keyword, I can assign the Finder <Dog> to IFinder <Animal> finder = new Finder <Dog> ();

A simple program of C language Bubble Sorting

Main ()
{
Int I, j, temp;
Int a [10];
For (I = 0; I <10; I ++)
Scanf ("% d,", & a [I]);
For (j = 0; j <= 9; j ++)
{For (I = 0; I <10-j; I ++)
If (a [I]> a [I + 1])
{Temp = a [I];
A [I] = a [I + 1];
A [I + 1] = temp ;}
}
For (I = 1; I <11; I ++)
Printf ("% 5d,", a [I]);
Printf ("\ n ");
}

--------------
Bubble Algorithm
Algorithm Analysis and Improvement of Bubble Sorting
The basic idea of exchanging sorting is to compare the keywords of the records to be sorted in pairs. If the order of the two records is the opposite, the two records are exchanged until there is no reverse order record.
The basic concepts of application exchange sorting include Bubble sorting and quick sorting.

Bubble Sorting

1. Sorting Method
Vertically arrange the sorted record array R [1. n]. Each record R is considered as a bubble with the weight of R. key. According to the principle that a Light Bubble cannot be under a heavy bubble, scan the array R from the bottom up: Any Light Bubble scanned to a violation of this principle will make it "float" up ". This is repeated until the last two bubbles are light and heavy.
(1) initial
R [1. n] is an unordered area.

(2) First scan
The weights of two adjacent bubbles are compared from the bottom of the unordered area to the top. If the light bubbles are found to be in the lower and severe bubbles, the positions of the two bubbles are exchanged. That is, compare (R [n], R [n-1]), (R [n-1], R [N-2]),…, (R [2], R [1]); for each pair of bubbles (R [j + 1], R [j]), if R [j + 1]. key <R [j]. key, then the contents of R [j + 1] and R [j] are exchanged.
When the first scan is complete, the "lightest" bubble floated to the top of the interval, that is, the record with the smallest keyword is placed on the highest position R [1.

(3) second scan
Scan R [2. n]. When scanning is completed, the "light" bubble floated to the R [2] position ......
Finally, the sequential area R [1. n] can be obtained through n-1 scanning.
Note:
During the I-trip scan, R [1 .. I-1] and R [I.. n] are the current sequential and disordered areas, respectively. The scan continues from the bottom of the unordered area to the top of the area. When scanning is completed, the shortest bubbles in the area float to the top position R. The result is that R [1. I] is changed to a new ordered area.

2. Bubble sorting process example
Bubble Sorting of files whose keyword sequence is 49 38 65 97 76 13 27 49

3. Sorting Algorithm
(1) Analysis
Because each sort adds a bubble to the ordered area, there are n-1 bubbles in the ordered area after N-1 sort, in the disordered area, the bubble weight is always greater than or equal to the bubble weight in the ordered area. Therefore, the entire Bubble sorting process requires at most n-1 sorting.
If no bubble position exchange is found in a sorting, it means that all bubbles in the unordered area to be sorted meet the principle of being light and heavy. Therefore, the Bubble sorting process can be terminated after this sorting. Therefore, in the following algorithm, a Boolean exchange is introduced, which is set to FALSE before each sort starts. If an exchange occurs during the sorting process, set it to TRUE. Check exchange at the end of sorting. If exchange has not occurred, terminate the algorithm and no longer perform the next sorting.

(2) specific algorithms
Void BubbleSort (SeqList R)
{// R (l. n) is the file to be sorted. It uses bottom-up scanning to perform Bubble Sorting on R.
Int I, j;
Boolean exchange; // exchange flag
For (I = 1; I <G id = "1">

A simple program of C language Bubble Sorting

Main ()
{
Int I, j, temp;
Int a [10];
For (I = 0; I <10; I ++)
Scanf ("% d,", & a [I]);
For (j = 0; j <= 9; j ++)
{For (I = 0; I <10-j; I ++)
If (a [I]> a [I + 1])
{Temp = a [I];
A [I] = a [I + 1];
A [I + 1] = temp ;}
}
For (I = 1; I <11; I ++)
Printf ("% 5d,", a [I]);
Printf ("\ n ");
}

--------------
Bubble Algorithm
Algorithm Analysis and Improvement of Bubble Sorting
The basic idea of exchanging sorting is to compare the keywords of the records to be sorted in pairs. If the order of the two records is the opposite, the two records are exchanged until there is no reverse order record.
The basic concepts of application exchange sorting include Bubble sorting and quick sorting.

Bubble Sorting

1. Sorting Method
Vertically arrange the sorted record array R [1. n]. Each record R is considered as a bubble with the weight of R. key. According to the principle that a Light Bubble cannot be under a heavy bubble, scan the array R from the bottom up: Any Light Bubble scanned to a violation of this principle will make it "float" up ". This is repeated until the last two bubbles are light and heavy.
(1) initial
R [1. n] is an unordered area.

(2) First scan
The weights of two adjacent bubbles are compared from the bottom of the unordered area to the top. If the light bubbles are found to be in the lower and severe bubbles, the positions of the two bubbles are exchanged. That is, compare (R [n], R [n-1]), (R [n-1], R [N-2]),…, (R [2], R [1]); for each pair of bubbles (R [j + 1], R [j]), if R [j + 1]. key <R [j]. key, then the contents of R [j + 1] and R [j] are exchanged.
When the first scan is complete, the "lightest" bubble floated to the top of the interval, that is, the record with the smallest keyword is placed on the highest position R [1.

(3) second scan
Scan R [2. n]. When scanning is completed, the "light" bubble floated to the R [2] position ......
Finally, the sequential area R [1. n] can be obtained through n-1 scanning.
Note:
During the I-trip scan, R [1 .. I-1] and R [I.. n] are the current sequential and disordered areas, respectively. The scan continues from the bottom of the unordered area to the top of the area. When scanning is completed, the shortest bubbles in the area float to the top position R. The result is that R [1. I] is changed to a new ordered area.

2. Bubble sorting process example
Bubble Sorting of files whose keyword sequence is 49 38 65 97 76 13 27 49

3. Sorting Algorithm
(1) Analysis
Because each sort adds a bubble to the ordered area, there are n-1 bubbles in the ordered area after N-1 sort, in the disordered area, the bubble weight is always greater than or equal to the bubble weight in the ordered area. Therefore, the entire Bubble sorting process requires at most n-1 sorting.
If no bubble position exchange is found in a sorting, it means that all bubbles in the unordered area to be sorted meet the principle of being light and heavy. Therefore, the Bubble sorting process can be terminated after this sorting. Therefore, in the following algorithm, a Boolean exchange is introduced, which is set to FALSE before each sort starts. If an exchange occurs during the sorting process, set it to TRUE. Check exchange at the end of sorting. If exchange has not occurred, terminate the algorithm and no longer perform the next sorting.

(2) specific algorithms
Void BubbleSort (SeqList R)
{// R (l. n) is the file to be sorted. It uses bottom-up scanning to perform Bubble Sorting on R.
Int I, j;
Boolean exchange; // exchange flag
For (I = 1; I <G id = "1">

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.