1. C # Program Structure
C # is organized using namespaces, using a type member in the namespace identified by the namespace name into the current compilation unit, allowing you to use the identifier of each imported type directly.
The Main method is the entry point for the program, and all of the main methods in C # must be static.
Static void Main () {}
2. Variables and types
Boxing : Converting a value type to a reference type; unpacking : Converting a reference type to a value type.
When defining a local variable, be sure to initialize it, and define as few global variables as possible.
the value of a Boolean type variable can only be true or falseand cannot be converted with another type.
Char represents a Unicode character in C #.
String is an alias of string, a string that represents a Unicode character, and when you build and build a long string, you must use the StringBuilder type instead of the string type. The string object is immutable, and each time a method in the string class is used, a new string object is created in memory, which is required to allocate a new space for the object. To modify a string without creating a new object, you can use the StringBuilder class.
Collection type: Array, ArrayList, Hashtable
3. Properties and Methods
There are two types of C # Properties: Properties (Attributes), custom properties (get and set accessors) defined in the common language runtime, as follows:
namespacecsharptest{[Defaultpropertyattribute ("StationName")] classStation {Private string_stationname; Private Double_lon =103; Private Double_lat = -; PrivateColor _color; Private string_file =string. Empty; PrivateFont _font; [CategoryAttribute ("General"), DescriptionAttribute ("file name"), ReadOnlyAttribute (true)] Public stringFileName {Get{return_file;} Set{_file =value;} } [CategoryAttribute ("Show"), DescriptionAttribute ("Color"), Displaynameattribute ("Color")] PublicColor Color {Get{return_color;} Set{_color =value;} } [CategoryAttribute ("Show"), DescriptionAttribute ("Font")] PublicFont Font {Get{return_font;} Set{_font =value;} } Public stringStationName {Get{return_stationname;} Set{_stationname =value;} } Public DoubleLon {Get{return_lon;} Set{_lon =value;} } Public DoubleLat {Get{return_lat;} Set{_lat =value;} } }}View Code
A get accessor is similar to a method body to return a value of a property type, and a set accessor is similar to a method that returns a type void, which uses an implicit argument called value.
The instantiated object of a class cannot call a static method and must be called directly using the class name.
4. Structure, class and interface
Structs can be instantiated without using the new operator; structs can declare constructors, but they must take arguments; in struct declarations, they cannot be initialized unless the field is declared const or static.
Methods or fields in a class cannot be called with the class name until the class is instantiated.
Common class Modifiers: new, private, protected, public, internal (only the class in which it resides), abstract (abstract class), sealed (sealed class, not allowed to inherit).
C # class Attributes: Encapsulation, inheritance (single inheritance only), polymorphism.
Interfaces are declared with interface and implemented by class inheritance. Multiple inheritance can be achieved through an interface, although a class can inherit only one base class, but inherit any interface.
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;namespacetest1{//Declaration of three interfaces InterfaceIpeople {stringName {Get; Set; } stringSex {Get; Set; } } InterfaceIteacher:ipeople {voidteach (); } InterfaceIstudent:ipeople {voidstudy (); } //class inherits from multiple interfaces and implements the interface classprogram:ipeople,iteacher,istudent {stringName =""; stringSex =""; Public stringName {Get { returnname; } Set{Name=value; } } Public stringSex {Get { returnsex; } Set{Sex=value; } } Public voidteach () {Console.WriteLine (Name+" "+ Sex +"Teachers"); } Public voidStudy () {Console.WriteLine (Name+" "+ Sex +"Student"); } Static voidMain (string[] args) { Program Program=NewProgram ();//instantiating an objectIteacher Iteacher =Program ; Iteacher. Name="Tea"; Iteacher. Sex="male"; Iteacher.teach (); Istudent istudent=Program ; Istudent. Name="Stu"; Istudent. Sex="female"; Istudent.study (); Console.readkey (); } }}View Code
Abstract classes and Interfaces:
- Abstract classes and interfaces cannot be instantiated directly, but their variables can be declared;
- The implementation of a member can be defined in an abstract class, but the interface may not;
- An abstract class can contain fields, constructs and destructors, static members, or constants, but not interfaces, and interfaces can contain events, indexers, methods, and properties;
- The members of the interface must be public;
When declaring a sealed method, the sealed modifier is always used in conjunction with the override modifier.
The partial class is defined using partial, and the declaration of the partial class must be in the same namespace as the other parts.
5. Exception Handling
try...catch...finally statements
6, Iterators and generics
An iterator (iterator), sometimes called a cursor, is a piece of code that returns an ordered sequence of values of the same type, which is a software design pattern for programming, which can traverse interfaces on a container. Designers do not care about the contents of the container.
The C # iterator code returns each element in turn using the yield return statement, and the yield break statement terminates the iteration, and the return type must be either IEnumerable or IEnumerator. The most common way to create iterators is to implement the GetEnumerator method on the IEnumerator interface.
Generics are a programming method for processing algorithms and data structures, mainly to improve the reusability of code. The use of generics is as follows:
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;namespacefinder{ Public classFinder { Public Static intFind<t>(t[] items, T item) { for(inti =0; I < items. Length; i++) { if(Items[i]. Equals (item)) {returni; } } return-1; } } classProgram {Static voidMain (string[] args) { inti = finder.find<int> (New int[] {1,2,3,4,5,6,7,8,9},6); Console.WriteLine ("6 position in the array:"+i.tostring ()); Console.readkey (); } }}View Code
7. IO operation
files, folders, XML, INI configuration files, registry, database
8. Network Programming
Sockets, Mail, Web
9. Threads
10. Form
Form form, MDI form, various controls (ListBox, TreeView, DockPanel, Crystal Report, print), GDI + graphic image technology
C # Learning Notes