C # initial knowledge

Source: Internet
Author: User

Because we need to look at monocross and use it to implement cross-platform mobile development. Monocross uses C #. net, so I read C # And C # are quite similar to C ++ and Java, but they also have some unique things. A brief summary is as follows:

1. C # is an object-oriented cross-platform language based on C ++ and Java. However, unlike Java, C # does not depend on JVM. net Framework, which is the essential difference between C # and Java.

Bytes are formed after Java compilation.CodeThese codes must be run on the JVM provided by JRE (Java Runtime Environment); C #ProgramIt is also compiled into il (intermediate language) and runs in the common language runtime of. NET Framework ).

2. C # discards pointers in C ++ and prohibits direct memory operations.

3. All the code of C # belongs to the class and object code. The concept of global variables and global functions does not exist. In terms of inheritance, C # only supports single inheritance.

4. Powerful web application support; high XML support;

5. Two-dimensional array:

E.g. Define a two-dimensional integer array:

Int[,] A;=New Int[2,3]; OrInt[,] A =New Int[2,3] {1,2,3},{4,5,6}};

6. enumeration:

Int[] A = {1,2,3,4};Foreach(IntIInA) {console. writeline (I );}

7. ClassThe concepts of inheritance, overload, and polymorphism are similar to those of C ++.

Access the members of the base class in the derived class and use base. xxx.

The constructor of the derived class D calls the overloaded constructor in the base class C: Public D (int x, int y, int Z ):Base (x, y){This. z = z ;}

8. interface:

InterfaceI {VoidF (IntX );}ClassA: I {Public VoidF (IntX ){}}

Interface declaration:

InterfaceI1 {VoidF (IntX );//MethodIntATT {Get;Set;}//Attribute, read/writeEventEventhandler ondraw;//EventString This[IntIndex] {Get;Set;}//Indexer}

 

9. MethodDelegate: This is a unique feature of C.

 

 Using  System;  Namespace  Simpledelegatepro {  Delegate   Void Mydelegate ( String  S );  Class  A {  Public   Void F (sting MSG) {console. writeline (MSG );}  Public   Static   Void  G (sting MSG) {console. writeline (MSG );}}  Class  B {  Public   Void  H (mydelegate m) {M (  "  Methods are passed through delegation.  " + M. method. Name + " This is the result of calling this method.  "  );}}  Class  Program {mydelegate GD = New  Mydelegate (A. G );  //  After that, "GD" is the same as "a. g", with the same function name  A = New  A (); mydelegate FD = New  Mydelegate (A. F ); //  After that, "FD" is the same as "a. g", with the same function name  Gd (  "  Here is the result of the delegate GD output of the static method A. G ().  "  );  //  Equivalent to a. G ("here is the result of the delegate GD output of the static method A. G ");  Console. writeline (  ""  ); FD (  "  Here is the result of the delegate FD output of method F () of object.  " );  //  Equivalent to A. F ("here is the result of the delegate FD output of the method F () of object ");  Console. writeline (  ""  ); B = New  B (); B. H (FD );  //  Upload method A. F to Method B. H through delegation  Console. readkey ();}}} 

The final output result is:

 
Here is the result of the delegate GD output of the static method A. G ().
 
Here is the result of the delegate FD output of method F () of object.
Method F is passed through delegation.This is the result of calling this method.

 

Delegate statement:

Attribute modifier delegate return type delegate type name (parameter list)

Attribute and modifier are optional. optional modifiers include new public internal protected private

The attribute list and return type jointly determine a group of methods that can be connected by the delegate.

Delegation instantiation:

Delegate object = new delegate type (Association method );

Reference of delegation:

After a delegate is created, you can call its associated methods by referencing this object. In short, the delegate object name is used as the method name.

Delegate combination:

As a combination, delegation is much more powerful than function pointers in C/C ++. For example, delegation can be combined.

 

 
Delegate StringMydelegate (IntN); mydelegate a, B, c, d;//After assigning values to A, B, C, D, you can perform the following operations:D = A + B + C;//Delegated combinationD = D-;

 

In this case, call D to call all the delegates it contains and pass the parameter values of d to these delegates. If the associated method has a return value, the combined delegate D returns the return value of the last delegate in the delegate list.

10. namespace

Namespace is an important concept in C #. It provides an organizational structure for the program logic structure.

Namespace declaration:

 
NamespaceNamespace name {namespace member ;}

To access members of another namespace in a namespace, use the namespace Name:Namespace name. namespace Member

Namespace import:

Using namespace name; // equivalent to Java Import

11. Is &

The is Operator in C # is the same as the instanceof operator in Java. Both of them can be used to test whether the instance of an object belongs to a specific type.

 

In Java, there is no operator equivalent to the as operator in C. The as operator is very similar to the is operator, but it is more aggressive: If the type is correct, the as operator will try to convert the referenced object to the target type; otherwise, it sets the variable reference to null.

 

For example, two classes: A and B
B = new B ();
// Perform forced conversion without the use of AS. If the type does not match, a conversion exception is thrown.
A A = (a) B;
// If the as is used, the type does not match and no exception is thrown. A null value is returned. A = NULL
A A = B as;

12. Attribute

Http://www.cnblogs.com/-dawn/archive/2012/09/29/2708053.html

13. Using

Using has three usage methods in C:

A. Reference a namespace

B. Create an alias:

E.g .:

 
UsingMSWord = Microsoft. Office. InterOP. word;

 

Benefits: Avoid being tedious. Solve the problem that different namespaces are referenced in the same. CS file but the class names are the same.

C. Force Resource Management

Purpose: Clear unmanaged resources that are not controlled by GC. After the using ends, the disposable method is called implicitly. It is equivalent to calling a local subroutine.

Check: http://developer.51cto.com/art/201105/263550.htm

14,List. addrange Method

Add the elements of the specified setList.

Namespace:System. Collections. Generic
Assembly:Mscorlib (in mscorlib. dll)

Check: http://msdn.microsoft.com/zh-cn/library/z883w3dc (V = vs.80). aspx? CS-save-lang = 1 & CS-lang = CSHARP # code-snippet-4

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.