c# learing(2)

1. A collection
is a complex type that stores a variety of data.
2. Basic collection type
dynamic array ArrayList
list: List
stack Stack
dictionary Dictionary
queue Queue
3. Dynamic array ArrayList
initialization, you can
get the length without specifying the size, use the Count attribute
to add, use Add
to delete, use Remove, RemoveAt
to access [index]
4. List <T>
ArrayList is type unsafe, and has performance problems with boxing and unboxing, so there is List<T>

1. Dictionary Dictionary<TKey, Tvalue>
The dictionary container stores a series of key-value pairs, and each value corresponds to a unique key. The significance of keys is that we can access values ​​relatively efficiently through keys.
2. The number of dictionary operations
Count
adds Add(key, value) to
delete Remove
access [key]
3.
Pop out of the
stack and Push into the stack
to get the top element Peek
4. Queue
first in first out container.
Dequeue Dequeue
into Enqueue, there is a return value

Boxing and unboxing
1. Boxing: According to the value of the value type, a complete reference type object is created on the heap and a reference to the object is returned, which is an implicit conversion (language autocomplete).
2. Why boxing?
Sometimes it is necessary to convert value types into reference types (such as object) for unified operations (such as function parameters) and unified storage (such as object[]).
3. A simple instance of boxing
int i=3;
object oi=null;
oi=i;//boxing occurs, the original int remains unchanged, and a new object is created, which is in the heap. requires performance consumption.
4. The essence of boxing
is to create a copy of the reference type on the pair, and the newly created reference type is independent of the original value type.
Mutual independent code verification:
int i=3;
object oi=i;//boxing occurs
Console.WriteLine("i="+i+",oi="+oi.ToString);//The output is i=3,oi =3
oi=10;
i=7;
Console.WriteLine("i="+i+",oi="+oi.ToString);//The output is i=7,oi=10,
indicating that the two are independent
of each other 5. Demolition
Boxing The process of converting a boxed object back to a value type, an explicit conversion (requiring manual intervention) .
6. Unboxing instance
in i=3;
object oi=i;//boxing
int j=(int)oi;//unboxing

Custom conversion
1. Concepts
are displayed and implicitly converted for their own class definitions and structures
2. Why do you need custom conversions
so that your own structure and type can become an expected related type (for example, I want to convert my dog ​​into cat), and make this transformation easier.
3. Implicit conversion syntax
Note
public static implicit operator Dog (Cat cat) //implecit is designated as implicit conversion, operator is designated as conversion, the parameter is the type to be converted, and the operator is followed by the target type
{
...
}
4. Display Convert
public static explicit operator Cat(Dog dog)
5. Convert instance
public static implicit operator Cat(Dog dog)//Convert 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 an overloaded operator?
You cannot create a new operator, so use an existing operator for a custom class or structure (the operation meaning of predefined types and existing operators is determined and cannot be changed) , define a certain operator (this operator has a certain correlation with the operation, such as male dog + female dog = newborn dog), so as to simplify the operation of the custom type.
2. Syntax details
public static Dog operator +(Dog male, Dog female)//For example, male dog + female dog = newborn dog
{
...
}
3. Those operators can overload
unary operators: +, -, ! , ~, ++, - -, true, false (operands must be class and struct)
Binary operators: +, -, *, %, &, |! , ^, <<, >>, = =, ! =, >, <, >=, <= (at least one of the two operands represents a class or structure)
cannot be overloaded: =, &&, ||, [] (index operation), (), etc.
4. Overloaded operations What the operator can't do
Create a new operator
Change the operator syntax Redefine how the operator handles
predefined types
Change the precedence and associativity of the
operator "Age="+_age); } ... //Reload the auto-increment operation for the pet's age







public static Pet opertor ++(Pet pet)//The return value is the Pet type, and the parameter is the pet itself. All overloaded methods require public static modification
{
++pet._age;
return pet;
}
...
Dog dog=new Dog("Jack");
dog._age=2;
dog++;
dog.ShowAge();


1. A generic class is a mold, put in the type of material (field attribute method), and can shape the desired product.
2. Syntax
class Cage<T>//This is a cage class and a generic class, with a pair of <> after the class name, plus a generic parameter
{
T[] petArray;//Define an array of T type
public void PutIn(T pet){...}//Put a pet of type T
public T TakeOut(int index){...}//Placeholder T, when the cage class becomes a concrete class, T changes accordingly into a concrete number
}
//Instantiate
var dogCage=new Cage<Dog>();//Get the dog cage
var catCage=new Cage<Cat>();//Get the cat cage
3. Why do you need generics, use base A class (including the base class object of all classes) or a public interface can also implement a Cage class, but the type is too broad, and it is necessary to display the conversion type and determine what the real type is.
4. Use of generics: declare a generic class -> reference generics -> construct an instance
class Cage<t>{...}
class Cage<Dog>{...}
dogCage=new Cage<Dog>;

For example, instead of writing Petcage<Dog> dog=new Petcage<Dog> ("name"); use a var that has never been seen before. Also return default<T> does not explain what it means
to understand: Generics is to set a variable to replace the fixed type when the parameter type and return type of the class are uncertain. When creating a class instance, assign the corresponding class type value to this variable, so that you can implement a class that outputs values ​​and methods of different types.
Instead of the teacher's example, it is better to use an example of assigning an array to an array and outputting an array. For example, if you set a class, the constructor initializes an array with an array type of int, and there are methods for assigning values ​​to arrays and obtaining array values. Note here: array The type of int and the return value type of the method to get the array value are both int. If an instance of this class calls the method, the actual parameter and return value type must also be int; this limits the instance of this class; if you want a set char An instance of a type must be implemented by creating a new char class and method; this requires a lot of code work; if using generics, use variable <T> instead of fixed type int or char, so that when instantiating, By assigning T to different types (int, double, char), you can get the desired return value type, thus implementing a template, and only changing one parameter T can achieve the same function as
the example given above. Just remove all <T> and change all the following T to int or char to become a normal class. Comparing and understanding is very simple

1. What is a generic method: it is the model of the method. Given a specific type, a specific method for operating the type can be instantiated.
Note: Generic classes can have generic methods, and ordinary classes can also have generic methods.
2. Generic method syntax
class Dog{
void DogIsHappy<T>(T target){//Generic methods in ordinary classes
...
}
}
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) ;
An instance object of a class is equivalent to the value of this class type; for
example, int 4; 4 is a value of type int;
Person new person() ;new person() is a value or instance of type Person; in fact, it is equivalent to creating a Person class, then Person person=new Person(); person is a type value of Person


1. What is Constraint
Constraint is the reins of the fierce horse that controls generics! Reduce the scope of generic parameters (no matter how general, there is always a scope, the smaller the scope, the better the control)
2. The meaning
of constraints can only call the methods in the generic parameters (such as T) after adding constraints.
Constraints can be added to both generic classes and methods.
3. Constrained type
class name - the class or the class that inherits the class
class - any class
struct - any value
interface name - the interface type or any type that implements the interface
new() - with no parameters Classes
that share
a constructor 5. Constraint example void Cage<T,M> where T:Pet,IClibTree,new()//where constrain which type parameter where M:Pet,IClibTree,new() {...}






A delegate is an object that holds one or more methods! And the object is executable, passable.
Define the delegate
delegate void ActCute();
declare the 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(){//The specific content of the method};//Declare an anonymous method
2. Lambda expression
del=delegate(){} ;
del=()=>{...};

Define delegate
public delegate void Handler();
define event
public event Handler NewDog;
constructor
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");

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324885616&siteId=291194637