C # knowledge points (1)

xiaoxiao2021-03-06  46

C # knowledge points (1) 1. In C and C use 0 to indicate that the fake other non-0 is indicated by this unqualified expression in C # has been discarded in the C # True value cannot be used by any anything Non-zero value instead of any other integer type and Boolean type no longer exists to convert an integer type into a block. BOOL X = 1 // Error does not exist such a way of writing can only be written into x = true or x = false2. The constant modifier can be .. new .. public .. protected .. interface .. private3. Implicit enumeration conversion allowed Converting the decimal integer 0 into any enumeration type to correspond to other integers, there is no such implicit conversion. Implicit reference conversion includes the following categories .. From any reference type to the object type conversion .. Switch from class type s to class type T is the derived class of T is T is the conversion of Type S to Interface Type T. The class S implements the interface T.. The conversion from the interface type S to the interface type T is the array type T-type type T-type TT of the parent interface from the element type TS to the element type TT to the element type TT. This conversion requires satisfaction The following conditions. S and T Only the data type of the element is different, but their dimension is the same .. TS and TT are reference types .. There is an implicit reference conversion from TS to TT .. From any array type to System.Array Conversion .. From any representative type to System.Delegate conversion .. From any array type or representative type to System.icloneable conversion .. Switch from empty type null to any reference type 4. Explicit reference conversion includes:. From the object to any reference type .. From the class type S to class type T is the base class of T is T. From class type S to the interface type T which is not sealing and does not implement T .. from the interface type S to Class Type T which T is not a seal and does not implement S .. From Interface Type S to Interface Type T, the sub interface of the T is not ts from an array type S to an array type TT of the element type TT to convert this The conversion needs to meet the following conditions: .. S and T Only the data type of the element is different, the dimension is the same .. TS and TT are reference types .. There is an explicit reference conversion from TS to TT ... From System.Array Go to the array type .. From System.Delegate to the representative type .. From System.icloneable to an array type or representative type explicit reference conversion occurs between the reference type needs to be detected at runtime to ensure correct, in order to ensure explicit reference conversion The value of the normal execution requirement source variable must be NULL or the type of object it is referenced can be implicitly referenced to the conversion to the target type. Otherwise, an explicit reference conversion failed will throw an InvalidCastException. Regardless of implicit or explicit reference conversions, although the type of reference may be changed, it will not change the value itself.

5. In C #: .. Positive zero and negative zero are considered equal .. Negative infinite is considered to be less than any other value just equal to another negative .. is endless than any other value just equal to another The enumerated type comparison operator simply compares the base type value of the two operands. 6. Establishing one of the following conditions is considered equal to two string values ​​.. Two string values ​​are null .. Two strings are the same non-empty string of the character sequence as the string length. Note that the string equivalent operator is more than two strings of the value rather than a string reference. When two separate strings instances contain the same string sequence, it is considered that the value of the two strings is equivalent to the equivalent operation. The comparative can be used to compare the two string references not compare the values ​​of the two strings. 7.New operator is used to create a new type of instance it has three forms: .. Object Creating an expression for creating an instance of a class type or value type .. Array Creating an expression is used to create an array type instance .. Representative Creating an expression is used to create a new representative type instance new operator suggests that a class instance is created but does not necessarily imply that dynamic memory allocation is different from C 's operations in C . 8.C and C language allow the Case tag in the Switch statement without the Break statement, but C # does not allow this, it requires the use of Break statements or jump statements after each tag item, that is, it is not allowed to automatically traverse from one case to others. CASE will make an error when compiling.

8.using system; class test {int x; static int y; void f () {x = 1; // correct, equivalent to this.x = 1Y = 1; // correct, equivalent TEST.Y = 1} static void g () {x = 1; // error cannot access this.xy = 1; // correct, equivalent to Test.y = 1} static void main () {Test T = New Test (); TX = 1; // Correct TY = 1; // Error cannot access static member test.x = 1 in the instance of the class; // Error cannot be accessed by the class Test.y = 1; // correct}} Non-static members of the class belong to the instance of the class All the instances of each class have opened up a zone for non-static members in memory; and the static member of the class belongs to all instances of the class to all instances of this class. Create. How many copies have only a zone in memory in memory. 9 ... The constructor of a class is usually the same as the class name. The constructor does not declare the return type .. General constructor always Public type If it is a private type indicating that the class cannot be instantified This is usually available The class of static members .. Do not do things other than the instance of the class in the constructor do not try to explicitly adjust the parameters of the method of 10.c # China method: .. Value parameters do not include any modifications The reference type parameter is declared in a REF modifier .. The output parameter is declared in an OUT modifier .. An array parameter declares 11 in params modifier 11. If the array parameter contains array parameters if it must be located in the parameter table Finally, additional parameters can only be allowed to be a one-dimensional array such as String [] and String [] [] types can be used as an array type parameter, and String [,] cannot be the final array parameter can no longer have REF and OUT modifiers.

12. In C #, if you use the Virtual this keyword when you declare a method, then in the derived class, you can use the Override or New keyword to discard it or ignore it. If you are on the father The type of virtual is used in the class, and in its derived class, there is no Override or New keyword, and the compiler will report in an error in the New way, ignore the NEW mode, ignore the derived class. The method is to run. The following example can help you understand: 000: // versioning / versioning.cs001: public class mybase 002: {003: public virtual string meth1 () 004: {005: return "mybase-meth1" 006:} 007: Public virtual string meth2 () 008: {009: Return "MyBase-Meth2"; 010:} 011: Public Virtual String Meth3 () 012: {013: Return "MyBase-Meth3"; 014:} 015:} 016: 017: Class myderived: mybase 018: {019: public override string meth1 () 020: {021: return "myderived-method1"; 022:} 023: public new string meth2 () 024: {025: Return "MyderiveD-Meth2"; 026:} 027: Public String Meth3 () // The system will have a warning here, and will hide the method Meth3 () 028: 029: 030: {031: return "myderived-method3 "032:} 033: 034: public static void main () 035: {036: myderived md = new myderived (); 037: mybase mb = (mybase) md; 038: 039: system.console.writeline (mb.meth1 ()); 040: system.console.writeline (mb.meth2 ( )); 041: system.console.writeline (Mb.meth3 ()); 042:} 043:} Out: Myderived-method1mybase-method2mybase-meth3 can be clearly seen, the output of the next two new keywords is the father The output of the method in the class, so it can be seen that the role of this keyword is if there is this method in the previous version, and the previous method is used instead of me now method. And the role of Virtual method is just the opposite It is the role if there is such a method in the parent class, use the method of the method I write, let the previous glow! However, this is not very good, let people misunderstood (bad, caught To hit me! & *% $ # @). If you remove the 037th line, change all the MB in 039-041 to MD, the output is changed to: myderived-method1myderived-method2myderived-method3 This is explained? Obvious, the object of the derived class will only take effect when it is reshaped by the parent class. Oh, this is really difficult to understand, everyone can only make it clear that this is the institution, the so-called "Practice is the only standard for testing C #", haha!

10. The structure in C # is very similar to the class, but it is a value type, not the reference type, its structural member is private, the structure does not support inheritance, and cannot be derived from any class, the only exception is The structure is derived from the class system.Object, so the structure can also access the system.object method, and can even rewrite the method in System.object - such as overwriting the toString () method, actually inherited is indirect: Structure is derived from SYSTEM. ValueType, System.ValueType is derived from System.Object, and ValueType does not add its own new method, but provides some Object's more suitable value type over-write method; transmitting the structure as a REF parameter to the method, avoid the copy data Come performance loss. For the structural definition constructor, the parameters must be specified, and the instance field in the structure is incorrect, there is a constructor. 11.C # There are 15 predefined types, including 13 value types and 2 reference types (String and Object) 12.c # think that Byte types and char types are completely different (when emphasizing types), they Programming must be clearly written. Byte's type is unsigned by default, and its symbolic version has a special name Sbyte. 13. String objects are kept on the pile instead of the stack; thus assigning a string variable to another string, two references to the same string in the memory will be obtained. Then, modify one of the strings, Note that this creates a full new String object and the other string has not changed. 14. Static read-only fields cannot be set in the instance constructor, or instantiate read-only fields in the static constructor; the only exception rules are: If the variable is declared as a field of a class or structure, the variable is specified. A value, you cannot reset the value of the read-only field, and you can instantiate read-only fields in the constructor. The read-only variable cannot be passed to the method as a reference or output parameter, except in the constructor. 15. The constant is always static, but it is not allowed to include static when declaring; the read-only field can be used to initialize constants. 16. You can only use the SIZEOF operator in insecure code. 17. In System.Object, toString only displays the class name and rewrites it. 18. The signature of the overload method must be different - the name, number of parameters must be different, and only the return type is not enough. 19. The static constructor does not define the access modifier, and other C # code never call it, but when loading the class, it is always called when it is running, so it is meaningless to make a modifier like public and private. The same static constructor cannot bring any parameters, and a class can only have a static constructor. The 20.const value is implicitly static, which must use a static member's grammar, but if it is displayed, adding Static equivalent errors. 21. The assignment of the Readonly field can only be in the constructor. If you want to set the ReadOnly field to static, you must explicitly declare that you don't have to assign a value to the ReadOnly field in the constructor. If it is not assigned, its value is its data type. Defaults.

转载请注明原文地址:https://www.9cbs.com/read-53826.html

New Post(0)