for more relevant articles, see my personal homepage: zhongxiewei.com
Class definition
Defines the modifier for the class:
- Internal (default access modifier), the code in the current project has access to it
- Public (access modifier), exposing its accessibility
- Abstract, which indicates that the class is an abstraction class, cannot be instantiated, can only be inherited, and may have abstract members
- Sealed, cannot be inherited
The modifiers for a class need to be noted:
- No private and protected modifiers
- The public class cannot inherit from the internal class, such as
public class MyClass : MyBase {} // MyBase is a internal class
, but internal can inherit from the public class
- When inheriting an abstract class, unless the current class is also abstract, it must be initialized for all abstract members.
- Inheritance is not allowed for multiple inheritance
- In a class definition, the parent class and interface exist at the same time, and the parent class must be in the first position after the colon, such as:
public class MyClass : MyBase, IMyInterface {}
Interface definition
Defines the modifiers for the interface:
- Internal (default access modifier), the code in the current project has access to it
- Public (access modifier), exposing its accessibility
It is important to note that:
- No sealed,abstract modifier
- Allow multiple inheritance of interfaces
- When an interface is inherited by a class, all functions in the interface must implement the
System.Object
When a class does not inherit an object, it inherits System.Object by default, so it is necessary to look at what is in that class.
Method |
Description |
Object () |
constructor function |
~object () |
Destructors |
virtual bool Equals (object) |
This and other objects are compared |
static bool Equals (Object,object) |
Compares the input two objects, calling the last function internally, true all null |
static bool ReferenceEquals (Object,object) |
Comparison of two objects entered is not a reference to the same object |
Virtual String ToString () |
Returns the name of the class type by default |
Object MemberwiseClone () |
Build a new object instance and copy the member variable, and the member variable of the reference type still references the same variable |
System.Type GetType () |
Returns the type of the object |
virtual int GetHashCode () |
Returns the hash value of an object |
An example of using GetType:
if (myObj.GetTypetypeof(MyComplexClass)){ // myObj is an instance of the class MyComplexClass}
Class member definitions
Modifier keywords:
- Public, any code can access
- Private, the code inside the class can access
- Internal, the code inside the project will be able to access
- Protected, code in an inherited subclass can access the
- Static, which belongs to a member of a class, not just an object
Modifiers for member variables only:
- ReadOnly, a read-only variable, initialized in a declaration or constructor function
Modifiers for member functions only:
- Virtual, a function that can be overloaded
- Abstract, a function that must be overloaded to exist only in the abstract class
- Override, overloading a function in a base class
- extern, the definition of a function in other places
- Sealed, functions cannot be further overloaded
Property Definition
privateint myInt;publicint MyIntProp{ get { return myInt; } set { if010) myInt = value; else throw(...); }}
Hide a function in a base class
It's easy to think of a way to implement the following:
publicclass MyBaseClass{ publicvoidDoSomething() // base implementation }}publicclass MyDerivedClass : MyBaseClass{ publicvoidDoSomething() // Derived class implementation, hides base implementation }}
But the code above will be warned during compilation that the DoSomething function in Myderivedclass hides the DoSomething function in the base class, using the new keyword, that is public new void DoSomething() {}
.
Collection
You can use the existing System.Collections.ArrayList implementation. What is to be said here is the definition of collections. Can be implemented in the following ways, such as:
public class animals:collectionbase{//add function public v OID add (Animal newanimal) {list. (Newanimal); } //remove function public void (Animal oldanimal) {list. (Oldanimal); } //implement Myanimal[0] access mode public Animal this [ int Animalindex] {get {return (Animal) list[a Nimalindex]; } set {List[animalindex] = value; }} public animals () {}}
Operator overloading
The member function of the overloaded operator is public static. It is important to note that there are two additional keywords explicit and implicitduring the definition of a member function for variable conversion.
//Two-dollar operator + Public classclass1{ Public intVal//Two-dollar operator Public StaticClass1operator+ (Class1 OP1, Class1 op2) {Class1 ReturnVal =New Class1(); ReturnVal.Val= Op1.Val+ OP2.Val;returnReturnVal; }//unary operator,- Public StaticClass1operator-(Class1 op1) {Class1 ReturnVal =New Class1(); ReturnVal.Val=-op1.Val;returnReturnVal; }}
Is operator and as operator
Is operator: detects that an unknown variable can be converted into a variable of known type. Returns true if it is possible, otherwise false. However, it is not possible to determine whether the two types are the same.
Its syntax structure is:<operand> is <type>
The possible results of this expression are as follows:
- If type is a class type, then the
<operand>
returned result is true if it is that type, or subclass of that class, or can be converted to that type.
- If type is interface, the return result is true when operand is the type, or the implementation of that type.
- If the type is a value type, such as int, then when operand is that type, or can be implicitly converted to that type, the return result is true
As operator to convert a variable to a special reference type. <operand> as <type>
.
Suitable for the following environments,
- If the type of operand is the same as types.
- If operand can be implicitly converted to type
- If the operand is able to be converted to type types without any damage.
If the conversion is not possible, the returned result is null.
newBaseClassas// return null 不会抛出异常,如果使用 ... = (DerivedClass)obj1; 则会抛出异常// 如果以下面的方式进行转换则是可以的newDerivedClassas// not null
Deep copy
Directly using the MemberwiseClone () within the System.Object object, when encountering a class containing reference variables, it is not possible to implement a deep copy, such as:
Public classcontent{ Public intVal;} Public classcloner{ PublicContent mycontent =New Content(); Public Cloner(intnewval) {mycontent.Val= newval; } Public Object getcopy() {return MemberwiseClone(); }}//Specific callCloner MySource =New Cloner(5); Cloner mytarget = (cloner) mySource.getcopy(); MySource.mycontent.Val=2;//This will result in a corresponding change of myTarget.MyTarget.Val to 2
To achieve a deep copy, the ICloneable interface can be implemented.
publicclass Cloner : ICloneable{ publicnewContent(); publicCloner(int newVal) { MyContent.Val = newVal; } publicobjectClone() { newCloner(MyContent.Val); return clonedCloner; // 或是 // Cloner clonedCloner = new Cloner(); // clonedCloner.MyContent = MyContent.Clone(); // return clonedCloner; }}
Custom exceptions
In a custom process, simply inherit the Expection
class and implement it. Such as:
publicclass AgeBelowZeroException : Exception{ publicAgeBelowZeroException() : base("The age must >= 0") { }}
Basic concepts related to classes----Beginning Visual C #