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

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>
              亚洲美洲欧洲综合国产一区| 日韩视频精品在线| 亚洲欧美日韩一区二区在线 | 亚洲国产精品悠悠久久琪琪 | 亚洲成人自拍视频| 嫩草国产精品入口| 亚洲欧洲另类| 亚洲人成精品久久久久| 欧美日韩八区| 亚洲专区欧美专区| 亚洲欧美日韩一区二区在线| 国产一区二区三区黄| 久久艳片www.17c.com| 麻豆av一区二区三区久久| 91久久黄色| 一本色道久久88精品综合| 国产精品精品视频| 久久精品视频免费观看| 麻豆精品精华液| 99视频一区二区| 亚洲欧美激情视频| 一区二区在线观看视频在线观看| 欧美国产一区二区| 国产精品久久久久婷婷| 久久久久国产一区二区三区| 快she精品国产999| 亚洲影音先锋| 久久女同互慰一区二区三区| 国产精品99久久久久久久久| 欧美亚洲在线观看| 99视频一区| 久久久www成人免费无遮挡大片| 亚洲电影欧美电影有声小说| 99视频一区二区三区| 国产午夜一区二区三区| 最近中文字幕日韩精品| 国产欧美日韩另类一区| 亚洲国产精品国自产拍av秋霞| 欧美三级在线视频| 麻豆9191精品国产| 国产精品亚洲激情| 亚洲高清精品中出| 国产一区二区在线观看免费播放| 亚洲人成人99网站| 国产综合视频| 一区二区三区|亚洲午夜| 亚洲电影中文字幕| 性欧美超级视频| 亚洲一区二区三区激情| 欧美成人精品一区二区三区| 久久久精品国产免大香伊 | 这里只有精品丝袜| 亚洲激情欧美| 久久米奇亚洲| 久久国产精品久久久久久| 欧美激情一区在线观看| 欧美xxxx在线观看| 黄色精品免费| 性xx色xx综合久久久xx| 亚洲在线播放| 欧美日韩三级在线| 亚洲日本欧美| 亚洲精一区二区三区| 久久er精品视频| 久久精品国产欧美亚洲人人爽| 欧美日韩中文精品| 亚洲美女视频在线观看| 亚洲美女诱惑| 欧美连裤袜在线视频| 亚洲国产精品成人综合色在线婷婷 | 蜜臀久久久99精品久久久久久| 国产精品爽爽ⅴa在线观看| 日韩视频不卡| 亚洲性感激情| 国产精品乱人伦中文| 一区二区三区日韩精品| 亚洲一本大道在线| 国产精品久久久久久久久婷婷| 99精品国产在热久久| 亚洲自拍高清| 国产视频综合在线| 久久精品一区中文字幕| 老司机免费视频一区二区| 在线观看国产成人av片| 免费观看在线综合| 亚洲人永久免费| 亚洲欧美日韩中文播放| 国产欧美日韩精品丝袜高跟鞋 | 亚洲欧洲精品成人久久奇米网| 91久久中文| 欧美午夜精品伦理| 亚洲一区二区三区四区视频 | 亚洲大片精品永久免费| 欧美成人小视频| 亚洲视频在线一区| 久久久久.com| 亚洲精选久久| 国产精品香蕉在线观看| 欧美在线播放一区二区| 蜜臀av一级做a爰片久久| 亚洲精品视频二区| 国产精品视区| 美女诱惑黄网站一区| 一区二区三区欧美视频| 久久噜噜噜精品国产亚洲综合| 最新中文字幕一区二区三区| 欧美三级欧美一级| 欧美综合国产| 亚洲精品久久久一区二区三区| 亚洲欧美不卡| 亚洲国产天堂网精品网站| 欧美色欧美亚洲另类二区| 欧美专区在线播放| 99精品热视频| 免费一区二区三区| 午夜国产欧美理论在线播放| 亚洲国产电影| 国产欧美精品一区| 欧美精品999| 久久精品二区| 亚洲视频狠狠| 亚洲国产精品久久久久婷婷老年| 午夜精品视频在线观看一区二区 | 欧美日韩国产高清| 久久国产精品99国产精| 亚洲午夜久久久| 亚洲国产欧美精品| 久久夜色撩人精品| 羞羞色国产精品| 中文亚洲字幕| 亚洲精品日韩精品| 伊人男人综合视频网| 国产欧美日韩免费| 国产精品免费视频观看| 欧美精品在线播放| 美女国产一区| 另类图片国产| 久久精品中文字幕免费mv| 亚洲一区二区三区久久| 99re视频这里只有精品| 亚洲国产精品一区在线观看不卡| 老司机午夜精品| 久久久久国色av免费看影院| 亚洲欧美一区二区三区久久| av成人国产| 一本色道久久加勒比88综合| 亚洲日本在线视频观看| 在线观看av不卡| 在线不卡免费欧美| 在线日韩电影| 亚洲黄色有码视频| 亚洲激情图片小说视频| 亚洲精华国产欧美| 91久久久久久国产精品| 亚洲精品乱码久久久久| 亚洲茄子视频| 一本一本大道香蕉久在线精品| 亚洲精选一区| 亚洲先锋成人| 午夜精品久久久久久久99樱桃| 亚洲一区二区少妇| 性伦欧美刺激片在线观看| 欧美在线在线| 久久视频在线免费观看| 免费在线观看日韩欧美| 欧美激情精品久久久久久蜜臀| 亚洲福利视频二区| 亚洲精品乱码| 亚洲欧美春色| 久久久噜噜噜久久人人看| 美女视频黄免费的久久| 欧美日韩理论| 国产欧美大片| 亚洲国产精品视频| 一本色道久久综合一区| 欧美一区=区| 美国成人直播| 亚洲黄色成人网| 中文av一区二区| 久久精品导航| 欧美日韩系列| 国模私拍视频一区| 日韩一区二区高清| 欧美一区在线直播| 欧美成人精品h版在线观看| 亚洲精品一区二区三区在线观看| 亚洲午夜电影网| 久久久久国产一区二区三区| 欧美久久久久中文字幕| 国产欧美日韩免费| 亚洲日本精品国产第一区| 亚洲女性裸体视频| 欧美激情1区2区| 午夜精品一区二区三区在线视| 女人香蕉久久**毛片精品| 国产精品视频一区二区高潮| 亚洲欧洲精品天堂一级| 久久精品人人做人人综合| 91久久精品网| 久久精品五月|