C # Learning Notes

Source: Internet
Author: User
Tags control characters

Read Console.readkey ();
The references item contains a list of. NET libraries used in the project
Pop-up message dialog box MessageBox.Show ("");
Event with + =, indicating registration, + = new event handler
Object Sender indicates who has this event
Object of the Eventargs event
Random number of the randomness rnd = new random (); Rnd. Next (255) random number of//0~255
Timer object inside the component, do not display, equivalent to the alarm clock property Interval, time interval, in milliseconds, to change the enable to True
Console Application If you press F5 to flash through, you can use Ctrl+f5
Console.WriteLine ("AAA"), read into Console.ReadLine ()
Console.WriteLine ("{0}, {1}", A, b); {0} represents a No. 0 parameter
Event-driven programming mechanisms
Web applications also become ASP.
Resolves the text to a real number double A = double. Parse (TextBox1.Text)
Because C # automates the release of objects, users generally do not define a destructor method
private string _name//Field
public string Name {
get {return _name;}
set {_name = value}
}


Class A {
public int A;
}
Inherit a, overwrite a to use new
Class B:a {
new public int A;
}
Methods to access the parent class
Base.sayhello ();

If it cannot be converted, then the value is null
Student S1 = s2 as Student;
Is operator
If (P is person)

Access control characters
Internal//non-subclasses of the same assembly can also be accessed
Static can also be modified to construct a class, which is called only once, and the call time is indeterminate.
ReadOnly equivalent to an immutable amount, can only be repeated once, in the construction or at the time of declaration of the assignment, can only modify the field
Sealed indicates non-inheritable
Abstract indicates that no instantiation can be instantiated
The interface interface, a reference type, is more abstract than an abstract class, helping to implement multiple inheritance (because C # is just a single inheritance), implementing the same behavior for unrelated classes
Interfaces can be used to understand the interface of an object, without needing to know the class of the object, which is customary to start with the letter I
Define an interface
Public interface Istringlist {
void Add (String);
int Count {get;}
String This[int index] {get; set;}
}//here public abstract Two keywords do not write out, because the default is public abstract
When multiple interfaces have the same method, the class of the inheriting interface is prefixed with the interface name when implementing the method
such as void Iwindow.close ()
Then the coercion type conversion ((Iwindow) p) is used when calling. Close (); You can call the Close method of the Iwindow interface
The difference between strcut and class: A struct is a value type, it cannot contain a parameterless construction method, a field cannot give an initial value when defined, and each field must be assigned a value in the constructor method
An enumeration is actually a meaningful integer

The delegate delegate, which is actually a function pointer, wraps the function prototype, is a reference type, a type, and the event is not a type
Public delegate double MyDelegate (double x);
Instantiation of
MyDelegate d2 = new MyDelegate (obj.mymethod)//parentheses is a method of an object/class that wraps this method.

Consolidation of Delegates-multicast MulticastDelegate:
A delegate can contain multiple functions, the return value has no meaning, there is no precedence, with + = =
Different delegates cannot be converted to each other, meaning that a delegate and a delegate cannot be added or reduced.

Event equivalent to callback function
This.button1.Click + = new System.EventHandler (This.button1_click);
You can also:
This.button1.Click + = this.button1_click;//cannot be thought of as adding a function, but rather the compiler converts the function into a delegate
private void Button1_Click (object sender, EventArgs e) {//sender who has this event, E event argument
....
}
Declaration of the event
Public event delegate Name Events name
Definition and use of event six
1. Declaring event arguments class xxxeventargs{}
2. Declaring a delegate delegate void Xxxeventhandler (obj, args)
3. Define events public Event type name//in a class
4. Event Occurrence: Event name (parameter)//In a class
5. Define a method: Void method name (obj, args)//In another class
6. Registration event: XXX. Event + = new Delegate (method name)//In someone else's class
Using system events such as (click) is easier (just press two tab after + = in Visual Studio)
Event can be used outside the class with + = =

Can define an accessor for an event
Modifier event delegate type name Events name {
Add {e + = value;}
Remove {e-= value;}//Do not write add remove in actual programming
}

anonymous function delegate (parameter) {method body}
can be implicitly converted to a compatible delegate type
Lamda expressions are shorthand for anonymous methods, omit delegate, and even omit parameter types:
directly with (parameter) =>{} or x=>x*x;
Anonymous functions One more function: Anonymous functions that do not write (parameters) can be specially appointed for any number of arguments

Linq:language Integrated Query
var m = from C in Customers where C.age > c.name select New {c.name, c.phone}
Another way of writing
var m = Customers
. Where c.age > 10
. C.name
. Select new {c.name, c.phone};

Type conversion operator declaration form
public static implicit operator type (type parameter name) {}//Implicit conversion
public static explicit operator type (type parameter name) {}//cast

Exception handling
If there is a return statement in the TRY clause, the finally clause also executes
try {...}
catch (Exception e) {...}
Finally {...}
System.Exception class
When a catch exception occurs, the subclass exception (more specific exception) is written before the parent class exception (more general exception).
Throw exception Throw expression

C # Learning Notes

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.