You are on page 1of 32

Chapter 4

Advanced C#
Module Objectives

1. Boxing/Unboxing

2. Generics

3. Collection

4. Framework Interfaces

5. Manipulating Strings
Boxing and Unboxing
• Boxing refers to implicit conversion of a value type into an
object type, or to any interface that it implements, e.g., int to
IComparable<int>.
• Further, the conversion of an underlying value type to a
nullable type is also known as boxing.
Unboxing

• Unboxing refers to an explicit conversion of object type to


non-nullable-value type or the conversion of an interface
type to a non-nullable-value type, e.g., IComparable<int> to
int.
• Further, the conversion of nullable type to the underlying
value type is also known as unboxing.
Generics Class
• Generics allow you to define a class with placeholders for the
type of its fields, methods, parameters, etc. Generics replace
these placeholders with some specific type at compile time.

1. Generic class: define by using “<>” angle brackets.


2. T is a generic type parameter; refers to any compile-time
type that is given when a class is instantiated.
3. In the same way, we can define generic structs and generic
interfaces, too.
Constraints on Generic Type Parameters
• There are 6 kinds of constraints that we can apply on generic
type parameters. The following table lists the kinds of
constraints.
Generic Methods
• Generic methods help to type-safe a method’s argument type,
which helps in calling a method’s parameter for multiple types.

• Constraints can also be applied on generic methods to restrict


the kinds of types used to pass values during method calling.
Example
Collection
• Collection helps to manage a group of related objects. In C#,
collections are data structures that provide a flexible way to
store and retrieve objects dynamically. Unlike arrays, a group of
objects in a collection can grow and shrink anytime.

• Collections are classes that are instantiated to manage a group


of related objects. In C#, there are three kind of collections:

1. System.Collections
2. System.Collections.Generic
3. System.Collections.Concurrent
System.Collections
ArrayList
• ArrayList is a non-generic type of collection in C#. It can
contain elements of any data types. It is similar to an array,
except that it grows automatically as you add items in it.
ArrayList
Hashtable
• Hashtable stores each element of a collection in a pair of
key/values.
Hashtable
Queue
• Queue represents a first-in, first out collection of object. It
is used when you need a first-in, first-out access of items.
When you add an item in the list, it is called enqueue, and
when you remove an item, it is called dequeue.
Queue
Stack
• Stack represents a last-in, first out collection of object. It is
used when you need a last-in, first-out access to items.
When you add an item in the list, it is called pushing the
item and when you remove it, it is called popping the item.
Stack
System.Collections.Generics
• System.Collections.Generics is a namespace which contains
classes and interfaces to manage a strongly-typed collection.
List<T>
• List<T> class in C# represents a strongly typed list of
objects. List<T> provides functionality to create a list of
objects, find list items, sort list, search list, and manipulate
list items. In List<T>, T is the type of objects.
List<T>
Dictionary<TKey, TValue>
• It’s a type-safe collection of key/value pairs. Each key in
dictionary must be unique and can store multiple values
against the same key. Dictionary<TKey, TValue> is much
faster than Hashtable.
Dictionary<TKey, TValue>
Queue<T>
• Queue<T> is a type-safe class. It stores and retrieves data in
FIFO (First In, First Out) order. In other words, it manages a
collection of data on a first come, first served basis. It is
much faster than Queue.
Queue<T>
Stack<T>
• Stack<T> is a class. It stores and retrieves elements in LIFO
(Last In, First Out) order. Elements pushed at the end will
pop up first, for example, a pile of plates. It is much faster
than Stack.
Stack<T>
Working with Strings
• Strings are one of most frequent use data types in C#.
Strings in C# are immutable means that once a string is
created, it cannot be changed. No characters can be added
or removed from it, nor can its length be changed.
• To solve this problem, C# introduced StringBuilder.
StringBuilder is a dynamic object that allows you to expand
the number of characters in the string.
StringBuilder
StringReader
• StringReader is a class of System.IO used to read lines from a
string. With StringReader, we can read a character with Read or
ReadAsync method, and an entire string with ReadToEnd or
ReadToEndAsync method. This type helps to access string data
through a stream-oriented interface.
StringWriter
• StringWriter is a class of System.IO. It is used to write to a
StringBuilder class. With StringWriter, we can write a
character/string with Write or WriteAsync method, and an entire
string line with WriteLine or WriteLineAsync method. It’s an
efficient way of using StringBuilder with StringWriter to
manipulate string.
PRG521
Main | CH4
Heading | Subheading

The End

2 0 2 32 0C2T0U CTTr U
a i nTirnagi nSi nogl u St ioolnust i o|n sA l |l RAi gl l hRt si gRhet s eRr ev es edr v |e dc t u| t rcat iuntirnagi n. ai nc g
. z. a c . z a

You might also like