The document discusses key concepts in .NET including value types, reference types, classes, inheritance, interfaces, generics, boxing and unboxing, exceptions, events and delegates.
It explains that value types like integers store data directly while reference types store references to data on the heap. Common reference types include strings, arrays and streams. Classes can inherit from other classes and implement interfaces. Generics allow types to be parameterized. Events and delegates are used for asynchronous notification. Explicit conversion is needed between non-compatible types.
1 of 52
More Related Content
70 536
1. An extract from the book Microsoft’s MCTS Exam 70-536 Chapter -1
2. We are going to focus on
Data
Interfaces
Types
Events &
Delegates
3. 1
• Using Value Types
2
• Using Common Reference Types
3
• Constructing Classes
4
• Converting Between Types
4. • Using Value Types
1
Simplest type in .NET framework
Variables that contain their data Directly.
Stored in area of memory called Stack.
Runtime can Create, Read, Update & Delete
Easily from Stack.
Stack
A=1
B = True
5. Value type holds it’s own data
When copied the value is copied and stored
separately in stack
Basically considered as an object
So Methods can be applied like Tostring
7. These types are provided with the .Net
framework
All Numeric Values are value type
Numeric type is selected according to
precision we need
There are 1,2,4,8,16,32 bytes are used
1 Byte = 256 values i.e. = 0 to 255
2 byte = 256 *256 = 65536
If signed then 65536/2 = 32768 i.e. -32768 to 32767
8. Type Byte
Byte , Sbyte 1
Int16 2
Int32,Uint, Single 4
Int64, Double 8
Decimal (Most Efficient for Doubles) 16
Optimized by Hardware
9. Some commonly used Value types
Char - 2 byte
Boolean - 4 byte
Intptr - Platform dependent
DateTime - 8 byte
There is more than 300 value types available
in framework
10. Declaring Value Type
Value types have an implicit constructors
So, No need to use NEW keyword.
Constructor assigns a default value but you
should Explicitly assign a value during declartion.
▪ E.g. Dim a as Boolean = false
.NET 2.0 has a new type called Nullable.
▪ E.g. Dim a as Nullable(as Boolean)
▪ Enables the HasValue and Value Members
Only Value types can be Nullable
11. Is also called as Structure or Structs
Language keyword should be used to create
VB
Structure <Name>
C#
Struct <Name>
12. Structures is more efficient than a classes
when these conditions are met
Logically represents one value
Has an instance less than 16 byte
Will not be cast to a Reference type
Will not be Changed after Creation.
13. Enumerations are related symbols that have
fixed value.
Vb
Enum <Name> as Integer
C#
Enum <Name>
Int{}
14. .NET Framework includes a large number of
built-in types. Can be used directly or used for
custom built
Value types directly contain their
data, offering excellent performance
In .NET all value types are 16bytes or shorter
15. Can create User defined types that stores
multiple values and methods.
Large portion of Application logic will be
stored in user defined types.
Enumerations improve Code readability by
providing symbols for a set of values
16. • Using Common Reference Types
2
Most types are Reference types
Great Flexibility
Excellent performance when passing it to
methods
17. Reference type stores the address of the data
called Pointer.
Data is stored in area of memory called Heap.
When copied only the address is copied.
Runtime manages the heap using Garbage
Collection (GC).
GC periodically recovers the memory by
disposing unreferenced values
GC can be triggered by calling GC.Collect
18. Reference Type Value Type
Derives from System.Object Derives from System.Object
Classes Structures
Holds the address of the data Holds the data
Stored in Heap Stored in Stack
Any change reflects in the original data Any change reflects in its own data.
When copied, address is copied When Copied, Data is copied
19. More than 2500 Types
Everything which is not Value type is
Reference type
Commonly used
String, Object, Array, exceptions, IO.Stream, Text.
StringBuilder
20. Strings are Immutable – Any change will
cause runtime to drop the old one and create
a new one.
Use String builder, to reduce the GC call
Which in turn improves performance
String Class overrides operators
Types are more than just containers , used to
manipulate the data thru members
21. Arrays are mechanisms that allows you to
treat several items as a single collection
VB uses {} : C# uses []
22. Commonly used type to read , write &
communicating thru Networks
FileStream
Memory stream
StreamReader
StreamWriter
System.IO.Stream is the most common.
Network streams – Network.sockets
Encryption streams – Security.Cryptography
23. Exceptions are the unexpected events that
interrupt normal execution of code
Derived from System.Exception
Can create own exceptions from
Application.Exception
Plan for exceptions and catch them ,respond
them
24. Exceptions should be from most specific to
most General – Filtering Exceptions
Different types of exceptions are provided
None of the variables declared inside the try
block is accessible in finally block.
Incurs Slight performance penalty
25. Reference type contains the address rather
than actual data.
Strings are immutable. Use Stringbuilder to
build strings dynamically
Use streams to read, write and communicate
thru networks.
26. • Constructing Classes
3
A container for data and code.
The data within the class can be accessed
with properties. The code is referred to as
methods.
Simplest applications require constructing
one or more custom classes. Each with
multiple methods and properties
27. The key feature of the inheritance is using all
the functions of a base class with adding
some more functions without rewriting the
class
Helps to maintain the consistency between
classes
Gives the benefits of interchangeable
28. Interface is a Contract between classes
Common set of methods for the class that
implements
Commonly used interface
•IDisposable •ICloneable
•IComparable •IFormattable
•IConvertible •IEquatable
29. A Class can implement multiple interface
Visual Studio automatically creates the
declarations
Visual studio creates the Interface from an
exiting class using Refractor feature
Consistency between classes
30. Splitting the class definition across many
source files.
31. Is a type system that allows to define a type
while leaving some details undefined.
Advantages
Reduced run time errors
Improved Performance
Limitation
Generics code is valid only if it will compile for
every possible constructed instance.
32. Generics will be limited if you could only write
code to compile for any class, because it will
be limited to system.object capabilities
Constrains come in picture to overcome.
VB
Class Example(of T as Icomparable)
C#
Class Example<T>
Where T : Icomparable
33. Types of Generic Constrains
• Allows only type that implement
Interface specific interfaces
• Allows only type that match or inherit
Base Class the specific base class
• Require types that use your generic to
Constructor implement a parameter less constructor
Reference or Value • Type to be either reference or value
Type type
34. Message sent by an object to signal the
occurrence of an action
Event sender
Object which raises the event
Does not know which object or method will
receive & respond
Event Receiver
Captures and responds the event
Event arguments contains the event data
35. Delegate is a class that can hold reference of a
method.
The intermediate between the event sender and
event receiver
It is a Function pointer.
Has a signature.
Holds the reference to the matching signature
Type safe
36. Delegate declaration is sufficient to define delegate
class
Delegate supplies signature CLR provides
Implementation
Event handler is a pre-defined delegate
Does not contain event data
To hold event data custom handlers can be created
To associate the event with method just add an
instance of the delegate
37. Responding an Receiving an
Event Event
Create an event to Create a delegate
respond to event
Create Event
Member
Adding the event
handler to indicate Invoke the delegate
the method within a method
38. Describe a type, method or property can be
programmatically queried using Reflection.
Attributes more than describing declares the
requirements and capabilities
Visual studio automatically creates standard
attributes like project name, company,
version etc.,
Need to update as required.
VB <>
C# []
39. Specify which security privileges a class
requires
Specify security privileges to refuse security
risk
Declare capabilities such as serialization
Describe the assembly by providing the title,
description and copyright Notice
40. Type forward to is an attribute allows you to
move a type from one assembly to another
Not Necessary to recompile clients that use the
assembly
C#
[assembly:TypeForwardedToAttribute(typeof(Example))]
You can forward a type to an assembly authored
in any language targeting the common language
runtime.
41. Examples of types that can be forwarded include:
ref classes
value classes
enums
interfaces
You cannot forward the following types:
Generic types
Native types
Nested types (if you want to forward a nested type, you
should forward the enclosing type)
42. Use Inheritance to create a new types from
Existing types
Use Interfaces to define a common set of
members that must be implemented by
related types
Partial Classes split a class definition across
multiple source files
43. Events allow you to run a specified method
when something occurs in a different part of
the code
Use attributes to describe assemblies, types
& members.
Use the Typeforwardedto attribute for
moving a type from one class library to
another.
44. • Converting Between Types
4
Often you need to convert between types
E.g.
▪ From Integer to Double
▪ From number to string etc.,
This is one of the area where C# and VB.NET
works differently
VB.NET allows implicit conversion; C# prohibits
By adding option strict on implicit conversion
can be turned off
45. VB.NET and C# allows implicit conversion if the
destination type can accommodate all possible
values of source type. Is called Widening
Conversion.
E.g. Converting from int to double
Narrowing conversion usually requires Explicit
conversion.
System.Convert, Type.Tostring
,Type.Parse, Type.ParseExact
Will fail if source value exceeds destination value
46. Boxing Value to Reference type
Unboxing Reference type to Value type
Both incurs Overheads. Avoid using for
repetitive tasks.
Boxing when we call virtual methods.
47. Conversion Operators
• To simplify narrowing and widening conversion
Override Tostring, Parse
• Tostring – Conversion to string
• Parse – Conversion from String
Implement systm.Iconvertible
• To enable system.convert
• Use this for culture-specific Conversions
Implement Typeconverter Class
• To enable design time conversion
48. Boxing
The conversion of a value type instance to an Reference type
Unboxing
Value type to reference type
Cast
A conversion from 1 type to another
Constrain
A condition on type parameter that restricts the type argument you
can supply for it.
Contract
Interface
49. Exception
Unexpected events that interrupt normal execution.
Filtering exceptions
Process of catching exceptions from most specific to most
general exceptional type
Garbage Collection
Recovery of memory heap through removal of dereferenced
items
Generic Type
A container to hold different data type with same functionality
50. Heap
Area of memory where reference types are stored
Interface
Defines a common set of members that all classes that
implement the interface must apply
Narrowing
Converting a value when destination type cannot hold all
data of source type Explicit conversion .
Nullable type
A value type that can be set to Null
51. Signature
The return type, parameter count and parameter types of
a member
Stack
Area of memory where value types are stored
Structure
A user-defined value types made up of other types
Widening
Conversion of a type where destination type can hold all
possible data from source type. Implicit Conversion
52. Value Type
• Nullable
• Stack
• Structures
Reference type
• Heap
• String is immutable
Classes
• Attributes
• Inheritance
• Interface
• Generics
Conversion
• Implicit Conversion
• Explicit Conversion