0% found this document useful (0 votes)
96 views13 pages

Introducing Delegate' To Cover Event.: - Declare Delegate - Bind Method(s) To The Delegate - Invoke

The document introduces various C# keywords used to modify classes, methods, and fields. It provides examples of how each keyword is used, including delegate for encapsulating method references, event for defining events and handlers, extern for external methods, override for overriding base methods, readonly for immutable fields, sealed to prevent inheritance, static for type members, unsafe for pointers, virtual for polymorphism, and volatile for thread safety. It demonstrates the keywords in code snippets and notes their usage in C# versus Visual Basic.

Uploaded by

ANKIT KUMAR
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
96 views13 pages

Introducing Delegate' To Cover Event.: - Declare Delegate - Bind Method(s) To The Delegate - Invoke

The document introduces various C# keywords used to modify classes, methods, and fields. It provides examples of how each keyword is used, including delegate for encapsulating method references, event for defining events and handlers, extern for external methods, override for overriding base methods, readonly for immutable fields, sealed to prevent inheritance, static for type members, unsafe for pointers, virtual for polymorphism, and volatile for thread safety. It demonstrates the keywords in code snippets and notes their usage in C# versus Visual Basic.

Uploaded by

ANKIT KUMAR
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

Introducing ‘delegate’ to cover event.

abstract
delegate (C#.Net) Delegate (VB.Net)
const
event
A delegate in C# is similar to a function pointer in C or C++.
Using a delegate allows the programmer to encapsulate a reference to a method inside a delegate
extern
object.
override
The delegate object can then be passed to code which can call the referenced method, without having to
readonly know at compile time which method will be invoked.
sealed Unlike function pointers in C or C++, delegates are object-oriented, type-safe, and secure.
static An interesting and useful property of a delegate is that it does not know or care about the class of the
unsafe object that it references. Any object will do; all that matters is that the method's argument types and
virtual
return type match the delegate's. This makes delegates perfectly suited for "anonymous" invocation.
volatile

• Declare Delegate DEMO

1
Note: Delegates
• Bind method(s) to the delegate run under the
2 caller's security
permissions, not
• Invoke the declarer's
permissions.
3
Introducing ‘delegate’ to cover event.
abstract
delegate (C#.Net) Delegate (VB.Net)
const
event
extern
By default, a delegate is a
override
readonly
System.MulticastDelegate
sealed
static
unsafe
virtual
volatile

DEMO
Introducing ‘delegate’ to cover event.
abstract
delegate (C#.Net) Delegate (VB.Net)
const
event
extern
override DEMO
readonly
sealed
static
unsafe
Removing the methods
virtual
from the delegate using
volatile decrementing operator.
event
abstract
event (C#.Net) Event (VB.Net)
const
event
The event keyword lets you specify a delegate that will be called upon the occurrence of some
"event" in your code. The delegate can have one or more associated methods that will be called
extern
when your code indicates that the event has occurred. An event in one program can be made
override
available to other programs that target the .NET Framework common language runtime.
readonly The following steps must be taken in order to create and use C# events:
sealed
static • Create or Identify a delegate
DEMO
unsafe
virtual
1
volatile • Define an event in a class from the delegate
2
• Define a method that will raise the event

3
• Define method(s) to handle the event
4
• Associate method(s) (event handler) to the event
5
event
abstract
C#
const
event
extern
override DEMO
readonly
sealed
static
unsafe
virtual
volatile
extern
abstract
extern (C#.Net)
const
event
Indicate that the method is implemented externally. Use the extern modifier in a method declaration
to indicate that the method is implemented externally. A common use of the extern modifier is with the
extern
DllImport attribute.
override
readonly abstract and extern modifiers cannot be used together to modify the same member. Using the extern
sealed modifier means that the method is implemented outside the C# code, while using the abstract modifier
static means that the method implementation is not provided in the class.
unsafe
virtual
C# DEMO
volatile

Because an external method declaration


provides no actual implementation,
there is no method body.
override
abstract
override (C#.Net)
const
event
Use the override modifier to modify a method, a property, an indexer, or an event.
An override method provides a new implementation of a virtual member inherited from a base class.
extern
The method overridden by an override declaration is known as the overridden base method.
override
The overridden base method must have the same signature as the override method.
readonly
sealed C#
static
unsafe
virtual
volatile DEM
O
readonly
abstract
readonly (C#.Net)
const
event
The readonly keyword is a modifier that you can use on fields.
When a field declaration includes a readonly modifier, assignments to the fields introduced by the
extern
declaration can only occur as part of the declaration or in a constructor in the same class.
override
readonly
C#
sealed
static
DEMO
unsafe
virtual
volatile
sealed
abstract
sealed (C#.Net)
const
event
A sealed class cannot be inherited.
It is an error to use a sealed class as a base class.
extern
Use the sealed modifier in a class declaration to prevent inheritance of the class.
override
It is not permitted to use the abstract modifier with a sealed class.
readonly Structs are implicitly sealed; therefore, they cannot be inherited.
sealed
static C#
unsafe
virtual
volatile DEMO
static
abstract
static (C#.Net) Shared (VB.Net)
const
event
Use the static modifier to declare a static member, which belongs to the type itself rather than to a specific
object.
extern
The static modifier can be used with fields, methods, properties, operators, events and constructors, but
override
cannot be used with indexers and destructors.
readonly
sealed
C#
static
unsafe
virtual
volatile

DEMO
unsafe
abstract
unsafe (C#.Net) Not supported in VB.Net
const
event
The unsafe keyword denotes an unsafe context, which is required for any operation involving pointers.
To compile unsafe code, you must specify the /unsafe compiler option. From IDE, from project properties
extern
check Build -> “Allow unsafe code” checkbox.
override
Unsafe code is not verifiable by the common language runtime.
readonly You can use the unsafe modifier in the declaration of a type or a member.
sealed
C#
static
unsafe DEMO
virtual
volatile
virtual
abstract
virtual (C#.Net) (VB.Net)
const
event
Declare a method or an accessor whose implementation can be changed by an overriding member in a
derived class.
extern
override C#
readonly
sealed
static DEMO
unsafe
virtual
volatile
volatile
abstract
volatile (C#.Net) Not supported in VB.Net
const
event
Indicate that a field can be modified in the program by something such as the operating system, the
hardware, or a concurrently executing thread.
extern
The volatile modifier is usually used for a field that is accessed by multiple threads without using the lock
override
statement to serialize access.
readonly Using the volatile modifier ensures that one thread retrieves the most up-to-date value written by
sealed another thread.
static
unsafe C#
virtual
volatile

DEMO

You might also like