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

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三区| 99视频在线观看一区三区| 欧美特黄一级大片| 欧美亚洲一区三区| 久久精品国产免费观看| 在线不卡a资源高清| 亚洲二区在线视频| 欧美极品在线播放| 亚洲一区二区三区国产| 午夜精品婷婷| 亚洲国产高清在线观看视频| 亚洲人成免费| 国产精品一区二区三区四区五区| 欧美亚洲一区在线| 久久久久久网站| 一本色道久久88综合日韩精品| 亚洲精品一区二区三区99| 国产精品毛片一区二区三区| 久久久一本精品99久久精品66| 久热精品视频| 亚洲自拍偷拍色片视频| 欧美一区中文字幕| 日韩亚洲一区在线播放| 亚洲性视频h| 亚洲欧洲综合另类在线| 在线一区二区日韩| 国内精品国产成人| 99伊人成综合| 激情成人av| 在线一区二区三区做爰视频网站| 国产自产女人91一区在线观看| 亚洲国产小视频| 国产一区二区三区在线观看免费| 91久久精品网| 一区二区三区无毛| 亚洲一区二区网站| 亚洲精品国产系列| 久久国产精品毛片| 亚洲一区二区3| 免费亚洲一区二区| 久久综合色婷婷| 国产精品青草久久| 亚洲精品乱码久久久久久久久| 黄色亚洲免费| 亚洲免费视频中文字幕| 一区二区三区国产盗摄| 麻豆av一区二区三区| 欧美在线免费观看视频| 国产精品成人v| 最新国产精品拍自在线播放| 在线看视频不卡| 欧美一区二区三区啪啪| 亚洲欧美美女| 欧美日韩在线亚洲一区蜜芽| 亚洲电影免费观看高清完整版在线观看| 国产三级欧美三级日产三级99| 一区二区三区精密机械公司| 一本大道久久a久久精二百| 久久综合久久88| 老牛影视一区二区三区| 黑人巨大精品欧美一区二区| 午夜精品福利电影| 欧美资源在线观看| 国产久一道中文一区| 亚洲一区二区视频在线观看| 亚洲欧美国产毛片在线| 国产精品yjizz| 亚洲午夜精品一区二区三区他趣 | 欧美精品导航| 亚洲激情影视| 中日韩美女免费视频网址在线观看| 欧美成人激情视频| 亚洲精品少妇| 亚洲午夜高清视频| 国产精品免费小视频| 亚洲欧美日韩视频一区| 久久精品亚洲乱码伦伦中文| 狠狠88综合久久久久综合网| 久久久999精品视频| 欧美1区2区| 日韩午夜av| 国产精品久久久久久久免费软件 | 久久精品99国产精品日本| 国产欧美成人| 久久男人资源视频| 91久久精品国产| 亚洲欧美视频一区| 国产偷国产偷精品高清尤物| 久久精品国产亚洲精品| 亚洲丰满少妇videoshd| 一区二区三区www| 国产欧美日本| 美女精品在线观看| 亚洲精品三级| 欧美一区午夜精品| 亚洲国产精品一区二区第四页av | 另类av一区二区| 亚洲精品久久久久久久久久久久| 亚洲桃花岛网站| 国产一区视频在线看| 欧美电影免费观看网站| 亚洲一区图片| 欧美高清你懂得| 亚洲在线不卡| 亚洲国产精品小视频| 欧美视频网址| 美女黄毛**国产精品啪啪| 一区二区三区四区国产精品| 久久人人97超碰人人澡爱香蕉| 亚洲日韩中文字幕在线播放| 国产精品一区二区三区观看| 欧美69视频| 性久久久久久| 这里只有精品丝袜| 亚洲国产精品热久久| 久久精品人人做人人综合| 99在线观看免费视频精品观看| 国产日韩高清一区二区三区在线| 欧美精品v日韩精品v韩国精品v| 欧美一区二区在线视频| 99精品黄色片免费大全| 免费久久99精品国产自| 亚洲欧美一区二区原创| 99视频精品全部免费在线| 红桃视频成人| 国产乱码精品一区二区三| 欧美日本一区二区视频在线观看| 久久精品国产亚洲一区二区| 亚洲午夜伦理| 99国产精品久久久久久久久久| 欧美电影在线免费观看网站| 久久青草久久| 久久精品在线播放| 午夜精品一区二区三区在线| 欧美肥婆bbw| 麻豆精品精华液| 亚洲欧美久久久久一区二区三区| 亚洲精品欧美精品| 欧美激情精品久久久久久| 久久在精品线影院精品国产| 国产亚洲精品v| 欧美午夜片欧美片在线观看| 国产亚洲一区二区三区在线播放| 在线成人免费观看| 在线一区亚洲| 久久久久久久久久久久久9999| 欧美黄色影院| 亚洲欧美成aⅴ人在线观看| 久久久久久久一区| 欧美三区不卡| 亚洲国产精品一区二区www| 亚洲午夜一区| 欧美h视频在线| 亚洲香蕉伊综合在人在线视看| 久久亚洲不卡| 国产美女搞久久| 夜夜嗨av一区二区三区网站四季av| 久久久久88色偷偷免费| 9国产精品视频| 欧美xart系列高清| 国产亚洲一区二区三区| 亚洲一区999| 亚洲国产欧美一区| 久久久99国产精品免费| 欧美视频手机在线| 91久久线看在观草草青青| 久久精品国产一区二区三| 一区二区三区三区在线| 免费久久99精品国产自| 国产综合亚洲精品一区二| 亚洲免费一区二区| 亚洲精品小视频在线观看| 媚黑女一区二区| 狠狠入ady亚洲精品| 午夜精品福利一区二区三区av | 亚洲欧洲日韩在线| 久久精品91久久久久久再现| 国产精品一卡二| 亚洲网站在线播放| 亚洲国产综合91精品麻豆| 久久精品青青大伊人av| 国产麻豆综合| 欧美永久精品| 亚洲欧美清纯在线制服| 国产精品爱久久久久久久| 99国产精品久久久久久久久久| 欧美电影电视剧在线观看| 久久免费视频这里只有精品| 精品99视频| 美女被久久久| 美女91精品| 亚洲精品一区二区三区蜜桃久| 亚洲国产精品激情在线观看| 欧美成年人网|