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

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>
              欧美99在线视频观看| 久久欧美肥婆一二区| 欧美日韩性视频在线| 亚洲人成在线影院| 亚洲国产一区二区精品专区| 久久一区二区三区四区| 亚洲黄色尤物视频| 亚洲精品在线三区| 国产精品日本精品| 久久精品理论片| 美女久久网站| 一区二区欧美在线观看| 夜色激情一区二区| 国产一区导航| 亚洲国产美女| 国产精品女主播一区二区三区| 欧美一级网站| 米奇777在线欧美播放| 一级日韩一区在线观看| 午夜亚洲性色视频| 亚洲精品国产视频| 亚洲婷婷综合久久一本伊一区| 国产欧美韩国高清| 亚洲成人在线网| 国产精品一区二区久激情瑜伽| 久久免费的精品国产v∧| 欧美大片在线观看一区二区| 亚洲香蕉伊综合在人在线视看| 性色av香蕉一区二区| 亚洲黄色成人久久久| 亚洲图色在线| 最新亚洲激情| 亚洲欧美中文日韩v在线观看| 亚洲国产岛国毛片在线| 亚洲天堂偷拍| 亚洲精品日韩在线| 欧美有码在线观看视频| 99re66热这里只有精品4| 亚洲欧美日韩国产成人精品影院 | 久久久激情视频| 在线亚洲自拍| 美女日韩在线中文字幕| 小黄鸭精品密入口导航| 欧美精品v日韩精品v国产精品| 久久久久久久久久久一区 | 久久久精品国产99久久精品芒果| 亚洲精品免费看| 久久久久久伊人| 久久精品国产亚洲aⅴ| 欧美日韩午夜视频在线观看| 牛人盗摄一区二区三区视频| 国产三区二区一区久久| 一区二区三区高清| 一区二区国产在线观看| 欧美成人精品影院| 米奇777在线欧美播放| 国产一区二区三区在线观看视频| 亚洲视频二区| 亚洲一区欧美一区| 欧美日韩精品一区二区| 亚洲精品裸体| 亚洲视频精选| 欧美午夜无遮挡| 99国产精品99久久久久久| 亚洲精品中文字幕有码专区| 美女啪啪无遮挡免费久久网站| 久久嫩草精品久久久精品| 国产午夜精品一区二区三区欧美| 亚洲一区视频在线| 欧美一级大片在线观看| 国产精品专区h在线观看| 亚洲欧美变态国产另类| 欧美中文字幕在线观看| 国产精品美女久久久免费 | 久久久综合网站| 国产亚洲第一区| 欧美一区免费视频| 久久只有精品| 尤物网精品视频| 欧美成人精品在线视频| 91久久久在线| 亚洲一区二区三区四区在线观看| 欧美日韩亚洲三区| 亚洲综合999| 久久精品青青大伊人av| 在线免费高清一区二区三区| 欧美激情视频网站| 99国产精品视频免费观看一公开| 久久裸体视频| 亚洲日本免费| 欧美激情一区二区三区在线视频 | 中文亚洲欧美| 国产精品久久久久久久久久三级| 亚洲欧美另类国产| 欧美ab在线视频| 亚洲在线视频一区| 国内精品一区二区| 欧美精品不卡| 亚洲欧美制服中文字幕| 欧美国产精品一区| 亚洲男人的天堂在线| 黄色在线一区| 欧美日韩在线不卡| 久久久久久久精| 一区二区三区不卡视频在线观看 | 一区二区三区视频在线看| 国产精品海角社区在线观看| 久久精品国产久精国产思思| 日韩视频在线一区| 久久这里只有| 亚洲尤物影院| 亚洲欧洲日韩女同| 国产色产综合色产在线视频| 欧美另类高清视频在线| 欧美在线视频一区| 夜夜狂射影院欧美极品| 另类av一区二区| 亚洲欧美综合v| 日韩午夜在线电影| 伊人久久大香线蕉av超碰演员| 欧美日韩一级视频| 免费观看成人鲁鲁鲁鲁鲁视频| 亚洲欧美综合v| 一区二区三区鲁丝不卡| 亚洲电影在线播放| 久久综合狠狠| 欧美一区亚洲二区| 亚洲一区二区在线视频| 亚洲精品在线观看免费| 国产日韩专区在线| 国产精品揄拍一区二区| 国产精品国产福利国产秒拍| 欧美第十八页| 美日韩丰满少妇在线观看| 午夜欧美精品久久久久久久| 在线亚洲高清视频| 9i看片成人免费高清| 91久久精品一区| 亚洲第一综合天堂另类专| 欧美成人午夜| 欧美成人日韩| 欧美国产日本| 欧美激情五月| 亚洲高清色综合| 亚洲高清自拍| 亚洲欧洲另类| 亚洲精品美女91| 日韩亚洲视频在线| 99综合在线| 亚洲天堂男人| 欧美一区永久视频免费观看| 亚洲欧美精品在线| 欧美怡红院视频| 久久精品亚洲一区| 久热re这里精品视频在线6| 久久青青草综合| 欧美jjzz| 欧美色精品天天在线观看视频| 欧美色图首页| 国产精品一区一区| 在线欧美电影| 99国内精品久久久久久久软件| 亚洲精品永久免费精品| 亚洲午夜激情| 久久久夜夜夜| 亚洲国产一区二区三区青草影视| 日韩视频不卡中文| 亚洲一区二区三区四区五区黄| 午夜精品福利视频| 久久综合九色欧美综合狠狠| 欧美精品成人在线| 国产精品久久久久婷婷| 国产综合18久久久久久| 亚洲欧洲日本mm| 午夜精品久久久久久久久久久久久| 久久精品在线免费观看| 欧美成人一区二免费视频软件| 亚洲乱码国产乱码精品精可以看| 亚洲天堂网在线观看| 久久久人成影片一区二区三区 | 午夜亚洲性色福利视频| 久久男女视频| 欧美亚韩一区| 亚洲高清网站| 欧美亚洲午夜视频在线观看| 欧美国产第一页| 亚洲欧美日韩第一区| 欧美成人四级电影| 国产视频久久久久久久| 一区二区高清| 老司机一区二区| 亚洲图片欧美一区| 蜜臀av在线播放一区二区三区| 国产精品美女一区二区| 最新国产乱人伦偷精品免费网站 | 伊人久久大香线蕉av超碰演员| 亚洲视频网在线直播| 欧美国产亚洲精品久久久8v| 亚洲在线免费| 欧美视频在线观看 亚洲欧|