Review C #

Source: Internet
Author: User

It may take some time to write a program in c #, but there are many concepts that are not very clear, or simply copy the existing code, and you can also create something for revision, modification, and modification, however, it is still a little difficult to write an empty hand. I plan to learn the most basic things over the past few days. There is a Microsoft. NET Framework Program Design (revised version) (Jeffrey Richter) at hand. You should repeat it.
1. Type access control (control from loose to strict)
Public: can be called by any code in any assembly.
Internal (Friend in assembly, VB): can be called by any code in the same assembly.
Protected (Family): it can be called by code of the same type and derived type, and cannot be accessed by other classes.
Private: It can only be accessed in the same type.
The default value is internal.
2. operations performed by a new object
A. Allocate the number of bytes required for the specified type from the managed heap to serve as the memory space for storing its objects.
B. Additional members of the initial object. Each object has two additional members. CLR uses these two members to manage objects. One is a pointer to the class method table, and the other is SyncBlockIndex.
C. input the parameter specified by the new operator to call the constructor.
3. is operator and typeof
Used to determine whether an object is of a certain type (or base class ). Returns true or false. If the object is null, false is returned.
Bool B = o is object;
Typeof () is used to input a type rather than an instance, and a system. type instance is returned.
4. primitive type and FCL type
The primitive type corresponds to the FCL type. C # The primitive type is recommended in the language specification. However, the FCL type is recommended for Jeffrey Richter.
Common
Remarks on the FCL type of the primitive type
Byte System. Byte
Int System. Int32
Long System. Int64
Float System. Single
Double System. Double
Bool System. Boolean
5. packing and unpacking
Packing is to convert the value type to the application type.
Binning is the opposite.
Packing process:
A. Create a new space in the managed heap and copy all the members of the Value-type instance to the new space.
B. Return the reference of the new space.
6. Class (type) members
A. Constant: the constant is static, belongs to a class, not an instance, and cannot be accessed using this.
Public const int theConst = 0;
B. readonly modifier: The variable modified by readonly belongs to the instance and can be modified in the constructor.
C. Type constructor (static constructor): static fields of the initialization type.
Static classname () {}, cannot be modified with public or other parameters. Run this command when the type is accessed for the first time.
D. static member: belongs to the type, does not belong to the instance, and cannot access non-static members in the static method. Can access static members in non-static members, but cannot add this
7. Type predefined features (abstract, sealed)
Abstract (VB: MustInherit): it cannot be instantiated and can be used as a base class. The base class can be instantiated.
Sealed (VB: NotInheritable): cannot be a base class
8. predefined features of fields and Methods
A. predefined Field Features
Static (VB: Shared)
Readonly
B. predefined method features
Static (VB: Shared): static method, which belongs to a type rather than a class instance.
Virtual (VB: overridable): When a method is called, it is called according to the method at the end of the Object Inheritance chain. If the parent class is virtual and the Child class override, it can be called at the end of the inheritance chain. Otherwise, it will be called according to the compilation type. The override method to be overwritten should be written to virtual in the base class.
New (VB: Shadows): do not override the base class Method
Override (Overrides): overwrites the base class method and works with the virtual
Abstract (Mustoverride)
Sealed (NotOverridable)
9. Delegate
A delegate is equivalent to a function pointer and provides a function callback mechanism using a type-safe method.
The function name is a delegated instance.
A. Define the delegate type
Inside the class or outside the class, the position is equivalent to a type.
Public delegate void FeedBack (string arg1, string arg2 );
Void indicates the return value of the delegate type, and parentheses indicate parameters of the delegate type.
B. Define a delegated instance
FeedBack fb = new FeedBack (function name );
Or define a delegate pointing to null, and then add a function to the delegate chain of the delegate.
FeedBack fb = null;
Fb + = function name;
Or simply use the function name as a delegate instance.
FeedBack fb = function name;
10. Class events
A: defines an event parameter class (in the same namespace as the class) and inherits from EventAgrs.
B: define a delegate type (in the same namespace as the class) public delegate void EventHandler (object sender, EventArgs e );
C: define events in the class (actually, define a delegate)
Public event EventHandler OnEvent; (the event keyword can be omitted)
D: Events
If (OnEvent! = Null)
{
OnEvent (sender, e );
}
If the event is registered, it will not be null.
Capture events
Subscription: Object Name. OnEvent + = new EventHandler (processing function name );
Unsubscribe: Object Name. OnEvent-= new EventHandler (processing function name );
Note: As long as an object still registers events with another object, the object cannot be recycled.
So all event registration should be deregistered where necessary
11. Exception Handling
Exception Handling has always been confusing, where exceptions should be caught, where exceptions should not be caught, how to handle caught exceptions, and the impact of exceptions on program performance, is to clarify the problem.
An exception is a violation of the implicit hypothesis of the program interface. So how can we notify the caller that it violates the assumption? The answer is to throw an exception.
C # You can write catch only, without parentheses, to capture all exceptions. It can be thorw. If you do not specify the exception to be thrown, The caught exception is thrown.
Finally internal code is used for cleaning (such as closing the file and closing the database link), even if the code in try throws an exception, or there is a return in try, finally code will also be executed. It can be considered that the exception thrown up is of the same level as the return, while finally runs before them.
12. Interface

13. Write Recursion
A recursive class:
Public class Node
{
Public string Name;

Public List <Node> Childs;

}

Print this class with a function:
Private void print (Node n)
{
Console. WriteLine (n. Name );
// Recursive operations at each layer

If (n. Childs! = Null)
{
Foreach (Node c in n. Childs)
{
Print (c );
}
}
// Exit the recursive Condition
}

14. Inheritance
Q: When the subclass is instantiated, what is the relationship with the constructor of the parent class? How to explicitly call the constructor of the parent class?

15:

In a case of c #, you cannot write multiple conditions at the same time as in vb, But you can write these conditions as follows:
Switch (afn)
{
Case 0x00:
Case 0x01:
Case 0x02:
Case 0x03:
Case 0x04:
Case 0x05:
Case 0x06:
Case 0x0A:
Case 0x0B:
Case 0x0C:
Case 0x0D:
Case 0x0E:
Case 0x0F:
Case 0x10:
Return (AFNS) afn;
Default:
Return AFNS. Unknown;
}

(That is, if there is no statement in case, you can not use break)

16. base keyword
Used to access a base class member from a derived class:
A. Call methods that have been overwritten by other methods on the base class.
B. Specify the base class constructor to call when creating a derived class instance.
C. base class access can only be performed in constructors, instance methods, or instance attribute accessors. It is incorrect to use the base keyword in a static method.

Call the base class method:
Public class Person
{
Protected string ssn = "444-55-6666 ";
Protected string name = "John L. Malgraine ";

Public virtual void GetInfo ()
{
Console. WriteLine ("Name: {0}", name );
Console. WriteLine ("SSN: {0}", ssn );
}
}
Class Employee: Person
{
Public string id = "ABC567EFG ";
Public override void GetInfo ()
{
// Calling the base class GetInfo method:
Base. GetInfo ();
Console. WriteLine ("Employee ID: {0}", id );
}
}

Call the base class constructor:
Public class BaseClass
{
Int num;

Public BaseClass ()
{
Console. WriteLine ("in BaseClass ()");
}

Public BaseClass (int I)
{
Num = I;
Console. WriteLine ("in BaseClass (int I )");
}

Public int GetNum ()
{
Return num;
}
}

Public class DerivedClass: BaseClass
{
// This constructor will call BaseClass. BaseClass ()
Public DerivedClass ():Base()
{
}

// This constructor will call BaseClass. BaseClass (int I)
Public DerivedClass (int I ):Base(I)
{
}

Static void Main ()
{
DerivedClass md = new DerivedClass ();
DerivedClass md1 = new DerivedClass (1 );
}
}

Dispose and finalize
Refer to blog: http://www.cnblogs.com/xlshcn/archive/2007/01/16/idisposable.html
// Design pattern for a base class.
Public class Base: IDisposable
{
// Implement IDisposable.
Public void Dispose ()
{
Dispose (true );
GC. SuppressFinalize (this );
}

Protected virtual void Dispose (bool disposing)
{
If (disposing)
{
// Free other state (managed objects ).
}
// Free your own state (unmanaged objects ).
// Set large fields to null.
}

// Use C # destructor syntax for finalization code.
~ Base ()
{
// Simply call Dispose (false ).
Dispose (false );
}
}
// Design pattern for a derived class.
Public class Derived: Base
{
Protected override void Dispose (bool disposing)
{
If (disposing)
{
// Release managed resources.
}
// Release unmanaged resources.
// Set large fields to null.
// Call Dispose on your base class.
Base. Dispose (disposing );
}
// The derived class does not have a Finalize method
// Or a Dispose method without parameters because it inherits
// Them from the base class.
}

Usage of x. gettype and typeof
1. determine whether an instance is of a certain type
C # o is classname
Vb typeof o is classname
2. c # typeof (int)
Bv gettype (integer)
3. object Class Method
C # o. GetType
Vb o. GetType
X. override)

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.