1. namespace
C # A program uses a namespace for organization. A namespace can be used as an internal organizational system of a program, it can also be used as an organizational system that is exposed to the outside (that is, a method that exposes its own program elements to other programs ).
To call a class or method in a namespace, you must first use the using command to introduce the namespace. The using command imports the type members in the namespace into the current compilation unit. Using namespace name.
Using system; <br/> using system. collections. generic; <br/> using system. LINQ; <br/> using system. text; <br/> using N1; <br/> namespace helloworld <br/> {<br/> class Program <br/> {<br/> static void main (string [] ARGs) <br/>{< br/> A ma = new A (); <br/> Ma. al (); <br/>}< br/> namespace N1 <br/>{< br/> Class A <br/>{< br/> Public void Al () <br/> {<br/> console. writeline ("namespace example"); <br/> console. readline (); // pause the Program <br/>}< br/>
2. Data Type
C # There are two data types: Value Type and reference type. The value type directly stores data. The reference type stores references to its data, also known as objects. Value types can be processed by objects by packing and unpacking.
Value Type: integer, floating point, Boolean, struct
Reference Type: string and object. Use new to create an object instance.
The C # type system is uniform, and the object type is the parent class of all types (pre-defined type, user-defined type, reference type, value type ).
Variable replication:
Int V1 = 0; integer
Int v2 = V1; // value assignment. The values are inconsistent.
Point P1 = new point (); Class
Point P2 = p1; // value assigned by reference, with the same value
Example:
Int intone = 300; // directly defined
Float thefloat = 1.12f;
Console. writeline ("intone = {0}", intone); // note this output method.
Console. Readline (); // The purpose is to pause the program for observation. Press enter to exit
Structure Type example:
Using system; <br/> using system. collections. generic; <br/> using system. LINQ; <br/> using system. text; <br/> namespace helloworld <br/>{< br/> struct point <br/>{< br/> Public int A; <br/> Public int B; <br/> Public point (int A, int B) // constructor <br/> {<br/> This. A = A; <br/> This. B = B; <br/>}< br/> Public void output () <br/>{< br/> console. writeline ("Area: {0}", a * B ); <br/>}< br/> class Program <br/> {<br/> static void main (string [] ARGs) <br/>{< br/> point P = new point (5, 6); <br/> P. output (); <br/>}< br/>
3. Variable Declaration
Similar to the C ++ variable declaration,
Int A = 99;
String STR = "hello ";
4. Data Type Conversion
(1) implicit type conversion:
(2) Explicit type conversion: Forced type conversion
Double X = 198802.5;
Int y = (INT) x; // method 1
Int y = convert. toint32 (x); // use the convert keyword
String STR = console. Readline ();
Int year = int32.parse (STR); // extract an integer from a string
5. packing and unpacking
Packing: converts a value type to a reference type. Changing the value after packing does not affect each other.
Int I = 2011;
Object OBJ = I; // bind a Value Type
Binning: converts a reference type to a value type. There are two phases: (1) Check the object instance to see if it is a Value Type Boxed value; (2) Assign the Instance value to a value type variable.
Int I = 2011;
Object OBJ = I; // boxed
Int J = (INT) OJB; // unpack
6. Special operators
(1) is checks whether the variable is of the specified type
Int I = 0;
Bool result = I is int;
(2) conditional operators? :
Using system; <br/> using system. collections. generic; <br/> using system. LINQ; <br/> using system. text; <br/> namespace helloworld <br/>{< br/> class Program <br/>{< br/> static void main (string [] ARGs) <br/> {<br/> console. write ("enter a year:"); <br/> string STR = console. readline (); <br/> int year = int32.parse (STR); // extract an integer from a string <br/> bool isleapyear = (Year % 400 = 0) | (Year % 4 = 0) & (Year % 100! = 0); <br/> string yesno = isleapyear? "Yes": "not"; <br/> console. writeline ("{0} year {1} leap year", year, yesno); <br/> console. readline (); <br/>}< br/>
(3) New: Create a new data type instance
String [] STR = new string [5];
STR [0] = "I ";
STR [1] = "am ";
(4) typeof obtain the type of the system prototype object
Type mytype = typeof (INT );
Console. writeline ("type: {0}", mytype );
Output: Type: system. int32
Example of determining prime numbers: focusing on data type conversion and Input/Output Processing
Using system; <br/> using system. collections. generic; <br/> using system. LINQ; <br/> using system. text; <br/> namespace helloworld <br/>{< br/> class Program <br/>{< br/> static void main (string [] ARGs) <br/> {<br/> console. write ("enter an integer:"); <br/> string STR = console. readline (); <br/> int num = int32.parse (STR); // extract an integer from a string <br/> int sqtnum = (INT) math. ceiling (math. SQRT (convert. todouble (Num); // Parameter Number type conversion <br/> bool flag = true; <br/> for (Int J = 2; j <sqtnum; j ++) {<br/> If (Num % J = 0) {<br/> flag = false; <br/> break; <br/>}< br/> string yesno = flag? "Yes": "not"; <br/> console. writeline ("{0} year {1} prime number", num, yesno); <br/> console. readline (); <br/>}< br/>
Fibonacci functions of Recursive Algorithms
Using system; <br/> using system. collections. generic; <br/> using system. LINQ; <br/> using system. text; <br/> namespace helloworld <br/>{< br/> class Program <br/>{< br/> static void main (string [] ARGs) <br/>{< br/> int n = DG (30); <br/> console. writeline ("the value of the 30th Fibonacci functions is: {0}", n); <br/> console. readline (); <br/>}< br/> # region recursive algorithm <br/> // <summary> <br/> // recursive algorithm <br/> // </Summary> <br/> /// <Param name = "I"> Number of digits </param> <br/> /// <returns> result </returns> <br /> static int DG (int I) <br/>{< br/> if (I <= 0) <br/>{< br/> return 0; <br/>}< br/> else if (I = 1 | I = 2) <br/> return 1; <br/> else <br/> return DG (I-1) + DG (I-2 ); <br/>}< br/> # endregion <br/>}< br/>
7. Object-Oriented Programming
Object-oriented programming, OOP
The schema struct statement does not need the New Keyword, Value Type
C # only supports single inheritance. If you want to inherit more, use the interface
7.1 interface:
Interface statement
1. An interface defines a contract.
2. interfaces can accommodate methods and C #
Attributes, events, and indexers.
3. In an interface declaration, we can declare zero or multiple members.
4. The default access type of all interface members is public.
.
5. If any modifier is included in the interface member declaration, a compiler error is generated.
6. Similar to a non-abstract class, a class must provide the implementation of all the Members in the interface as long as these Members appear in the base class of the class.
7.
When the base type list contains the base class and interface, the base class must be the first in the list.
8.
The class that implements the interface can explicitly implement the members of this interface.Explicitly implemented members cannot be accessed through class instances, but can only be accessed through interface instances.
.
Example:
Interface isampleinterface <br/>{< br/> void samplemethod (); <br/>}< br/> class implementationclass: isampleinterface <br/>{< br/> // explicit interface member implementation: <br/> void isampleinterface. samplemethod () <br/>{< br/> // method implementation. <br/>}< br/> static void main () <br/> {<br/> // declare an interface instance. <br/> isampleinterface OBJ = new implementationclass (); <br/> // call the member. <br/> obj. samplemethod (); <br/>}< br/>}
Using system; <br/> using system. collections. generic; <br/> using system. LINQ; <br/> using system. text; <br/> namespace consoleapplication1 <br/>{< br/> class program: myinterface <br/>{< br/> string id = ""; <br/> string name = ""; <br/> Public String ID // method for implementing the attributes in the interface <br/>{< br/> Get <br/>{< br/> return ID; <br/>}< br/> set <br/>{< br/> id = value; <br/>}< br/> Public string name <br/>{< br/> Get <br/>{< br/> return name; <br/>}< br/> set <br/> {<br/> name = value; <br/>}< br/> Public void myinterface. showinfo () // method in the implementation interface. The interface member implementation is displayed here <br/>{< br/> console. writeline ("No./t name"); <br/> console. writeline (ID + "/t" + name); <br/>}< br/> static void main (string [] ARGs) <br/>{< br/> program P = new program (); // instantiate a Class Object <br/> myinterface = P; // use the Object Instantiation interface of the derived class <br/> myinterface. id = "tomorrow"; // call an instance using the interface <br/> myinterface. name = "C # Learning"; <br/> myinterface. showinfo (); <br/> console. readline (); <br/>}< br/> interface myinterface <br/>{< br/> string ID {Get; set ;} // The interface member must be a public <br/> string name {Get; Set ;}< br/> void showinfo (); <br/>}< br/>
7.2 abstract class
Abstract: The class is an abstract class, which only expresses an abstract concept. It can only be used as a base class. Abstract classes and non-abstract classes are different in the following aspects:
- Abstract classes cannot be directly instantiated, and the use of the new operator for abstract classes is a compilation error. Although some types of variables and values during compilation can be abstract, such variables and values must be null, or a reference to an instance that contains a non-abstract class (this non-abstract class is derived from the abstract class ).
- Allowed (but not required) abstract classes include abstract members.
- Abstract classes cannot be sealed.
When a non-abstract class is derived from an abstract class, these non-abstract classes must specifically implement all the abstract members inherited to override those abstract members.
Abstract method:
- Abstract methods must be declared in abstract classes.
- Virtual, static, and private keywords cannot be used to declare an abstract method.
When a non-abstract class is derived from an abstract class, the abstract method must be rewritten in a non-abstract class to provide specific implementation. Override keywords are used to override abstract methods.
Abstract classes and interfaces
Similarities:
(1) can be inherited
(2) cannot be instantiated.
(3) All methods can contain method declarations.
(4) The derived class must implement the unimplemented method.
Partition:
(1) abstract base classes can define fields, attributes, and methods. The interface can only define attributes, indexers, events, and method declarations, and cannot contain fields.
.
(2) abstract classes are incomplete classes and need to be further refined. interfaces are a behavior specification. Microsoft's custom interface always carries the able field to prove that it is a class of expression "I can do it ..."
(3) The interface can be implemented in multiple ways, and the abstract class can only be inherited by a single
(4) abstract classes are more defined in a series of closely related classes, while interfaces are mostly classes with loose relationships but all implement certain functions.
(5) abstract classes are abstract concepts from a series of related objects, so they reflect the internal commonalities of things. interfaces are a functional Convention defined to satisfy external calls, therefore, it reflects the external characteristics of things.
(6) The interface basically does not have any specific characteristics of inheritance. It only promises the methods that can be called.
(7) interfaces can be used to support callback, but inheritance does not have this feature
(8) The specific methods implemented by abstract classes are virtual by default, but the interface methods in the class implementing interfaces are non-virtual by default. Of course, you can also declare them as virtual
(9) If an abstract class implements an interface, you can map the methods in the interface to the abstract class, instead of implementing the methods in the abstract class.
Rules:
1. abstract classes are mainly used for closely related objects, and interfaces are most suitable for providing general functions for irrelevant classes.
2. If you want to design a large functional unit, use an abstract class. If you want to design a small and concise functional block, use an interface.
3. If you want to create multiple versions of a component, create an abstract class. The interface cannot be changed once it is created. If you need a new version of the interface, you must create a new interface.
4. If you create a function that will be used across a wide range of different objects, use the interface. If you want to provide general implemented functions among all the implementations of the component, use the abstract class.
5. Analyze objects and extract internal commonalities to form abstract classes to express the object nature, that is, "What ". Interfaces are preferred when external calls or functions need to be expanded
6. A good interface definition should be specific and functional, rather than multi-functional, otherwise it may cause interface pollution.
If a class only implements a function of this interface and has to implement other methods in the interface, it is called interface pollution.
7. Avoid using inheritance to implement the build function, but use black box multiplexing, that is, object combination. Because the hierarchy of inheritance increases, the most direct consequence is that when you call a class in this group, you must
Load all to the stack! The consequences can be imagined. (Based on the stack principle ). At the same time, some interested friends can note that Microsoft often uses the object combination method when building a class. For example
In Asp.net, the page class has the server
Request and other attributes, but they are actually the objects of a class. This object of the page class is used to call the methods and attributes of other classes. This is a very basic design principle.
Refer:
Http://www.cnblogs.com/lovemyth/archive/2008/09/08/828909.html
Http://blog.csdn.net/fxh_hua/archive/2009/08/20/4464739.aspx
7.3 Seal
Keyword sealed
The sealed class is used to restrict scalability. If a class is sealed, the class cannot be inherited. If a member is sealed, the derived class cannot override the implementation of the member.
Get and set accessors
C # The split fields and attributes are both member variables. The fields are private and used inside the program, the attribute is the permission to access fields externally (other classes other than this class). You may ask why the field is not directly written as public, if you do this, you will lose the controllability. For example, if you want to allow a field to be read but not modified during external access, you can only write get. For example, if no value can be assigned during the assignment, you can control the value in the set if you want to restrict it. Otherwise, the value assigned by others to this segment is. If the program you are writing is cracked, you will be changed at will. No security, not controllable at all. To put it bluntly, attributes are used to control external access.
Fields and attributes
The field is private and used internally. The attribute is public or protected.
Generally, fields should be used only for private or protected variables.
A field is a data type variable. Its attribute is read-only, write-only, read-write, and can be customized to assign values to the field (get, set ).
Code demo
Int someintfield = 1; Field
Int someintproperty, set the field access property
{
Set {someintfield = value ;}
Get {return somrintfield ;}
}
Overwrite and overwrite
The subclass processes the Virtual Methods in the parent class with override and overwrite (new)
Override means that a method with the same name as the parent class is written in the subclass. Override means that the subclass inherits the virtual method in the parent class.
Override is to override a virtual method, which will reflect the "polymorphism" in the object-oriented model; new is the real coverage, without "polymorphism ".
Rewriting is a way to achieve polymorphism. For example, B inherits a, and a contains a virtual method named AA, and B overwrites it. If you declare a A = new B (), then you call. AA () actually calls the AA method in Class B.