C # beginner's note

Source: Internet
Author: User

C # is required for graduation project #.
After learning c ++ and java, I first read c # Getting Started classic and c # advanced programming. c # and java are compared with c ++ in terms of syntax and take notes.

Pre-Compilation:
Like C ++, c # has pre-compiled commands.

Namespace and using:
I still don't know much about it. It is somewhat the same as the import in java.

Basic and data types:
As in c ++, there are unsigned types, such as uint and ushort.

Goto:
The obsolete goto keyword in java is returned.

Const:
Define a constant using the const keyword;

@:
@ The character string after the symbol is a literal meaning (no transfer character), for example, string s = @ "\ n", which means "\ n" instead of a line break

Foreach:
For example, int [] intArray = new int [3]; foreach (int I in intArray ){...}

Switch:
It can be used for string; default does not have to be written in the last sentence, but is executed only when no case match exists. Each case must have break, return, or goto case... end, except when two or more cases are written together consecutively

Enum:
The default underlying type is int, which is assigned a value from 0. It can be operated like an int or converted to an int. The underlying type can also be another basic data type, or you can assign values by yourself.
01
Enum Orientation: byte
02
{
03
East,
04
West = 3,
05
North,
06
South = north
07
}
08
...
09
Orientation o = Orientation. south;
10
Int I = (byte) o;
11
Orientation east = (Orientation) 0;
12
Orientation direction = (Orientation) Enum. Parse (typeof (Orientation), "north"); // String Conversion to Enumeration
Typeof:
Typeof (Object) is like Object. class in java; object. GetType () is like object. getClass () in java ();

Struct:
The structure is derived from System. ValueType, which is also derived from System. Object.
The struct is similar to the class. It can also have attributes and methods or implement interfaces. However, struct is a value type, while class is a reference type.
1
MyStruct struct1 = new MyStruct ();
2
MyStruct struct2 = struct1;
3
// Struct1 copies a copy to struct2. The setting of struct2 does not affect struct1.
4
MyClass class1 = new MyClass ();
5
MyClass class2 = class1;
6
// Changes to class2 will affect class1 because they direct to the same instance.
Ref, out:
Ref allows the parameter to be passed by reference

1
Void Triple (ref int)
2
{A * = 3 ;}
3
...
4
Int a = 3;
5
Triple (ref );
6
Console. WriteLine (a); // a is changed to 9
The out and ref functions and usage are the same. The difference is that the parameters passed into the function using ref must be initialized first, and out does not need to be used.

Params:
The params keyword allows the function to accept any number of parameters.
Int fun (params int [] a) is like int fun (int... a) in java)

Main function:
There are four types of static int Main (string [] args), static void Main (string [] args), static int Main (), static void Main ()


Multi-dimensional array, variable-length array:

1
Int [] [] array = new int [3] []; // Variable Length array
2
Array [0] = new int [3];
3
Array [0] [1] = 43;
4
 
5
Int [,] array2 = new int [4, 5]; // multidimensional array
6
Array2 [0, 1] = 32;
Java is a variable-length array, that is, an array.

Checked and unchecked:
When checked is used, an exception is thrown when overflow occurs. The default value is unchecked.

1
Bytes B = 255;
2
Checked {B ++;} // throw an exception
3
Console. WriteLine (B );
 

Delegate:
The delegate (proxy) seems to be a function pointer in c ++, and the delegate is also a type derived from System. MulticastDelegate

1
Int add (int a, int B) {return a + B ;}
2
Int subtract (int a, int B) {return a-B ;}
3
Delegate int Fun (int a, int B );
4
Fun f = add;
5
Int a = f (); // get 2
6
F = subtract;
7
Int B = f (); // get 0
Multicast delegation can use the +,-, + =, and-= operators.

Set:
Array and ArrayList are indexed by brackets []. Dictionary can also be indexed by any type (this reminds me of js)

1
Dictionary <string, int> dic = new Dictionary <string, int> ();
2
List <int> l = new List <int> ();
3
...
4
Int a = (int) al [0];
5
Int B = dic ["haha"];
 

Function overload:
Virtual explicitly specifies the virtual function or virtual attribute. Without virtual, It is not virtual by default (this is the same as C ++, while java is virtual by default ).
The override keyword is used to overload the virtual function. The new keyword is used to explicitly declare a subclass function to hide functions with the same name as the parent class.

01
Class
02
{
03
Public void M () {Console. WriteLine ("");}
04
Public virtual void V () {Console. WriteLine ("");}
05
}
06
Class B:
07
{
08
Public new void M () {Console. WriteLine ("B ");}
09
Public override void V () {Console. WriteLine ("B ");}
10
}
11
...
12
A a = new B ();
13
B B = new B ();
14
A. M (); //
15
A. V (); // B
16
B. M (); // B
17
B. V (); // B
The call of a virtual function depends on the instance type, and the call of a non-virtual function depends on the type of the referenced variable.
I tried to find that verride cannot be used with virtual. But if a class C needs to overload Class B's V () method in the future, how can I make B. V () a virtual function?


Operator overload:
The reload method is the same as that in C ++ (it is very convenient to remember that I like it most when I learned C ++), but there are more limits than C ++. For example, it must be declared as public and static. You cannot reload "+ =". After reload "+", "+ =" can naturally be used. You cannot reload "=". You can reload "&". you cannot reload "&", "<", ">", "<=", "> =", and "! "=" And "=" must be reloaded in pairs. You cannot load only one of them. It seems that you can only load them as static.

1
Class Time
2
{
3
Public int hour, min;
4
Public Time (int h, int m) {hour = h; min = m ;}
5
Public static Time operator + (Time a, Time B)
6
{Return new Time (a. hour + B. hour, a. min + B. min );}
7
}
 

Custom Data type conversion:
Very powerful. It must be public static in the same way as an overloaded operator. It can be converted between a class and a basic data type. Implicit indicates implicit conversion, and explicit it indicates that the conversion must be displayed. Conversion functions must be written in the definition of classes or struct of the source or target type. classes with inheritance relationships that can be converted cannot be overloaded.

01
Class C
02
{
03
Public int x;
04
Public C (int I) {x = I ;}
05
Public static implicit operator int (C c) {return c. x ;}
06
Public static explicit operator C (int I) {return new C (I );}
07
}
08
...
09
Int I = new C (3); // I = 3
10
C c = (C) 5; // c. x = 5
11
Byte B = (byte) new C (2); // B = 2, C is converted to int type and then converted to byte
 

Boxing and unboxing:
Boxing is automatically loaded between the reference type and value type, while unboxing must be explicitly stated.

1
Int I = 0;
2
Object o = I; // I is automatically packed into reference type o
3
String str = 2. ToString (); // 2 is automatically packed into a temporary reference type.
4
Int B = (int) o; // unboxing
5
Console. WriteLine (Object. ReferenceEquals (I, I ));
6
// If the value type is compared with the ReferenceEquals (a, B) function, false is always obtained, because the referenced variable after packing points to a copy of the original value type variable instead of the original value. In this way, operations on the boxed object will not affect the content of the original value type.
In C # advanced programming, the. Net Framework implicitly provides an implicit class, which is the same as the struct, but is a reference type for packing. The enumeration type is similar.

Is and:
"Is" is equivalent to "instanceof" in java; as is used to try type conversion

1
ClassA a = (ClassB) B; // an exception may be thrown if the conversion fails.
2
ClassA a = B as ClassA // when the conversion fails, a = null

Event:
In event processing, c # uses delegation, which is not as clear as the listener concept in java.

Class visibility:
Protected -- can only be accessed by a class or a derived class
Internal -- can only be accessed by the internal code of the project (assembly) that defines it
Private -- the default value is private.
Sealed -- equivalent to final in java, so that the class cannot be derived, or the function cannot be overloaded
The keywords virtual, override, new, and extern are unclear.

Property:
I feel bored. I cannot tell whether it is a property or a field. In fact, the setter and getter methods in java provide the same functions as www.2cto.com.

Inheritance:
It is also a single inheritance, multi-interface, separated by commas, the inherited class is written in the first, then the interface to be implemented, separated by commas. The parent class uses the base keyword. The constructor can be followed by a colon and other constructor.

View sourceprint?
01
Interface I {void dosomething ();}
02
Class
03
{
04
Private int;
05
Public A (int a) {this. a = ;}
06
Public void Method () {Console. WriteLine ("A. Method ()");}
07
}
08
Class B: A, I
09
{
10
Private int B;
11
Public void dosomething (){}
12
Public new void Method () {base. Method (); Console. WriteLine ("B. Method ");}
13
Public B (int a, int B): base (a) {this. B = B ;}
14
Public B (int B): this (0, B ){}
15
}


From leo_de_macondo

Related Article

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.