Structure and Enumeration are used as value type in c#.Value type variables store their data on the stack memory.An enumeration is a set of named integer constants. An enumerated type is declared using the enum keyword. C# enumerations are value data type. In other words, enumeration contains its own values and cannot inherit or cannot pass inheritance.
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;