- Function and Method are synonymous in C#
Encapsured in some class and struct Doesn't support global method Value and Reference Parameters
In both case, a copy of parameters passes from caller to called method
- Value Type: a copy of the data
- Reference Type: a copy of the reference
ref Method Parameter
This keyword tells the C# compiler that the arguments being passed point to the same memory as the variables in the calling method.
Limitation: Initialize the passed arguments efore calling the method
The ref parameters are prefixed with an ampersand(&) stind. popping indirect values off the stack.
out Method Parameter
Doesn't need the calling code to initialize the passed arguments The arguments must be modified in the method.
Method Overloading
Enable C# programmer to use same method name multiple times with only the passed parameters changed.
Each method's parameters list must be different, it doesn't count if a method's return type and access modifier are different
The buttom line is that that must be difference in the parameter list
Compiler consider ref and out equivalent to each other.
Overloading Constructor
default constructor: Every instantiation must have a matching constructor if the code is to compile, even if the constructor doesn't do anything.
Inheritance and Overloading
An overloaded method is considered an overload even if the overloaded versions of the function exist in a base class rather than the current class.
Variable Method Parameters
Params: Specify a variable number of a method parameters by using params keyword and specify an array in the method's parameter's list.
Virtual Method (method overriding)
Use the new keyword with the derived class's method definition, the fact base calss and derived class have identical signature is pure coincidence and of no consequence
Polymorphism --- enable to define the method multiple times throughout the class hierarchy so that the runtime can call the apropriate version of the method for specific object being used.
early binding late binding
virtual keyword on the base class override keyword on the derived class
callvirt. Calls virtual method of obj.(and nonvirtual)
Virtual function table -- VTBL
newslot. new slot in the object's VTBL virtual. a virtual method
in the non-polymorphism, both the base and the drived class are not virtual
-
The override function must have the same access level as virtual function it override.
-
A virtual member can't be declared as private.
-
To declare a virtual protected member is legal but dumb
combine new and virtual when declaring a class function
Calling Virtual Method from Constructors
Static methods
A static method is a method that exists in a class as whole, rather than in specific instance of the class.
Class.Method syntax to call the method. This syntax is necessary even if the user has a reference to an instance of the class
Access to class member
A static method can access any static memeber within the class, but it can't access an instance member.
Nonstatic method can access both static and nonstatic members.
Static Constructor
-
Only one static constructor
-
can't take parameters
-
can't access instance member -- include this pointer
-
a static consturctor executed before the first instance of a class is created
-
access modifiers are not allowed on static constructors
-
you can provide a nonstatic constructor with the same signature as the static constructor, both the constructors will be called and the static version being called first.
|