Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 21

Chapter 2

Types in C#
Chapter Objectives

1.Understand Types

2.Create Types

3.Types and Memory Management

4.Special Types in C#

5.Type Conversion
Understand Types

• Types are the declaration of an object which stores


information and actions, that an object uses to produce
required results. Type also stores the following information:

•How much memory an object holds


•Memory location where object is stored in memory
•The base type it inherits from

• This information helps the compiler to make sure everything


is type safe. In the previous chapter, we learned how to
create variables by using common built-in types, i.e., int,
float, and bool.
Create Types

• C# allow users to create their own types by using:


1.Enum
2.Struct
3.Class

• Enum, a.k.a enumeration, is a set of named integer


constants. It is used to group similar logically named
constants (for example, days of the week and rainbow
colors, etc.).
Struct

• Struct is used to encapsulate the attribute and behaviour of


an entity. It’s used to define those objects which hold small
memory. Most primitive types (int, float, bool) in C# are
made up from struct. Struct doesn’t support all object-
oriented principles.

• Syntax

• struct is a keyword, used to declare a type.


Constructor in struct

Constructor is a method called and executed first by runtime


soon after its type’s instance is created on memory. It doesn’t
have a return type. It is used to initialize data members that
protect the application from any garbage computation error.

•Default Constructor (parameter less) is not allowed in struct.


•Constructor is optional in struct but if included it must not be
parameter less.
•Constructor can be overload but each overloaded constructor
must initialize all data members.
•Data members or fields cannot be initialized in the struct
body. Use constructor to initialize them.
•Creating the object (without a new keyword) would not cause
constructor calling even though a constructor is present.
this keyword

• this keyword indicates current instance. It is a special


reference type variable that is used to call an
instance’s member inside non-static method
definition.

this keyword has many uses:


• To pass an object itself as a parameter to other
methods.
• To return an object itself from a method.
• To declare an extension method.
• To eliminate the naming conflict of a parameter’s
variable name and instance field name.
Class

• Class is used to encapsulate the attribute and behavior


of an entity. It supports object-oriented principles.
Therefore, classes are helpful to define complex types.
Types and Memory Management
Value Type
A type that is defined by either struct or enum is known as a value type. It
holds data in its own memory allocation.

Reference Type
A type that is defined by either class, interface, or delegate is known as a
reference type. It holds a pointer to a memory location that contains data
called reference type.

In a .NET framework, Common Language Runtime(CLR) manages instances of


value and reference type on three memory locations:
1.Heap: reference type are stored
2.Stack: value type and the memory address of an object can be stored
3.Registers: value type or computation values of arithmetic operations are
stored
Special Types in C#
• C# provides special types which are syntactic sugar for users.
These types help users to maximize their productivity by
writing helper code inside them. These special types are listed
below.

• System.Object type
• anonymous type
• dynamic type
• nullable type
• static type
System.Object Type

• All value and reference types are derived from


system.object type. In .NET, object is the base of all type
hierarchy. The below Figure shows system.object’s type
hierarchy.

• Because all types in .NET are derived from system.object, it


can handle values of all types.
Anonymous Type

• Types that don’t have names are called anonymous types.


They are used to group temporary data into read-only
properties. Unlike class, anonymous types don’t have a
blueprint to define property types. Therefore, each property
must have a data to determine its property type.

• Anonymous types are created by using a new operator with


object initializer. The implicit type variable var is used to hold
the reference of anonymous types.
Anonymous Type
Dynamic Type

• Dynamic type is used to store and manipulate any data


whose types definition and operation errors are determined
at runtime. It ignores compile-time checks.
Nullable Type

• Normally, value type cannot be assigned with null value. But


with nullable type, value type can be assigned with null
value.
• Value type can become nullable type by using “?”.
Static Type

• Unlike normal classes, static classes cannot be instantiated.


They're useful to define helper static methods.
• Static class doesn’t have any instance member. All members
must be static in static class.
• Therefore, members of static classes can access by using the
class name itself.
• Static class is defined by writing a static keyword before the
class definition.

• Unlike normal class, static class doesn’t contain a public


instance constructor. It contains a private static constructor
to initialize static members. It is called automatically before
the first instance is created or any static members are
referenced.
Static Type
Extension Methods

• Extension methods are special static methods. They inject


addition methods without changing, deriving, or recompiling
the original type. They are always called as if they were
instance method.

• Extension methods are always defined inside static class.

• The first parameter of extension method must have “this”


operator, which tells on whose instance this extension
method should give access.

• The extension method should be defined in the same


namespace in which it is used, or import the namespace in
which the extension method was defined.
Extension Methods
Code Challenges

• Develop Temperature Converter Application

Application has two classes: FahrenheitTemperature and


CelsiusTemperature. FahrenheitTemperature stores
temperature in Fahrenheit and CelsiusTemperature stores
temperature in Celsius. You have to define conversion methods
in both classes to convert Fahrenheit to Celsius Implicitly and
vice versa.
PRG521
Main | CH2
Heading | Subheading

The End

2 0 2 02 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