1. Collection
is a complex type that stores multiple kinds of data.
2. Basic collection type
Dynamic array ArrayList
Listing: List
Stack stack
Dictionary Dictionary
Queues Queue
3. Dynamic array ArrayList
Initialize, can not specify size
Get length, using the Count property
Add is to use the add
Delete with Remove,removeat
Access [index]
4, List<t>
ArrayList is an unsafe type and has a performance problem with boxed unpacking, so there is a list<t>
1. Dictionary dictionary<tkey,tvalue>
A dictionary container stores a series of key-value pairs, with no value corresponding to a unique key. The meaning of the key is that we can access the value relatively efficiently by key.
2. Dictionary operation
Quantity Count
Adding Add (Key,value)
Delete Remove
Access [key]
3, Stack
Pop out of Stack
into the stack push
Get stack top element Peek
4. Queue
FIFO container.
Out of the team Dequeue
Queue Enqueue, with return value
Boxing and unpacking
1, Boxing: Creates a complete reference-type object on the heap and returns a reference to the object, based on the value of the value type, which is an implicit conversion (language auto-completion).
2, why boxing
Sometimes you need to convert a value type to a reference type (such as object) for uniform operations (such as parameters as functions) and unified storage (such as object[]).
3, Boxing simple instance
int i=3;
Object Oi=null; The
oi=i;//occurs, and the original int remains unchanged, creating a new object in the heap. Requires a performance drain.
4, the nature of boxing
is to create a copy of the reference type on the pair, the newly created reference type and the original value type are independent of each other.
Mutual Independent Code validation:
int i=3;
Object oi=i;//occurs boxing
Console.WriteLine ("I=" +i+ "oi=" +oi. ToString);//output is i=3,oi=3
oi=10;
i=7;
Console.WriteLine ("I=" +i+ ", oi=" +oi. ToString);//output is i=7,oi=10
to show that they are independent of each other
5, unpacking
The process of converting a boxed object back to a value type is a display conversion (requires manual intervention).
6, unpacking instance
in I=3;
Object oi=i;//Boxing
int j= (int) oi;//unpacking
Custom Conversions
1, Concept
for your own class definition and structure display and implicit conversions
2, why you need custom transformations
to make your own structure and type into an expected related type (e.g. I want to turn my dog into a cat), and make this transformation easier.
3, implicit conversion syntax
Note
public static implicit operator Dog (cat cat)//IMPLECIT is specified as an implicit conversion, operator is specified as a conversion, and the argument is a type that requires conversion. After operator is the target type
{
...
}
4, display conversion
public static explicit operator cat (dog dog)
5, conversion instance
public static implicit operator Cat (dog dog )//Convert the dog to cat
{
return new cat (Dog.name);
}
//Convert cat to Dog
public static explicit Opterator dog (cat cat)//Display conversion
{
return new Dog ("Tom");
}
...
Dog Dog=new Dog ("Jack");
Dog. Speak ();
Cat Cat=dog;
Cat. Spesk ();
Dog dog2= (dog) cat;
Dog2. Speak ();
1, what is overloaded operator
cannot create a new operator, so by using an existing operator, for a custom class or struct (the predefined type and the operation meaning of the existing operator are deterministic, all cannot be changed), define an operator (the operator and the operation have some relevance, such as the male Dog + Mother dog = Newborn dog), simplifying the operation of the custom type.
2, syntax details
public static dog operator + (dog Male,dog female)//such as male dog + female dog = Newborn dog
{
...
}
3, those operators can overload the
unary operator: + 、-、! , ~, + + 、--、 true, False (operand must be class and struct)
two-tuple operator: + 、-、 *,%, &, |! , ^, <<, >>, =,! =, >, <, >=, <= (two operands have at least one representation class or struct)
cannot be overloaded: =, &&, | |, [] (Index Operations), (), etc.
4, overloaded operators cannot do what
Create new operator
Change the operator syntax
redefine how operators handle predefined types
change the precedence and binding of operators
5, heavy in operator examples
...
public void Showage ()
{
Console.WriteLine ("Age=" +_age);
}
...
//Overload auto-increment operation, age for pet
public static pet Opertor + + (Pet pet)//Return value is pet type, parameter is pet itself. All overloaded methods require the public static modifier
{
++pet._age;
return pet;
}
...
Dog Dog=new Dog ("Jack");
dog._age=2;
dog++;
Dog. Showage ();
1, the generic class is a mold, put into the type of material (field property method), can shape the desired product.
2, Syntax
class cage<t>//This is a cage class and a generic class, with the class name followed by a pair of <>, plus a generic parameter
{
t[] petarray;//define an array of type T
Public void PutIn (T pet) {...} Put a pet of type T
Public t Takeout (int index) {...} When the placeholder T,cage class becomes a concrete class, t corresponds to a specific number
}
//Instantiate
var dogcage=new cage<dog> ();//Get Dog Cage
var catcage=new Cage <Cat> ();//Get a cat cage
3, why you need generics, with a base class (including all classes of the base class object) or a common interface can also implement a cage class, but the type is too broad, you need to display the type of conversion and determine what the true type is.
4, generic use: Declares a generic class--reference to a generic, constructed instance
class cage<t>{}
Class cage<dog>{}
Dogcage=new cage<dog>;
For example, do not write petcage<dog> dog=new petcage<dog> ("name"), but with a never-seen var instead. And return default<t> doesn't explain what it means.
Understanding: Generics are when you are unsure of the class's parameter type and return type, set a variable to replace the fixed type. When you create an instance of a class, you assign the value of the class type to the variable, so that you can implement a class that outputs different types of values and methods.
Do not use the teacher's example, with an array to assign the output array of examples to better understand; If you set a class, the constructor initializes an array array of type int, there is a method for assigning the array and getting the array values, note that the return value type of the array's type int and the method that gets the array value is int , if an instance of this class invokes a method, the argument and the return value type must also be int, the instance of this class is qualified, and if you want an instance of the char type to be set, you must create a new char class and method to implement it; If you use generics, variable <T> Instead of a fixed type int or char, so that when instantiated, the T is assigned different types (int, double, char) to obtain the desired return value type, thus implementing a template, only one parameter T can achieve the same function
The example given above. All you need to do is remove all <T>, and change the T to int or char to become the normal class. The comparison is easy to understand
1, what is a generic method: is the model of the method, given a specific type, you can instantiate a specific way to manipulate the type.
Note: You can have generic methods in generic classes, or you can have generic methods in a normal class.
2, generic method syntax
class dog{
void Dogishappy<t> (T target) {//generic method in normal class
...
}
}
Instance
public void ishappy<t> (T target)
{
Console.Write ("Happy" +target. ToString ())
}
Class person{}
...
var dog=new dog ("A");
Dog.ishappy<person> (new Person ());
Dog.ishappy<int> (3); The
instance object of a class is equivalent to a value of this class type,
such as int 4;4 is a value of type int,
person New person (), and new person () is a value or instance of the person type, in fact it is equivalent to creating a person class , then person person=new person (); Person is a type value for person
1. What is constraint
Constraint is the control of generics this Bronco reins! Narrow the range of generic parameters (no matter how broad, there is always a range, the smaller the better control)
2. The meaning of restraint
You can invoke a method in a generic parameter (such as T) only if you have added a constraint.
You can add constraints to both generic classes and methods.
3. Types of Constraints
Class name--the class or class that inherits the class
class--any class
struct--any value
Interface name--the interface type or any type that implements the interface
New ()--class with a non-participating constructor
4. Constraint overlay Rules (these three constraints can exist arbitrarily)
A, primary constraint, only one (class name, class,struct)
B, interface constraints, can have any number of
C, construction constraints
5. Constraint examples
void cage<t,m>
where T:pet,iclibtree,new ()//where constrains which type parameter
where M:pet,iclibtree,new ()
{···}
A delegate is an object that holds one or more methods! And the object can be executed and can be passed.
Defining delegates
delegate void Actcute ();
Declaring a delegate
Actcute del = null;
Dog dog = new Dog ("A");
Cat cat = new Cat ("B");
Let the delegate hold the method
del = dog. Wagtail;
del + = Cat. Innocentlook;
Execute delegate
Del ();
Lambda expression
1. What is an anonymous method
delegate void Actcute ();
Actcute del;
Del=delegate () {///method specific content};//declares an anonymous method
2. Lambda expression
Del=delegate () {};
Del= () =>{};
Defining delegates
public delegate void Handler ();
Defining events
public event Handler Newdog;
constructor function
Public Dog (string name): Base (name)
{
++num;
if (newdog!=null)
{
Newdog ();
}
}
Client C1 = new Client ();
Client C2 = new Client ();
Dog.newdog + = C1. Wantadog;
Dog.newdog + = c2. Wantadog;
Dog dog = new Dog ("Q");
C # learing (2)