Table of Contents
- Module 7. Building Types
This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.
Module 7. Building Types
Overloading Methods
Reference:
Member overloading means creating two or more members on the same type (same return type) that differ only in the number or type of parameters but have the same name. Because only methods, constructors, and indexed properties can have parameters, only those members can be overloaded.
Methods, constructors, indexers, and operators are characterized by their signatures. A signature makes a method look unique to the C# compiler.
Method signature consists:
- Method name
- Parameter types, & number of paramter
Method signature DOES NOT include:
- return type/kind
Defining Properties
Reference:
A property is a member that provides a flexible mechanism to read, write, or compute the value of a private field. Properties can be used as if they are public data members, but they are actually special methods called accessors. This enables data to be accessed easily and still helps promote the safety and flexibility of methods.
private string name;
public string Name
{
get
{
return name;
}
set
{
if (!String.IsNullOrEmpty(value))
{
name = value;
}
}
}
Instead of a public data Name
, we create a private data and public property member (with getter and setter)
Defining Property Getters and Setters
Reference:
- Auto-Implemented Properties (C# Programming Guide)
- How to implement a lightweight class with auto-implemented properties (C# Programming Guide)
Instead of implement property, it can be implemented automatically if it's standard getter & (or private) setter (as read-only object). Besides, we can declare setter to be private
Overall, property provide more concise and convenient way to define than field.
Defining readonly & const Members
References:
Some class members are decalred to be readonly. There are multiple methods can achieve this objective:
readonly
keyword can be used to create readonly field, that can only be initialized during declaration and overwritten in constructor.const
is more strict, it can only be initialized in during declaration, and cannot be overwritten in any place. (It must be defined before compilation).- Constant fileds are NOT variable.
- Local variable within block can be constant.
const
value can be assigned withpublic
access modifier, as it cannot be modified- Normally
const
values are CAPITAL. const
value is treaded asstatic
(i.e. belong to class, instead of object as it cannot be changed)
Introducing Events and Delegate
- Events is not in style with server frameworks
- Events are hard to understand
- Events are popular in forms and desktop programming
- Events build on top of delegates
Defining a Delegate
Reference:
- Early binding and late binding in C++
- Introduction to Delegates
- 委托(C# 编程指南)
- Best explain with e.g.: Delegates in C#
What is Delegate
In some programs, it is not possible to know which function will be called until runtime (when the program is run). This is known as late binding (or dynamic binding). In C++, one way to get late binding is to use function pointers. In C#, we can get late binding using delegate
Methods, constructors, indexers, and operators are characterized by their signatures.
In simple words, we can say that the delegates in C# are the Type-Safe Function Pointer. It means they hold the reference of a method or function and then calls that method for execution.
Steps to define & use Delegates
To invoke or call a method using delegates, 3 steps are required:
- Defining a delegate
- Instantiating a delegate
- Invoking a delegate
Step 1: Defining a delegate
Syntax (outside of class):
<Access Modifier> delegate <return type> <delegate name> (arguments list);
e.g. for method (within class)
public void Add(int x, int y)
{
Console.WriteLine(@"The Sum of {0} and {1}, is {2} ", x, y, (x + y));
}
we have delegate defined as
public delegate void AddDelegate(int a, int b);
Step 2: Instantiating the Delegate
To consume the delegate you must first create an object of the delegate. While creating the object using defined delegate, you should parse the method you want to execute as a parameter
Syntax:
DelegateName ObjectName = new Delegate_Name (target_function_name);
e.g.
AddDelegate ad = new AddDelegate(obj.Add);
Step 3: Invoking the delegate
Now call the delegate by supplying the required values to the parameters so that the methods (bound with the delegates) get executed internally.
e.g.
ad(100, 50);
Rules of using Delegates in C#:
- A delegate in C# is a user-defined type and hence before invoking a method using delegate, we must have to define that delegate first.
- The signature of the delegate must match the signature of the method, the delegate points to otherwise we will get a compiler error. This is the reason why delegates are called as type-safe function pointers.
- A Delegate is similar to a class. Means we can create an instance of a delegate and when we do so, we need to pass the method name as a parameter to the delegate constructor, and it is the function the delegate will point to
- Tip to remember delegate syntax: Delegates syntax look very much similar to a method with a delegate keyword.
Using Multi-cast Delegates
Reference:
- Delegate and MulticastDelegate classes
- Best explain with e.g.: Multicast Delegates in C# with Examples
A Multicast Delegate is a delegate that holds the references of more than one function. When mutliple methods having the same signature so we can create a single delegate that holds the reference of the these methods.
- use += operator to chain delegates together and -= operator to remove.
- A multicast delegate invokes the methods in the invocation list, in the same order in which they are added.
Overall, delegate enable us to declare a method that used like a variable.
Defining an Event & Subscribing to an Event
Reference:
Using delegate, we can implement Event Handler in C# to achieve subscription.
Event is not popular within ASP.NET, but very popular in desktop app & Mobile development (e.g. mouse clicking, keyboard pressing)
TODO: research this part. It's very complicated.