青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品

C++ Programmer's Cookbook

{C++ 基礎} {C++ 高級} {C#界面,C++核心算法} {設計模式} {C#基礎}

Enums and Structs in C#

Introduction

Just about everything is a heap object when you are using C#. Only elementary native types like int are treated as value types. But there are two value types in C# that are pretty much more useful that first glances would tell you. They are the enum and struct types. Very few tutorials even cover these topics, but they have their own uses. And both of them are a lot more efficient than classes and you can use them in place of classes when they meet your requirements to improve performance.

Enums

Enums are basically a set of named constants. They are declared in C# using the enum keyword. Every enum type automatically derives from System.Enum and thus we can use System.Enum methods on our Enums. Enums are value types and are created on the stack and not on the heap. You don't have to use new to create an enum type. Declaring an enum is a little like setting the members of an array as shown below.

enum Rating {Poor, Average, Okay, Good, Excellent}

You can pass enums to member functions just as if they were normal objects. And you can perform arithmetic on enums too. For example we can write two functions, one to increment our  enum and the other to decrement our enum.

Rating IncrementRating(Rating r)
{
    if(r == Rating.Excellent)
        return r;
    else
        return r+1;
}
Rating DecrementRating(Rating r)
{
    if(r == Rating.Poor)
        return r;
    else
        return r-1;
}

Both functions take a Rating object as argument and return back a Rating object. Now we can simply call these functions from elsewhere.

for (Rating r1 = Rating.Poor; 
    r1 < Rating.Excellent ; 
    r1 = IncrementRating(r1))
{           
    Console.WriteLine(r1);
}

Console.WriteLine();

for (Rating r2 = Rating.Excellent; 
    r2 > Rating.Poor; 
    r2 = DecrementRating(r2))
{
    Console.WriteLine(r2);          
}

And here is a sample code snippet showing how you can call System.Enum methods on our Enum object. We call the GetNames method which retrieves an array of the names of the constants in the enumeration.

foreach(string s in Rating.GetNames(typeof(Rating)))
    Console.WriteLine(s);

Where to use enums

Quite often we have situations where a class method takes as an argument a custom option. Let's say we have some kind of file access class and there is a file open method that has a parameter that might be one of read-mode, write-mode, read-write-mode, create-mode and append-mode. Now you might think of adding five static member fields to your class for these modes. Wrong approach! Declare and use an enumeration which is a whole lot more efficient and is better programming practice in my opinion.

Structs

In C++ a struct is just about the same as a class for all purposes except in the default access modifier for methods. In C# a struct are a pale puny version of a class. I am not sure why this was done so, but perhaps they decided to have a clear distinction between structs and classes. Here are some of the drastic areas where classes and structs differ in functionality.

  • structs are stack objects and however much you try you cannot create them on the heap
  • structs cannot inherit from other structs though they can derive from interfaces
  • You cannot declare a default constructor for a struct, your constructors must have parameters
  • The constructor is called only if you create your struct using new, if you simply declare the struct just as in  declaring a native type like int, you must explicitly set each member's value before you can use the struct
struct Student : IGrade
{   
    public int maths;
    public int english;
    public int csharp;

    //public member function
    public int GetTot()
    {
        return maths+english+csharp;
    }

    //We have a constructor that takes an int as argument
    public Student(int y)
    {
        maths = english = csharp = y;
    }

    //This method is implemented because we derive
    //from the IGrade interface
    public string GetGrade()
    {
        if(GetTot() > 240 )
            return "Brilliant";
        if(GetTot() > 140 )
            return "Passed";
        return "Failed";
    }
}

interface IGrade
{
    string GetGrade();
}

Well, now let's take a look at how we can use our struct.

Student s1 = new Student();
Console.WriteLine(s1.GetTot());
Console.WriteLine(s1.GetGrade());

//Output
0
Failed

Here the default constructor gets called. This is automatically implemented for us and we cannot have our own default parameter-less constructor. The default parameter-less constructor simply initializes all values to their zero-equivalents. This is why we get a 0 as the total.

Student s2;
s2.maths = s2.english = s2.csharp = 50;
Console.WriteLine(s2.GetTot());
Console.WriteLine(s2.GetGrade());

//Output
150
Passed

Because we haven't used new, the constructor does not get called. Of all the silly features this one must win the annual contest by a long way. I see no sane reason why this must be so. Anyway you have to initialize all the member fields. If you comment out the line that does the initialization you will get a compiler error :- Use of unassigned local variable 's2'

Student s3 = new Student(90);
Console.WriteLine(s3.GetTot());
Console.WriteLine(s3.GetGrade());

//Output
270
Brilliant

This time we use our custom constructor that takes an int as argument.

When to use structs

Because structs are value types they would be easier to handle and more efficient that classes. When you find that you are using a class mostly for storing a set of values, you must replace those classes with structs. When you declare arrays of structs because they are created on the heap, efficiency again improves. Because if they were classes each class object would need to have memory allocated on the heap and their references would be stored. In fact lots of classes within the .NET framework are actually structs. For example System.Drawing.Point is actually a struct and not a class.

posted on 2006-03-14 11:27 夢在天涯 閱讀(787) 評論(0)  編輯 收藏 引用 所屬分類: C#/.NET

公告

EMail:itech001#126.com

導航

統計

  • 隨筆 - 461
  • 文章 - 4
  • 評論 - 746
  • 引用 - 0

常用鏈接

隨筆分類

隨筆檔案

收藏夾

Blogs

c#(csharp)

C++(cpp)

Enlish

Forums(bbs)

My self

Often go

Useful Webs

Xml/Uml/html

搜索

  •  

積分與排名

  • 積分 - 1816882
  • 排名 - 5

最新評論

閱讀排行榜

青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品
  • <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
      <noscript id="pjuwb"></noscript>
            <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
              <dd id="pjuwb"></dd>
              <abbr id="pjuwb"></abbr>
              亚洲国产美国国产综合一区二区| 欧美国产三区| 亚洲综合日韩在线| 国产日产精品一区二区三区四区的观看方式| 一区二区三区欧美| 一本一本久久a久久精品牛牛影视| 欧美日韩亚洲天堂| 亚洲欧美一区二区三区极速播放| 亚洲免费视频中文字幕| 国产综合精品| 亚洲欧洲精品天堂一级| 国产精品国产三级国产专播精品人 | 亚洲国产高清在线| 亚洲国产精品毛片| 国产精品成人va在线观看| 午夜亚洲性色福利视频| 久久久爽爽爽美女图片| 99精品国产99久久久久久福利| 亚洲视频免费观看| 尤物精品在线| 亚洲午夜激情免费视频| 亚洲福利久久| 亚洲一区二区三区高清| 激情综合色综合久久综合| 99爱精品视频| 亚洲成人自拍视频| 亚洲一区二区三区久久| 亚洲欧洲精品一区二区三区 | 午夜一级在线看亚洲| 久久久久国产精品一区三寸| 夜夜爽夜夜爽精品视频| 久久av一区二区三区| 亚洲天堂成人| 欧美成人精品在线观看| 久久精品国产亚洲一区二区| 欧美极品一区二区三区| 久久亚洲春色中文字幕| 欧美视频二区| 亚洲经典视频在线观看| 国内精品久久久久久影视8 | 亚洲视频高清| 99综合在线| 久久综合福利| 久久人人97超碰精品888| 国产精品成人在线观看| 亚洲国产经典视频| 好吊一区二区三区| 羞羞漫画18久久大片| 亚洲一区制服诱惑| 欧美激情中文字幕一区二区| 你懂的国产精品| 国产精品资源| 亚洲视频福利| 亚洲美女av黄| 99re66热这里只有精品3直播| 久久久精品tv| 久久一区二区三区四区| 国产亚洲视频在线| 亚欧成人精品| 久久国产日韩欧美| 国产视频一区在线观看| 亚洲专区一二三| 午夜精品视频网站| 国产精品呻吟| 亚洲欧美久久久久一区二区三区| 亚洲一区视频| 国产精品日韩在线一区| 亚洲一区二区网站| 久久国产一区二区| 海角社区69精品视频| 久久国产加勒比精品无码| 久久久国产精品一区二区中文 | 国产精品vip| 亚洲图片欧美午夜| 欧美一区免费视频| 国产三级欧美三级| 久久久久免费| 91久久视频| 亚洲欧美日韩电影| 国产丝袜美腿一区二区三区| 欧美在线免费播放| 欧美成人蜜桃| 一区二区三区国产精品| 欧美欧美全黄| 亚洲欧洲av一区二区| 狼人社综合社区| 亚洲伦理在线| 国产农村妇女精品一二区| 久久高清福利视频| 亚洲欧洲偷拍精品| 欧美一区二区高清| 伊人婷婷欧美激情| 欧美日韩一卡二卡| 久久精品91| 亚洲精品国产精品乱码不99 | 美女视频网站黄色亚洲| 亚洲国产欧美久久| 国产精品视频99| 久久伊人精品天天| 一区二区三区www| 久久婷婷一区| 一区二区三区四区五区在线| 国产亚洲激情视频在线| 欧美aa在线视频| 午夜国产精品视频| 亚洲二区在线视频| 久久成人免费日本黄色| 亚洲精品欧美激情| 国产自产女人91一区在线观看| 欧美第一黄色网| 久久成人av少妇免费| 亚洲伦伦在线| 欧美激情精品久久久久久久变态 | 欧美高潮视频| 久久成年人视频| 亚洲视频1区| 亚洲国产天堂久久国产91| 国产乱码精品一区二区三区五月婷| 午夜视频精品| 91久久久亚洲精品| 激情欧美一区| 国产日本欧洲亚洲| 国产精品av久久久久久麻豆网| 久久综合激情| 久久av在线看| 亚洲欧美三级在线| 一区二区国产在线观看| 亚洲国产色一区| 欧美大片免费观看| 欧美成人福利视频| 久久亚洲一区二区三区四区| 久久av在线看| 性色av一区二区三区红粉影视| 日韩午夜电影av| 最新高清无码专区| 亚洲欧洲一区二区天堂久久 | 欧美成在线视频| 久久亚洲免费| 欧美尤物一区| 欧美一区二区三区男人的天堂| 亚洲欧美日韩中文视频| 亚洲综合电影| 羞羞色国产精品| 欧美一区视频| 久久精品国产99国产精品| 欧美一级视频精品观看| 欧美一区二区三区成人| 欧美在线影院| 久久综合网络一区二区| 蜜桃精品久久久久久久免费影院| 米奇777超碰欧美日韩亚洲| 久久综合伊人77777蜜臀| 麻豆九一精品爱看视频在线观看免费| 久久综合99re88久久爱| 欧美大片网址| 欧美视频在线免费| 国产精品视频免费一区| 国产日韩欧美中文| 亚洲风情亚aⅴ在线发布| 亚洲国产精品一区二区第四页av | 韩国一区二区三区美女美女秀| 国产亚洲精品aa| 亚洲第一精品福利| 一区二区高清视频| 欧美影院视频| 欧美成ee人免费视频| 亚洲精品乱码久久久久久按摩观| 一区二区三区精品视频| 欧美一区二区三区视频在线观看 | 欧美韩国在线| 国产精品美女久久久| 伊人成综合网伊人222| 亚洲三级视频| 久久精品av麻豆的观看方式 | 蜜臀91精品一区二区三区| 欧美激情亚洲一区| 在线亚洲一区二区| 久久亚洲影院| 欧美午夜精品久久久久久孕妇| 国产精品一区二区久激情瑜伽| 影音国产精品| 亚洲女同在线| 亚洲二区在线观看| 亚洲网在线观看| 免费影视亚洲| 国产精品免费一区二区三区观看| 亚洲福利视频一区| 校园激情久久| 亚洲精品国产日韩| 久久精品国产精品亚洲精品| 欧美揉bbbbb揉bbbbb| 亚洲国产成人不卡| 欧美一级视频| 亚洲精品影院在线观看| 久久久久久网站| 国产欧美日韩一区| 亚洲综合大片69999| 亚洲国产另类久久久精品极度| 欧美一区二区视频在线| 亚洲成人资源|