🗊 Презентация Operators, delegates and events

Нажмите для полного просмотра!
Operators, delegates and events, слайд №1 Operators, delegates and events, слайд №2 Operators, delegates and events, слайд №3 Operators, delegates and events, слайд №4 Operators, delegates and events, слайд №5 Operators, delegates and events, слайд №6 Operators, delegates and events, слайд №7 Operators, delegates and events, слайд №8 Operators, delegates and events, слайд №9 Operators, delegates and events, слайд №10 Operators, delegates and events, слайд №11 Operators, delegates and events, слайд №12 Operators, delegates and events, слайд №13 Operators, delegates and events, слайд №14 Operators, delegates and events, слайд №15 Operators, delegates and events, слайд №16 Operators, delegates and events, слайд №17 Operators, delegates and events, слайд №18 Operators, delegates and events, слайд №19 Operators, delegates and events, слайд №20 Operators, delegates and events, слайд №21 Operators, delegates and events, слайд №22 Operators, delegates and events, слайд №23 Operators, delegates and events, слайд №24 Operators, delegates and events, слайд №25 Operators, delegates and events, слайд №26 Operators, delegates and events, слайд №27 Operators, delegates and events, слайд №28 Operators, delegates and events, слайд №29 Operators, delegates and events, слайд №30 Operators, delegates and events, слайд №31 Operators, delegates and events, слайд №32 Operators, delegates and events, слайд №33 Operators, delegates and events, слайд №34 Operators, delegates and events, слайд №35 Operators, delegates and events, слайд №36 Operators, delegates and events, слайд №37 Operators, delegates and events, слайд №38 Operators, delegates and events, слайд №39 Operators, delegates and events, слайд №40 Operators, delegates and events, слайд №41 Operators, delegates and events, слайд №42 Operators, delegates and events, слайд №43 Operators, delegates and events, слайд №44 Operators, delegates and events, слайд №45 Operators, delegates and events, слайд №46 Operators, delegates and events, слайд №47 Operators, delegates and events, слайд №48 Operators, delegates and events, слайд №49 Operators, delegates and events, слайд №50 Operators, delegates and events, слайд №51 Operators, delegates and events, слайд №52 Operators, delegates and events, слайд №53

Содержание

Вы можете ознакомиться и скачать презентацию на тему Operators, delegates and events. Доклад-сообщение содержит 53 слайдов. Презентации для любого класса можно скачать бесплатно. Если материал и наш сайт презентаций Mypresentation Вам понравились – поделитесь им с друзьями с помощью социальных кнопок и добавьте в закладки в своем браузере.

Слайды и текст этой презентации


Слайд 1


Operators, Delegates and Events
Описание слайда:
Operators, Delegates and Events

Слайд 2


Agenda Introduction to Operators Operator Overloading Creating and Using Delegates Defining and Using Events
Описание слайда:
Agenda Introduction to Operators Operator Overloading Creating and Using Delegates Defining and Using Events

Слайд 3


Operators, Delegates and Events
Описание слайда:
Operators, Delegates and Events

Слайд 4


Agenda Introduction to Operators Operator Overloading Creating and Using Delegates Defining and Using Events
Описание слайда:
Agenda Introduction to Operators Operator Overloading Creating and Using Delegates Defining and Using Events

Слайд 5


Introduction to Operators Operators and Methods Predefined C# Operators Operators are different from methods. They have special requirements that...
Описание слайда:
Introduction to Operators Operators and Methods Predefined C# Operators Operators are different from methods. They have special requirements that enable them to function as expected. C# has a number of predefined operators that you can use to manipulate the types and classes supplied with the Microsoft® .NET Framework.

Слайд 6


Operators and Methods Using methods Reduces clarity Increases risk of errors, both syntactic and semantic Using operators Makes expressions clear
Описание слайда:
Operators and Methods Using methods Reduces clarity Increases risk of errors, both syntactic and semantic Using operators Makes expressions clear

Слайд 7


Operators and Methods The purpose of operators is to make expressions clear and easy to understand. We can use Method for adding two numbers:...
Описание слайда:
Operators and Methods The purpose of operators is to make expressions clear and easy to understand. We can use Method for adding two numbers: myIntVar1 = Int.Add(myIntVar2, myIntVar3); myIntVar2 = Int.Add(myIntVar2, 1); We can use Operator+: myIntVar1 = myIntVar2 + myIntVar3; myIntVar2 = myIntVar2 + 1;

Слайд 8


Predefined C# Operators
Описание слайда:
Predefined C# Operators

Слайд 9


Predefined C# Operators The C# language provides a large set of predefined operators. Following is the complete list. Operator category Operators...
Описание слайда:
Predefined C# Operators The C# language provides a large set of predefined operators. Following is the complete list. Operator category Operators Arithmetic +, -, *, /, % Logical (Boolean and bitwise) &, |, ^, !, ~, &&, ||, true, false String concatenation + Increment and decrement ++, -- Shift Relational ==, !=, , = Assignment =, +=, -=, *=, /=, %=, &=, |=, = Member access . Indexing [ ]

Слайд 10


Predefined C# Operators The C# language provides a large set of predefined operators. Following is the complete list. Operator category Operators...
Описание слайда:
Predefined C# Operators The C# language provides a large set of predefined operators. Following is the complete list. Operator category Operators Cast ( ) Conditional ?: Delegate concatenation and removal +, - Object creation new Type information is, sizeof, typeof Overflow exception control checked, unchecked Indirection and address *, ->, [ ], &

Слайд 11


Operator Overloading Introduction to Operator Overloading Overloading Relational Operators Overloading Logical Operators Overloading Conversion...
Описание слайда:
Operator Overloading Introduction to Operator Overloading Overloading Relational Operators Overloading Logical Operators Overloading Conversion Operators Overloading Operators Multiple Times Quiz: Spot the Bugs

Слайд 12


Operator Overloading We should only define operators when it makes sense to do so. Operators should only be overloaded when the class or struct is a...
Описание слайда:
Operator Overloading We should only define operators when it makes sense to do so. Operators should only be overloaded when the class or struct is a piece of data (like a number), and will be used in that way. An operator should always be unambiguous in usage; there should be only one possible interpretation of what it means. For example, you should not define an increment operator (++) on an Employee class (emp1++;) because the semantics of such an operation on an Employe e are not clear.

Слайд 13


Syntax for Overloading Operators All operators must be public static methods and their names follow a particular pattern: operator@ @ - specifies...
Описание слайда:
Syntax for Overloading Operators All operators must be public static methods and their names follow a particular pattern: operator@ @ - specifies exactly which operator is being overloaded. For example, the method for overloading the addition operator is operator+.

Слайд 14


Operator Overloading. Example
Описание слайда:
Operator Overloading. Example

Слайд 15


Overloading Relational Operators Relational operators must be paired < and > = == and != For consistency, create a Compare method first and define...
Описание слайда:
Overloading Relational Operators Relational operators must be paired < and > = == and != For consistency, create a Compare method first and define all the relational operators by using Compare. Override the Equals method if overloading == and != Override the GetHashCode method if overriding Equals method

Слайд 16


Overloading Relational Operators The following code shows how to implement the relational operators, the Equals method, and the GetHashCodemethod for...
Описание слайда:
Overloading Relational Operators The following code shows how to implement the relational operators, the Equals method, and the GetHashCodemethod for the Time struct: public struct Time { // Equality public static bool operator==(Time lhs, Time rhs) { return lhs.Compare(rhs) == 0;} public static bool operator!=(Time lhs, Time rhs) { return lhs.Compare(rhs) != 0;} // Relational public static bool operator(Time lhs, Time rhs) { return lhs.Compare(rhs) > 0;} public static bool operator= 0;}

Слайд 17


Overloading Relational Operators The following code shows how to implement the relational operators, the Equals method, and the GetHashCodemethod for...
Описание слайда:
Overloading Relational Operators The following code shows how to implement the relational operators, the Equals method, and the GetHashCodemethod for the Time struct: / / Inherited virtual methods (from Object) public override bool Equals(object obj) { return (obj is Time) && Compare((Time)obj) == 0; } public override int GetHashCode( ) { return TotalMinutes( ) ; } private int Compare(Time other) { int lhs = TotalMinutes( ); int rhs = other.TotalMinutes( ); int result; if (lhs < rhs) result = -1; else if (lhs > rhs) result = +1; else result = 0; return result; } . . . }

Слайд 18


Overloading Logical Operators Operators && and || cannot be overloaded directly They are evaluated in terms of &, |, true, and false, which can be...
Описание слайда:
Overloading Logical Operators Operators && and || cannot be overloaded directly They are evaluated in terms of &, |, true, and false, which can be overloaded x && y is evaluated as T.false(x) ? x : T.&(x, y) x || y is evaluated as T.true(x) ? x : T.|(x, y)

Слайд 19


Overloading Conversion Operators Overloaded conversion operators You can define implicit and explicit conversion operators for your own classes and...
Описание слайда:
Overloading Conversion Operators Overloaded conversion operators You can define implicit and explicit conversion operators for your own classes and create programmer-defined cast operators that can be used to convert data from one type to another.

Слайд 20


Overloading Conversion Operators explicit operator Time (int minutes) It is explicit operator because not all int can be converted; a negative...
Описание слайда:
Overloading Conversion Operators explicit operator Time (int minutes) It is explicit operator because not all int can be converted; a negative argument results in an exception being thrown. explicit operator Time (float minutes) It is explicit operator because a negative parameter causes an exception to be thrown. implicit operator int (Time t1) It is implicit operator because all Time values can safely be converted to int.

Слайд 21


Overloading Conversion Operators implicit operator string (Time t1) This operator converts a Time into a string. This is also implicit because there...
Описание слайда:
Overloading Conversion Operators implicit operator string (Time t1) This operator converts a Time into a string. This is also implicit because there is no danger of losing any information in the conversion. If a class defines a string conversion operator - the class should override ToString

Слайд 22


Overloading Conversion Operators public struct Time { ... public static explicit operator Time (int minutes) // Conversion operators { return new...
Описание слайда:
Overloading Conversion Operators public struct Time { ... public static explicit operator Time (int minutes) // Conversion operators { return new Time(0, minutes); } public static explicit operator Time (float minutes) { return new Time(0, (int)minutes); } public static implicit operator int (Time t1) { return t1.TotalMinutes( ); } public static explicit operator float (Time t1) { return t1.TotalMinutes( ); } public static implicit operator string (Time t1) { return t1.ToString( ); } public override string ToString( ) // Inherited virtual methods (from Object) { return String.Format("{0}:{1:00}", hours, minutes); } ... }

Слайд 23


Overloading Operators Multiple Times The same operator can be overloaded multiple times to provide alternative implementations that take different...
Описание слайда:
Overloading Operators Multiple Times The same operator can be overloaded multiple times to provide alternative implementations that take different types as parameters. At compile time, the system establishes the method to be called depending upon the types of the parameters being used to invoke the operator.

Слайд 24


Quiz: Spot the Bugs
Описание слайда:
Quiz: Spot the Bugs

Слайд 25


Quiz: Spot the Bugs. Answers Operators must be static. The definition for the != operator should be: public static bool operator != (Time t1, Time...
Описание слайда:
Quiz: Spot the Bugs. Answers Operators must be static. The definition for the != operator should be: public static bool operator != (Time t1, Time t2) { ... } The “type” is missing. Conversion operators must be implicit or explicit. public static implicit operator float (Time t1) { ... } You cannot overload the += operator. However, += is evaluated by using the + operator, which you can overload. The Equals method should be an instance method rather than a class method. However, if you remove the static keyword, this method will hide the virtual method inherited from Object and not be invoked as expected, so the code should use override instead, as follows: public override bool Equals(Object obj) { ... } The int and implicit keywords have been transposed. The name of the operator should be int, and its type should be implicit, as follows: public static implicit operator int(Time t1) { ... }

Слайд 26


2. Windowing system Modern graphical environments use event model for communicating between interactive objects and the input/output system. The...
Описание слайда:
2. Windowing system Modern graphical environments use event model for communicating between interactive objects and the input/output system. The event model was developed to support direct manipulation interfaces. In a windowing system a user interface of an application is built of top level windows, and controls (ui components, child windows, widgets, ...). User actions with the input devices are translated into software events (messages) and distributed to the appropriate window. Events (or messages) are identified by an event type.

Слайд 27


Process of WA execution
Описание слайда:
Process of WA execution

Слайд 28


Analyzing the Problem. WinAPI How create a simple WIN32 window #include LONG WINAPI WndProc(HWND, UINT, WPARAM, LPARAM); int WINAPI WinMain(HINSTANCE...
Описание слайда:
Analyzing the Problem. WinAPI How create a simple WIN32 window #include LONG WINAPI WndProc(HWND, UINT, WPARAM, LPARAM); int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { HWND hMainWnd, hWndButton; MSG msg; WNDCLASS w; memset(&w,0,sizeof(WNDCLASS)); w.style = CS_HREDRAW | CS_VREDRAW; w.lpfnWndProc = WndProc; w.hInstance = hInstance; w.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH); w.lpszClassName = "My Class"; RegisterClass(&w);

Слайд 29


Analyzing the Problem. WinAPI hMainWnd = CreateWindow("My Class", "My title", WS_OVERLAPPEDWINDOW, 300, 200, 200, 180, NULL,...
Описание слайда:
Analyzing the Problem. WinAPI hMainWnd = CreateWindow("My Class", "My title", WS_OVERLAPPEDWINDOW, 300, 200, 200, 180, NULL, NULL, hInstance, NULL); ShowWindow(hwnd,nCmdShow); UpdateWindow(hwnd); while(GetMessage(&msg,NULL,0,0)) { TranslateMessage(&msg); DispatchMessage(&msg); } return msg.wParam; } LONG WINAPI WndProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lparam) { switch (Message) { case WM_DESTROY: PostQuitMessage(0); break; default: return DefWindowProc(hwnd, Message, wparam, lparam); } return 0; }

Слайд 30


Delegates and event handlers in .NET A dlegate allows a method to be called indirectly A delegate is a special kind of class that holds a reference...
Описание слайда:
Delegates and event handlers in .NET A dlegate allows a method to be called indirectly A delegate is a special kind of class that holds a reference to a method with a pre-defined signature. All methods invoked by the same delegate must have the same parameters and return value

Слайд 31


Using Delegates. Example.
Описание слайда:
Using Delegates. Example.

Слайд 32


Using Delegates. Example.
Описание слайда:
Using Delegates. Example.

Слайд 33


Using Delegates. Example.
Описание слайда:
Using Delegates. Example.

Слайд 34


Using Delegates. Example.
Описание слайда:
Using Delegates. Example.

Слайд 35


Using Delegates. Example. Null reference
Описание слайда:
Using Delegates. Example. Null reference

Слайд 36


Using Delegates. Example.
Описание слайда:
Using Delegates. Example.

Слайд 37


Using Delegates. Example. Multiple delegates Overloading operator+= and operator+
Описание слайда:
Using Delegates. Example. Multiple delegates Overloading operator+= and operator+

Слайд 38


Using Delegates. Example.
Описание слайда:
Using Delegates. Example.

Слайд 39


Defining and Using Events How Events Work Defining Events Passing Event Parameters Demonstration: Handling Events
Описание слайда:
Defining and Using Events How Events Work Defining Events Passing Event Parameters Demonstration: Handling Events

Слайд 40


Pattern Observer
Описание слайда:
Pattern Observer

Слайд 41


How Events Work Publisher (Student) Raises an event to alert all interested objects (subscribers) Subscriber (Parents, Registrar) Provides a method...
Описание слайда:
How Events Work Publisher (Student) Raises an event to alert all interested objects (subscribers) Subscriber (Parents, Registrar) Provides a method to be called when the event is raised

Слайд 42


Defining Events Defining an event Subscribing to an event Notifying subscribers to an event
Описание слайда:
Defining Events Defining an event Subscribing to an event Notifying subscribers to an event

Слайд 43


Passing Event Parameters Parameters for events should be passed as EventArgs Define a class descended from EventArgs to act as a container for event...
Описание слайда:
Passing Event Parameters Parameters for events should be passed as EventArgs Define a class descended from EventArgs to act as a container for event parameters The same subscribing method may be called by several events Always pass the event publisher (sender) as the first parameter to the method

Слайд 44


.NET Delegates
Описание слайда:
.NET Delegates

Слайд 45


I am pretty sure you all must have seen these delegates when writing code. IntelliSense shows methods that accept Actions, Func and some accept...
Описание слайда:
I am pretty sure you all must have seen these delegates when writing code. IntelliSense shows methods that accept Actions, Func and some accept Predicate. So what are these? Let’s find out. I am pretty sure you all must have seen these delegates when writing code. IntelliSense shows methods that accept Actions, Func and some accept Predicate. So what are these? Let’s find out. Let’s go by a simple example. I have following “Employee” class and it has a helper method which will return me a list of Employees.

Слайд 46


Operators, delegates and events, слайд №46
Описание слайда:

Слайд 47


Operators, delegates and events, слайд №47
Описание слайда:

Слайд 48


Action Action series of delegates are pointers to methods which take zero, one or more input parameters, and do not return anything. Let’s consider...
Описание слайда:
Action Action series of delegates are pointers to methods which take zero, one or more input parameters, and do not return anything. Let’s consider List.ForEach method, which accepts a Action of type T. For my list of type Employee, it accepts an Action of type Employee.

Слайд 49


Action So let’s create an Action now. I have the following method which will calculate the age of the employee when the employee is passed in. static...
Описание слайда:
Action So let’s create an Action now. I have the following method which will calculate the age of the employee when the employee is passed in. static void CalculateAge(Employee emp) { emp.Age = DateTime.Now.Year - emp.Birthday.Year; } So I can create an Action, pointing to above method. Action empAction = new Action(CalculateAge); employees.ForEach(empAction); foreach (Employee e in employees) { Console.WriteLine(e.Age); } This will print me the calculated age for each employee. With the use of Lambda Expressions, I can eliminate writing a separate method for calculating the age and put it straight this way. employees.ForEach(e => e.Age = DateTime.Now.Year - e.Birthday.Year);

Слайд 50


Func Func series of delegates are pointers to methods which take zero, one or more input parameters, and return a value of the type specified by the...
Описание слайда:
Func Func series of delegates are pointers to methods which take zero, one or more input parameters, and return a value of the type specified by the TResult parameter. For this, let’s consider Enumerable.First method, which has an overloading method which accepts a Func. In my scenario, this particular method accepts Func which accepts an Employee and returns a bool value. For this, let’s create a method which I am going to point my Func to. Following method accepts an employee and checks whether his/her FirstName is equal to “Jaliya” and returns true or false. static bool NameIsEqual(Employee emp) { return emp.FirstName == "Jaliya"; } Now I can create aFunc myFunc = new Func(NameIsEqual); Console.WriteLine(employees.First(myFunc).FirstName); Again with the use of Lambda Expressions, I can make my code simple. Console.WriteLine(employees.First(e => e.FirstName == "Jaliya").FirstName); Func and get the first employee which satisfies the condition on Func.

Слайд 51


Predicate Predicate represents a method that defines a set of criteria and determines whether the specified object meets those criteria. For this,...
Описание слайда:
Predicate Predicate represents a method that defines a set of criteria and determines whether the specified object meets those criteria. For this, let’s consider List.Find Method which accepts a Predicate. In here it’s a Predicate of type Employee. So let’s create a method which accepts a Employee and check whether he/she is born in “1986”. If yes, it will return true or else false. static bool BornInNinteenEightySix(Employee emp) { return emp.Birthday.Year == 1986; } Now I am creating a Predicate pointing to above method. Predicate predicate = new Predicate(BornInNinteenEightySix); Console.WriteLine(employees.Find(predicate).FirstName); Again with the use of Lambda Expressions, I can simplify the code. Console.WriteLine(employees.Find(e => e.Birthday.Year == 1986).FirstName);

Слайд 52


Func Vs. Predicate Now you must be wondering what is the difference between Func and Predicate. Basically those are the same, but there is a one...
Описание слайда:
Func Vs. Predicate Now you must be wondering what is the difference between Func and Predicate. Basically those are the same, but there is a one significant difference. Predicate can only be used point to methods which will return bool. If the pointing method returning something other than a bool value, you can’t use predict. For that, you can use Func. Let’s take a look at following method. static string MyMethod(int i) { return "You entered: " + i; } The method accepts a integer value and returns a string. I can create the following Func and use it to call the above method. Func myFunc = new Func(MyMethod); Console.WriteLine(myFunc(3)); This will compile and print the desired output. But if you try to create a Predicate for this, you can’t.

Слайд 53


Questions?
Описание слайда:
Questions?



Похожие презентации
Mypresentation.ru
Загрузить презентацию