C# 0 The Complete Reference


Specify the Underlying Type of an Enumeration



Download 4,07 Mb.
Pdf ko'rish
bet431/1096
Sana23.01.2022
Hajmi4,07 Mb.
#402171
1   ...   427   428   429   430   431   432   433   434   ...   1096
Bog'liq
C-Sharp 3 The Complete Reference Herbert Schildt

Specify the Underlying Type of an Enumeration

By default, enumerations are based on type 



int

, but you can create an enumeration of any 

integral type, except for type 

char

. To specify a type other than 



int

, put the desired type 

after the enumeration name, separated by a colon. For example, this statement makes 

Apple

 an enumeration based on 



byte

:

enum Apple : byte { Jonathan, GoldenDel, RedDel, Winesap,



                    Cortland, McIntosh };

Now


Apple.Winesap

, for example, is a 



byte

 quantity.



Use Enumerations

At first glance you might think that enumerations are an interesting but relatively 

unimportant part of C#, yet this is not the case. Enumerations are very useful when your 

program requires one or more specialized symbols. For example, imagine that you are 

writing a program that controls a conveyor belt in a factory. You might create a method 

called


Conveyor( )

 that accepts the following commands as parameters: start, stop, forward, 

and reverse. Instead of passing 

Conveyor( )

 integers, such as 1 for start, 2 for stop, and so 

on, which is error-prone, you can create an enumeration that assigns words to these values. 

Here is an example of this approach:

// Simulate a conveyor belt.

using System;

class ConveyorControl {

  // Enumerate the conveyor commands.

  public enum Action { Start, Stop, Forward, Reverse };

  public void Conveyor(Action com) {

    switch(com) {

      case Action.Start:

        Console.WriteLine("Starting conveyor.");

        break;

      case Action.Stop:

        Console.WriteLine("Stopping conveyor.");

        break;

www.freepdf-books.com




Download 4,07 Mb.

Do'stlaringiz bilan baham:
1   ...   427   428   429   430   431   432   433   434   ...   1096




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish