SlideShare une entreprise Scribd logo
1  sur  11
@ 2010 Tata McGraw-Hill Education
1
Education
Structures and EnumerationsStructures and Enumerations
@ 2010 Tata McGraw-Hill Education
2
Education
Introduction
We have already seen that C# supports two kinds of value types, namely,
predefined types and userdefined types. We have defined and used the
predefined data types such as int and double throughout our earlier
discussions. C# allows us to define our own complex value types (known as
userdefined value types) based on these simple data types. There are two
sorts of value types we can define in C#:
Structures
Enumerations
As we know, value type variables store their data on the stack and therefore
the structures and enumerations are stored on the stack. We shall discuss here
briefl y how these data types are defined and used in programming.
@ 2010 Tata McGraw-Hill Education
3
Education
STRUCTURES
Structures (often referred to as structs) are similar to classes in C#. Although classes
will be used to implement most objects, it is desirable to use structs whe re simple
composite data types are required. Because they are value types sto red on the stack,
they have the following advantages compared to class object s stored on the heap:
They are created much more quickly than heap-allocated types.
They are instantly and automatically deallocated once they go out of scope.
It is easy to copy value type variables on the stack.
The performance of programs may be enhanced by judicious use of structs.
Defining a Struct
A struct in C# provides a unique way of packing together data of different types. It is a
convenient tool for handling a group of logically related data items. It creates a template
that may be used to define its data properties
@ 2010 Tata McGraw-Hill Education
4
Education
Assigning Values to Members
Member variables can be accessed using the simple dot notation as follows:
s1.Name = “John”;
s1.RollNumber = 999 ;
s1.TotalMarks = 575.50 ;
We may also use the member variables in expressions on the right-hand side. Example:
FinalMarks = s1.TotalMarks + 5.0 ;
Copying Structs
We can also copy values from one struct to another. Example:
Student s2; // s2 is declared
s2 = s1 ;
@ 2010 Tata McGraw-Hill Education
5
Education
STRUCTS WITH METHODS
We have seen that values may be assigned to the data members using struct objects
and the dot operator. We can also assign values to the data members using what are
known as constructors. A constructor is a method which is used to set values of data
members at the time of declaration. Consider the code below:
sruct Number
{
int number; // data member
public Number ( int value ) // constructor
{
number = value;
}
}
@ 2010 Tata McGraw-Hill Education
6
Education
NESTED STRUCTS
C# permits declaration of structs nested inside other structs. The following code is valid:
struct Employee
{
public string name;
public int code;
public struct Salary
{
public double basic;
public double allowance;
}
}
@ 2010 Tata McGraw-Hill Education
7
Education
DIFFERENCES BETWEEN CLASSES AND STRUCTS
@ 2010 Tata McGraw-Hill Education
8
Education
ENUMERATIONS
An enumeration is a user-defined integer type which provides a way for attaching
names to numbers, thereby increasing the comprehensibility of the code. The enum
keyword automatically enumerates a list of words by assigning them values 0, 1, 2 and
so on. This facility provides an
alternative means of creating ‘constant’ variable names. The syntax of an enum
statement is illustrated
below:
enum Shape
{
Circle, //ends with comma
Square, //ends with comma
Triangle //no comma
}
@ 2010 Tata McGraw-Hill Education
9
Education
ENUMERATOR INITIALIZATION
As mentioned earlier, by default, the value of the fi rst enum member is set to 0, and
that of each subsequent member is incremented by one. However, we may assign
specific values for different members, if we so desire.
Example:
enum Colour
{
Red = 1,
Blue = 3,
Green = 7,
Yellow = 5
}
@ 2010 Tata McGraw-Hill Education
10
Education
ENUMERATOR BASE TYPES
By default, the type of an enum is int. However, we can declare explicitly a base type
for each enum. The valid base types are:
byte, sbyte, short, ushort, int, uint, long and ulong
Examples:
enum Position : byte
{
off,
on
}
enum Shape : long
{
Circle,
Square = 100,
Triangle
}
@ 2010 Tata McGraw-Hill Education
11
Education ENUMERATOR TYPE CONVERSION
Enum types can be converted to their base type and back again with an explicit
conversion using a cast.
Example:
enum Values
{
Value0,
Value1,
Value2,
Value3
}
. . . .
. . . .
Values u1 = (Values) 1;
int a = (int ) u1;
. . . .
. . . .
The exception to this is that the literal 0 can be converted to an enum type without a cast.
That is
Values u0 = 0;

Contenu connexe

Tendances

Tendances (20)

Union in c language
Union  in c languageUnion  in c language
Union in c language
 
C Structures and Unions
C Structures and UnionsC Structures and Unions
C Structures and Unions
 
Operator overloading
Operator overloadingOperator overloading
Operator overloading
 
Pointers in c++
Pointers in c++Pointers in c++
Pointers in c++
 
C# Arrays
C# ArraysC# Arrays
C# Arrays
 
Type casting
Type castingType casting
Type casting
 
Literals,variables,datatype in C#
Literals,variables,datatype in C#Literals,variables,datatype in C#
Literals,variables,datatype in C#
 
Constructor and Types of Constructors
Constructor and Types of ConstructorsConstructor and Types of Constructors
Constructor and Types of Constructors
 
Data Types - Premetive and Non Premetive
Data Types - Premetive and Non Premetive Data Types - Premetive and Non Premetive
Data Types - Premetive and Non Premetive
 
Unit 9. Structure and Unions
Unit 9. Structure and UnionsUnit 9. Structure and Unions
Unit 9. Structure and Unions
 
Data types in C
Data types in CData types in C
Data types in C
 
Structure in c
Structure in cStructure in c
Structure in c
 
Exception handling
Exception handlingException handling
Exception handling
 
Structure in C
Structure in CStructure in C
Structure in C
 
Presentation on c structures
Presentation on c   structures Presentation on c   structures
Presentation on c structures
 
Data types in c++
Data types in c++Data types in c++
Data types in c++
 
Constructors and Destructors
Constructors and DestructorsConstructors and Destructors
Constructors and Destructors
 
What is Dictionary In Python? Python Dictionary Tutorial | Edureka
What is Dictionary In Python? Python Dictionary Tutorial | EdurekaWhat is Dictionary In Python? Python Dictionary Tutorial | Edureka
What is Dictionary In Python? Python Dictionary Tutorial | Edureka
 
Destructors
DestructorsDestructors
Destructors
 
C# basics
 C# basics C# basics
C# basics
 

Similaire à Structure and Enum in c#

Chapter 13.1.9
Chapter 13.1.9Chapter 13.1.9
Chapter 13.1.9
patcha535
 
Chapter15 structure
Chapter15 structureChapter15 structure
Chapter15 structure
Deepak Singh
 
08. Object Oriented Database in DBMS
08. Object Oriented Database in DBMS08. Object Oriented Database in DBMS
08. Object Oriented Database in DBMS
koolkampus
 
C PROGRAMMING LANGUAGE
C  PROGRAMMING  LANGUAGEC  PROGRAMMING  LANGUAGE
C PROGRAMMING LANGUAGE
PRASANYA K
 

Similaire à Structure and Enum in c# (20)

Chapter 13.1.9
Chapter 13.1.9Chapter 13.1.9
Chapter 13.1.9
 
Structures in c++
Structures in c++Structures in c++
Structures in c++
 
Chapter4.pptx
Chapter4.pptxChapter4.pptx
Chapter4.pptx
 
Chapter15 structure
Chapter15 structureChapter15 structure
Chapter15 structure
 
Structures in c++
Structures in c++Structures in c++
Structures in c++
 
Lk module4 structures
Lk module4 structuresLk module4 structures
Lk module4 structures
 
Presentation on c programing satcture
Presentation on c programing satcture Presentation on c programing satcture
Presentation on c programing satcture
 
Chapter 8 Structure Part 2 (1).pptx
Chapter 8 Structure Part 2 (1).pptxChapter 8 Structure Part 2 (1).pptx
Chapter 8 Structure Part 2 (1).pptx
 
Learn C# Programming - Structure & Enums
Learn C# Programming - Structure & EnumsLearn C# Programming - Structure & Enums
Learn C# Programming - Structure & Enums
 
08. Object Oriented Database in DBMS
08. Object Oriented Database in DBMS08. Object Oriented Database in DBMS
08. Object Oriented Database in DBMS
 
Ch-3(b) - Variables and Data types in C++.pptx
Ch-3(b) - Variables and Data types in C++.pptxCh-3(b) - Variables and Data types in C++.pptx
Ch-3(b) - Variables and Data types in C++.pptx
 
User Defined Datatypes in C++ (Union, enum, class)
User Defined Datatypes in C++  (Union, enum, class)User Defined Datatypes in C++  (Union, enum, class)
User Defined Datatypes in C++ (Union, enum, class)
 
C PROGRAMMING LANGUAGE
C  PROGRAMMING  LANGUAGEC  PROGRAMMING  LANGUAGE
C PROGRAMMING LANGUAGE
 
Structures
StructuresStructures
Structures
 
Programming in C
Programming in CProgramming in C
Programming in C
 
Structures
StructuresStructures
Structures
 
Structures in c language
Structures in c languageStructures in c language
Structures in c language
 
Structures in c language
Structures in c languageStructures in c language
Structures in c language
 
Structured Languages
Structured LanguagesStructured Languages
Structured Languages
 
Concept Of C++ Data Types
Concept Of C++ Data TypesConcept Of C++ Data Types
Concept Of C++ Data Types
 

Plus de Prasanna Kumar SM

Plus de Prasanna Kumar SM (7)

C# Introduction brief
C# Introduction briefC# Introduction brief
C# Introduction brief
 
Overview of c#
Overview of c#Overview of c#
Overview of c#
 
Operators and Expressions in C#
Operators and Expressions in C#Operators and Expressions in C#
Operators and Expressions in C#
 
Methods in C#
Methods in C#Methods in C#
Methods in C#
 
Decision making and loop in C#
Decision making and loop in C#Decision making and loop in C#
Decision making and loop in C#
 
Characteristics of c#
Characteristics of c#Characteristics of c#
Characteristics of c#
 
C# intro
C# introC# intro
C# intro
 

Dernier

Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
ZurliaSoop
 

Dernier (20)

Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - English
 
How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024
 
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxHMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
 
REMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxREMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptx
 
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfUnit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptx
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 

Structure and Enum in c#

  • 1. @ 2010 Tata McGraw-Hill Education 1 Education Structures and EnumerationsStructures and Enumerations
  • 2. @ 2010 Tata McGraw-Hill Education 2 Education Introduction We have already seen that C# supports two kinds of value types, namely, predefined types and userdefined types. We have defined and used the predefined data types such as int and double throughout our earlier discussions. C# allows us to define our own complex value types (known as userdefined value types) based on these simple data types. There are two sorts of value types we can define in C#: Structures Enumerations As we know, value type variables store their data on the stack and therefore the structures and enumerations are stored on the stack. We shall discuss here briefl y how these data types are defined and used in programming.
  • 3. @ 2010 Tata McGraw-Hill Education 3 Education STRUCTURES Structures (often referred to as structs) are similar to classes in C#. Although classes will be used to implement most objects, it is desirable to use structs whe re simple composite data types are required. Because they are value types sto red on the stack, they have the following advantages compared to class object s stored on the heap: They are created much more quickly than heap-allocated types. They are instantly and automatically deallocated once they go out of scope. It is easy to copy value type variables on the stack. The performance of programs may be enhanced by judicious use of structs. Defining a Struct A struct in C# provides a unique way of packing together data of different types. It is a convenient tool for handling a group of logically related data items. It creates a template that may be used to define its data properties
  • 4. @ 2010 Tata McGraw-Hill Education 4 Education Assigning Values to Members Member variables can be accessed using the simple dot notation as follows: s1.Name = “John”; s1.RollNumber = 999 ; s1.TotalMarks = 575.50 ; We may also use the member variables in expressions on the right-hand side. Example: FinalMarks = s1.TotalMarks + 5.0 ; Copying Structs We can also copy values from one struct to another. Example: Student s2; // s2 is declared s2 = s1 ;
  • 5. @ 2010 Tata McGraw-Hill Education 5 Education STRUCTS WITH METHODS We have seen that values may be assigned to the data members using struct objects and the dot operator. We can also assign values to the data members using what are known as constructors. A constructor is a method which is used to set values of data members at the time of declaration. Consider the code below: sruct Number { int number; // data member public Number ( int value ) // constructor { number = value; } }
  • 6. @ 2010 Tata McGraw-Hill Education 6 Education NESTED STRUCTS C# permits declaration of structs nested inside other structs. The following code is valid: struct Employee { public string name; public int code; public struct Salary { public double basic; public double allowance; } }
  • 7. @ 2010 Tata McGraw-Hill Education 7 Education DIFFERENCES BETWEEN CLASSES AND STRUCTS
  • 8. @ 2010 Tata McGraw-Hill Education 8 Education ENUMERATIONS An enumeration is a user-defined integer type which provides a way for attaching names to numbers, thereby increasing the comprehensibility of the code. The enum keyword automatically enumerates a list of words by assigning them values 0, 1, 2 and so on. This facility provides an alternative means of creating ‘constant’ variable names. The syntax of an enum statement is illustrated below: enum Shape { Circle, //ends with comma Square, //ends with comma Triangle //no comma }
  • 9. @ 2010 Tata McGraw-Hill Education 9 Education ENUMERATOR INITIALIZATION As mentioned earlier, by default, the value of the fi rst enum member is set to 0, and that of each subsequent member is incremented by one. However, we may assign specific values for different members, if we so desire. Example: enum Colour { Red = 1, Blue = 3, Green = 7, Yellow = 5 }
  • 10. @ 2010 Tata McGraw-Hill Education 10 Education ENUMERATOR BASE TYPES By default, the type of an enum is int. However, we can declare explicitly a base type for each enum. The valid base types are: byte, sbyte, short, ushort, int, uint, long and ulong Examples: enum Position : byte { off, on } enum Shape : long { Circle, Square = 100, Triangle }
  • 11. @ 2010 Tata McGraw-Hill Education 11 Education ENUMERATOR TYPE CONVERSION Enum types can be converted to their base type and back again with an explicit conversion using a cast. Example: enum Values { Value0, Value1, Value2, Value3 } . . . . . . . . Values u1 = (Values) 1; int a = (int ) u1; . . . . . . . . The exception to this is that the literal 0 can be converted to an enum type without a cast. That is Values u0 = 0;