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

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

搜索

  •  

積分與排名

  • 積分 - 1811735
  • 排名 - 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久久精品综合麻豆| 91久久线看在观草草青青| 欧美日韩视频在线第一区| 亚洲一级网站| 久久国内精品自在自线400部| 尤物在线观看一区| 亚洲欧洲日产国产综合网| 欧美三日本三级少妇三2023| 欧美与欧洲交xxxx免费观看| 久久久久.com| 亚洲一区二区精品在线| 久久精品国产清自在天天线| 亚洲日本电影| 午夜国产精品视频免费体验区| 黄色欧美成人| 9l国产精品久久久久麻豆| 国产亚洲一本大道中文在线| 亚洲国产精品嫩草影院| 国产精品一区二区你懂的| 欧美成人一品| 国产精品一区二区在线| 亚洲第一久久影院| 国产午夜精品视频免费不卡69堂| 亚洲福利国产精品| 国产日韩精品久久| 亚洲免费av电影| 在线观看国产欧美| 亚洲欧美日韩电影| 99视频超级精品| 久久露脸国产精品| 欧美亚洲视频在线观看| 欧美精品免费看| 玖玖国产精品视频| 国产模特精品视频久久久久| 亚洲日本成人女熟在线观看| 一区二区三区我不卡| 亚洲图片在区色| 亚洲精品专区| 美女久久网站| 欧美a级片一区| 国产主播一区| 校园激情久久| 欧美一区亚洲二区| 国产精品日日摸夜夜添夜夜av | 黄色精品一区| 亚洲欧美色一区| 午夜国产精品视频| 欧美午夜在线一二页| 亚洲黄色一区| 亚洲免费福利视频| 欧美不卡一区| 亚洲电影在线| 日韩特黄影片| 欧美国产日韩一区二区三区| 模特精品在线| 亚洲国产欧美一区二区三区丁香婷| 欧美一区二区视频在线观看| 欧美在线综合| 国产日韩av在线播放| 亚洲影院一区| 久久成人亚洲| 红桃视频成人| 久久午夜国产精品| 亚洲国产老妈| 99国产精品久久久久老师| 欧美伦理a级免费电影| 亚洲精品中文在线| 亚洲在线中文字幕| 国产日韩欧美一区二区| 久久精品国产99国产精品| 狂野欧美激情性xxxx| 亚洲国产欧美一区二区三区丁香婷| 免费观看国产成人| 亚洲精品一区久久久久久| 亚洲午夜未删减在线观看| 国产精品亚洲成人| 久久久久在线观看| 亚洲精品国产视频| 午夜视频久久久| 一区二区三区在线免费观看| 欧美阿v一级看视频| 在线亚洲美日韩| 久久久久成人精品免费播放动漫| 亚洲成人影音| 欧美三级视频在线| 欧美在线关看| 91久久精品国产91久久性色tv| 亚洲免费在线| 在线日本成人| 国产精品va在线| 久久人人九九| 一区二区三区四区五区视频| 久久亚洲图片| 亚洲一区在线观看视频| 一色屋精品视频免费看| 欧美日韩国产首页| 久久久www免费人成黑人精品 | 亚洲第一区色| 欧美在线不卡视频| 亚洲免费黄色| 狠狠色综合网站久久久久久久| 欧美搞黄网站| 久久精品免费看| 日韩一区二区精品在线观看| 久久综合狠狠综合久久激情| 亚洲少妇一区| 亚洲国产精品女人久久久| 国产精品女人久久久久久| 欧美成人久久| 久久久久久噜噜噜久久久精品| 中文在线资源观看视频网站免费不卡| 蜜臀a∨国产成人精品| 香蕉成人啪国产精品视频综合网| 亚洲精品久久久久中文字幕欢迎你| 国产亚洲福利社区一区| 欧美日韩一区二区在线| 欧美成人精品一区二区三区| 久久av一区二区三区| 亚洲一区二区在线| 欧美日韩成人网| 欧美一区二区免费观在线| 免费成人黄色| 久久久久国产精品午夜一区| 亚洲一区二区三区高清| 亚洲精品综合在线| 影音先锋亚洲一区| 黄页网站一区| 国产欧美一区二区三区久久人妖 | 亚洲欧美日韩国产一区二区三区| 亚洲欧洲在线播放| 亚洲第一二三四五区| 男人的天堂成人在线| 久久综合狠狠综合久久激情| 久久久久国产精品午夜一区| 久久精品人人| 久久亚洲电影| 欧美大片在线影院| 欧美激情第9页| 亚洲国产视频直播| 亚洲国产一区视频| 亚洲黄色影院| 一区二区激情| 亚洲一区三区视频在线观看| 亚洲午夜激情免费视频| 亚洲永久网站| 欧美一区2区三区4区公司二百| 性欧美1819sex性高清| 欧美一级久久| 麻豆精品精华液| 欧美了一区在线观看| 欧美日韩日韩| 国产欧美在线观看一区| 国内偷自视频区视频综合| 激情久久久久| 亚洲免费观看在线视频| 亚洲影院色无极综合| 欧美一区二区视频网站| 猛男gaygay欧美视频| 亚洲欧洲日韩综合二区| 亚洲午夜av| 久久天堂国产精品| 欧美日韩免费一区| 国产一区二区在线观看免费| 亚洲国产一二三| 亚洲一区视频在线观看视频| 久久精品视频一| 亚洲黄色免费网站| 亚洲欧美国产另类| 美女精品自拍一二三四| 国产精品伦子伦免费视频| 在线观看国产精品网站| 中日韩在线视频| 久久尤物视频| 一区二区三区精品视频在线观看| 欧美一区二区三区日韩视频| 欧美激情一二区| 狠狠色狠狠色综合日日91app| 日韩午夜视频在线观看| 久久精品av麻豆的观看方式| 亚洲欧洲精品一区二区精品久久久| 亚洲欧美国产精品桃花| 欧美福利视频在线| 国模精品一区二区三区| 亚洲深夜福利| 欧美激情精品久久久久久久变态| 亚洲免费人成在线视频观看| 欧美激情亚洲| 亚洲国产精品ⅴa在线观看| 新狼窝色av性久久久久久| 亚洲黄色影院| 美女主播精品视频一二三四| 国产视频在线一区二区| 亚洲在线视频免费观看| 亚洲欧洲视频| 欧美成人免费在线视频| 极品中文字幕一区| 久久精品日韩|