Three features of OOP object-oriented: encapsulation (encapsulation), Inheritance (inheritance), polymorphism (encapsulation)
Like Java and C + +, C # is also the OOP language. Also, C # encapsulates data better than C + +. In addition, in C + + it may be customary to start a program from the main function, and the class exists as part of the main function. In C #, all of this is done in a class.
This is also where C # is different than other languages.
About Classes Class
1. Method parameter arguments and pass parameter parameters
First of all, say the difference between the two words. In fact, they all refer to parameters, and most of the time they are common to each other. Specifically, parameters refers to a parameter in the time when the method is declared, while arguments refers to the parameter that is passed to the method when the method is called.
First, about optional (default) variables:
1 class Program2 {3 Static voidMain (string[] args)4 {5Program obj =NewProgram ();6 intNUM1 = -, num2 = $, num3 = -;7 obj. Printmethod (NUM1, num2, num3);8 obj. Printmethod (NUM1);9 obj. Printmethod (NUM1, arg3:num3);Ten Console.readkey (); One } A voidPrintmethod (intArg1,intArg2= -,intarg3= -) - { -Console.WriteLine (Arg1 +" "+ Arg2 +" "+arg3); the } -}
There are three parameters in the Printmethod method parameter list, and the latter two are optional variables. That is, they can be filled in or not when the method is called. If you do not fill in, the default value is used.
Optional variables are not allowed in c#3.0, and can be used by 4.0. Note that if you want to skip an optional variable for subsequent assignments at the time of the call, you need to use the parameter name, as in the 9th row above, to assign a value in the form of a specific merit.
The result of the above operation
Second, passing by reference or by value
Believe that these two have been well-know to the heart. Not much to explain. C # is the same as no description, by value, by adding a ref description in the parameter list is by reference.
In addition to these two, C # has one more out parameter. Because C # needs to be explicitly assigned, a variable must be assigned before it is used. Therefore, an error occurs if a variable that is declared only and not assigned is passed to the method by reference ref. This out is used here, you can pass an unassigned variable to the method.
1 class Program2 {3 Static voidMain (string[] args)4 {5Program obj =NewProgram ();6 intNUM1 = -, num2;7 obj. Valuemethod (NUM1);8Console.WriteLine ("After by Value:"+num1);9NUM1 = -;TenObj. Referencemethod (refnum1); OneConsole.WriteLine ("After by Reference:"+num1); AObj. Outmethod ( outnum2); -Console.WriteLine ("After by-out :"+num2); - Console.readkey (); the } - voidValuemethod (intarg1) - { -Arg1 = -; + } - voidReferencemethod (ref intarg1) + { AArg1 = -; at } - voidOutmethod ( out intarg1) - { -Arg1 = -; - } -}
These apply to data types other than reference types. There are five types of reference: class, interface, delegate, object, string
This is not to say that reference types are passed by reference, specifically when the reference type is a parameter:
1. When modifying the variable itself, the result is similar to a value pass, that is, the value of the variable before the pass is not changed
2. When modifying a variable's property or field, it is the reference pass that affects the value of the variable before it is passed
3, the parameter uses ref, is the true reference pass, regardless of the variable itself or modify the variable's property or field, will affect the value of the variable before passing
2.this pointer
To clarify some of the confusion of names, we often use the this pointer inside a class, which points to the current object. The difference is that C + + inside the this pointer to get the contents of object through, and C # can be directly through. (dot) to use. (Do not deliberately differentiate pointers and specific objects)
3.static
A static method or member variable must only be accessed directly through a class in C #, not through an instance object of that class. This point should be noted
This is not available in the static method, and static methods can only access static member variables.
4. Destroying objects
C # is implicitly garbage-collected like Java. If you need to display the free space, it is done by means of a destructor.
However, C # shows that calling a destructor method is not legal. You need to add the Dispose method specifically by implementing the IDisposable interface.
Here you can use the code
1 classsomeclass:idisposable2 {3 BOOLis_disposed =false;4 protected Virtual voidDispose (BOOLdisposing)5 {6 if(!is_disposed)7 {8 if(disposing)9 {TenConsole.WriteLine ("Not in destructor"); One } AConsole.WriteLine ("Disposing ..."); - } - This. is_disposed =true; the } - Public voidDispose () - { -Dispose (true); +Gc. SuppressFinalize ( This); - } +~SomeClass () A { atDispose (false); -Console.WriteLine ("In destructor"); - } -}
5. Data encapsulation via attributes
When designing a class, we always want to hide the internal state of the class between the members of the class and access it indirectly through the method. When you are a customer, you always want to access these properties directly. The way to resolve this conflict is through attribute implementation.
1 Public intSalary2 {3 Get4 {5 returnsalary;6 }7 Set8 {9 This. Salary =value;Ten } One}
Get and set can be used to set or access specific content in a manner similar to a property
6.readonly Field
That's constant.
Values declared as ReadOnly can only be assigned at the time of declaration. If it is a static readonly value, you can also assign a value in static from constructor
C # Learning Record 3--class encapsulation, inheritance, polymorphism