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

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 夢在天涯 閱讀(776) 評論(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

搜索

  •  

積分與排名

  • 積分 - 1811738
  • 排名 - 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>
              久久综合色天天久久综合图片| 亚洲欧美一区二区视频| 麻豆精品视频在线| 久久视频一区二区| 亚洲国产一区二区精品专区| 亚洲国产精品精华液2区45 | 久久久亚洲一区| 久久久精品网| 日韩天天综合| 亚洲一区二区三区国产| 国内精品视频在线观看| 亚洲高清视频在线| 国产精品二区在线观看| 久久综合激情| 欧美日韩一区自拍| 久久婷婷人人澡人人喊人人爽 | 日韩午夜电影av| 国产精品日本| 国产精品久久久爽爽爽麻豆色哟哟| 羞羞答答国产精品www一本| 久久高清国产| 亚洲性夜色噜噜噜7777| 久久不见久久见免费视频1| 亚洲欧洲综合另类在线| 亚洲欧美成人一区二区三区| 亚洲电影专区| 午夜精品久久久久久久99热浪潮| 亚洲人精品午夜| 性视频1819p久久| 日韩一级片网址| 久久精品盗摄| 亚洲欧美第一页| 欧美激情第8页| 巨胸喷奶水www久久久免费动漫| 欧美激情欧美激情在线五月| 欧美中文字幕在线观看| 欧美日韩视频一区二区三区| 免费中文日韩| 国产一区二区三区在线观看免费| 99国产精品久久久久久久久久| 伊人久久综合97精品| 亚洲一区二区毛片| 99精品国产在热久久下载| 久久精品国内一区二区三区| 亚洲欧美一区二区三区在线 | 亚洲欧美综合一区| 在线视频中文亚洲| 欧美成人精品福利| 久久综合狠狠| 国产一区二区三区久久久| 99国产一区| 亚洲视频1区2区| 欧美激情第10页| 91久久嫩草影院一区二区| 在线观看精品| 巨胸喷奶水www久久久免费动漫| 久久精品欧美日韩精品| 国产精品视频专区| 亚洲午夜久久久| 亚洲制服丝袜在线| 欧美性理论片在线观看片免费| 亚洲另类黄色| 亚洲自拍三区| 国产精品三区www17con| 亚洲小说欧美另类婷婷| 香港成人在线视频| 国产欧美日韩免费看aⅴ视频| 亚洲一区二区精品在线| 久久国产精品免费一区| 狠狠入ady亚洲精品经典电影| 欧美在线免费观看| 欧美大片va欧美在线播放| 在线观看一区欧美| 欧美激情一区二区三区| 一区二区久久久久久| 午夜精品久久久久久| 国产日韩欧美亚洲| 久久精品夜色噜噜亚洲a∨| 欧美1区2区| 一本大道久久a久久综合婷婷| 欧美日韩国产在线看| 亚洲视频在线观看网站| 久久国产精品亚洲va麻豆| 在线免费观看欧美| 欧美二区在线看| 亚洲天堂免费观看| 久久婷婷成人综合色| 亚洲精品一区二区三区不| 欧美三级中文字幕在线观看| 亚洲欧美日韩在线不卡| 欧美福利视频网站| 亚洲永久视频| 伊人精品成人久久综合软件| 欧美日本三级| 欧美在线观看www| 91久久国产综合久久蜜月精品| 亚洲欧美日韩爽爽影院| 伊人久久婷婷| 国产精品毛片一区二区三区 | 亚洲欧美激情诱惑| 久久综合九色欧美综合狠狠| 9i看片成人免费高清| 国产亚洲网站| 欧美精品成人| 久久国产婷婷国产香蕉| 亚洲免费黄色| 欧美xxxx在线观看| 欧美一区二区三区视频在线| 亚洲人成在线观看网站高清| 国产精品一区二区三区久久| 欧美激情第3页| 久久久精品性| 午夜免费在线观看精品视频| 亚洲国产日韩欧美在线动漫| 久久久久国产一区二区三区| 亚洲自拍偷拍麻豆| 亚洲毛片在线| 在线免费观看视频一区| 国产精品综合久久久| 欧美日韩午夜精品| 美女网站久久| 久久精品免费看| 午夜精品久久久久久99热| 99re热精品| 亚洲国产欧美在线| 欧美高清在线一区二区| 久久视频这里只有精品| 欧美一区国产二区| 香蕉国产精品偷在线观看不卡| 在线一区二区三区做爰视频网站 | 国产美女高潮久久白浆| 欧美日韩一区自拍| 欧美韩国在线| 欧美高清在线视频| 欧美成人官网二区| 欧美成人69| 欧美成人国产| 欧美精品一区二区在线观看| 欧美91视频| 欧美精品久久一区| 欧美激情在线| 欧美日韩网站| 国产精品午夜电影| 国产精品午夜视频| 国产欧美三级| 黄色成人av在线| 亚洲大片免费看| 亚洲激情网站| 亚洲色图在线视频| 亚洲一二三区在线| 亚洲欧美中文另类| 欧美一区二区视频网站| 久久国产欧美日韩精品| 狂野欧美一区| 亚洲国产美女| 亚洲毛片在线观看| 亚洲一区二区三区免费在线观看| 亚洲午夜一区二区| 久久精品99国产精品| 久久综合九九| 欧美日本高清一区| 国产精品亚洲第一区在线暖暖韩国| 国产日韩一区二区三区在线播放| 黄色日韩精品| 99国产精品国产精品久久| 亚洲制服欧美中文字幕中文字幕| 久久国产一区| 亚洲啪啪91| 亚洲男女自偷自拍图片另类| 久久久久久久激情视频| 欧美日韩国产综合新一区| 国产欧美日本一区视频| 在线观看国产精品淫| 亚洲一区二区精品在线观看| 久久精品日韩| 亚洲免费电影在线观看| 欧美专区第一页| 欧美日韩国产成人精品| 国产一区二区三区黄视频| 一本久久青青| 久久综合色8888| 在线亚洲免费视频| 久热综合在线亚洲精品| 国产精品视频精品视频| 亚洲精品在线视频观看| 欧美中文在线观看| 亚洲精选91| 久久综合九色99| 国产三区精品| 亚洲一区三区视频在线观看| 欧美大片在线看免费观看| 亚洲欧美中日韩| 欧美日韩亚洲高清| 亚洲第一区色| 久久中文在线| 亚洲伊人一本大道中文字幕| 欧美日韩成人一区二区三区| 在线免费观看日本一区| 久久久久国色av免费看影院 | 裸体歌舞表演一区二区|