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

C++ Programmer's Cookbook

{C++ 基礎} {C++ 高級} {C#界面,C++核心算法} {設計模式} {C#基礎}

Properties/Access Modifiers/Enumerations/Interfaces(c#)

Properties

Properties provide added functionality to the .NET Framework. Normally, we use accessor methods to modify and retrieve values in C++ and Visual Basic. If you have programmed using Visual Basic's ActiveX technology, this concept is not new to you. Visual Basic extensively uses accessor methods such as getXXX() and setXXX() to create user-defined properties.

A C# property consists of:

  • Field declaration
  • Accessor Methods (getter and setter methods)

Getter methods are used to retrieve the field's value and setter methods are used to modify the field's value. C# uses a special Value keyword to achieve this. Listing 10 declares a single field named zipcode and shows how to apply the field in a property.

Listing 10

using System;
class Propertiesexample
{

  //Field "idValue" declared
  public string idValue;

  //Property declared
  public string IdValue
  {

    get
    {
      return idValue;
    }
    set
    {
      idValue = value;
    }
  }

  public static void Main(string[] args)
  {

    Propertiesexample pe = new Propertiesexample();
    pe.IdValue = "009878";
    string p = pe.IdValue;
    Console.WriteLine("The Value is {0}",p);
  }
}

If you look at the MSIL generated by the code in Listing 10, you can view two methods, as shown in Listing 11:

Listing 11

Address::get_Zip() and Address::set_Zip().

We have reproduced the code for your reference. On careful scrutiny, you can see that the set_zip() method takes a string argument. This is because we have passed a string value while accessing the property. It's illegal to call these methods directly in a C# program.

In the preceding code, the Address.zipcode property is considered a read and write property because both getter and setter methods are defined. If you want to make the property read-only, omit the set block and to make it write only, omit the get block.

----------------------------------------------------------------------------------------------------------------------------------

Access Modifiers

Access modifiers determine the scope of visibility for variables and methods. There are four types of modifiers in C#: Public, Protected, Private, and Internal. For example, a variable declared as private in a super class cannot be called in a sub class. It should be declared as either public or protected. Let's examine each of these in detail.

  1. Public members are accessible from outside the class definition.
  2. Protected members are not visible outside the class and can be accessed only by derived or sub classes.
  3. Private member's scope is limited to the defined class. Derived classes or any other classes cannot access these members.
  4. Internal members are visible only within the current compilation unit.

-----------------------------------------------------------------------------------------------------------------------------------------

Enumerations

the data type for each constant in an enumeration is an integer by default, but you can change the type to byte, long, and so forth.

Listing 6 below illustrates how to apply the Employees enumeration in a C# program.

Listing 6

using System;
enum Employees
{
  Instructors,
  Assistants,
  Counsellors
}

class Employeesenum
{
  public static void Display(Employees e)
  {

    switch(e)
    {
      case Employees.Instructors:
        Console.WriteLine("You are an Instructor");
        break;

      case Employees.Assistants:
        Console.WriteLine("You are one of the Assistants");
        break;

      case Employees.Counsellors:
        Console.WriteLine("You are a counsellor");
        break;

      default:break;
    }

  }

  public static void Main(String[] args)
  {
    Employees emp;
    emp = Employees.Counsellors;
    Display(emp);
  }

}

C# enumerations derive from System.Enum. Table 1 explains some of the important methods and properties of this class.

Table 1—List of Properties and methods of System.Enum class

Method Name Description
GetUnderlyingType() Returns the data type used to represent the enumeration.
Format() Returns the string associated with the enumeration.
GetValues() Returns the members of the enumeration.
Property Name Description
IsDefined() Returns whether a given string name is a member of the current enumeration.

----------------------------------------------------------------------------------------------------------------------------------------------

Interfaces

To rectify the drawback of multiple inheritance, the creators of C# have introduced a new concept called interfaces. Java programmers may be well aware of this concept. All interfaces should be declared with the keyword interface. You can implement any number of interfaces in a single derived class, but you should provide signatures to all method definitions of the corresponding interfaces.

Two or more interfaces can be combined into a single interface and implemented in a class,

You easily can determine whether a particular interface is implemented in a class by using is and as operators. The is operator enables you to check whether one type or class is compatible with another type or class; it returns a Boolean value. Listing 3 illustrates the usage of the is operator by revisiting Listing 2.

Listing 3

using System;

interface Interdemo
{
  bool Show();
}

interface Interdemo1
{
  bool Display();
}

class Interimp:Interdemo
{
  public bool Show()
  {
    Console.WriteLine("Show() method Implemented");
  return true;
  }

  public static void Main(string[] args)
  {
    Interimp inter = new Interimp();
    inter.Show();

    if(inter is Interdemo1)
    {
      Interdemo1 id = (Interdemo1)inter;
      bool ok = id.Display();
      Console.WriteLine("Method Implemented");
    }

    else
    {
      Console.WriteLine("Method not implemented");
    }
  }
}

Whereas the is operator returns a boolean value as shown in the preceding listing, the as operator returns null if there is any incompatibility between types. Listing 4 examines the usage of this operator. Here we have revisited Listing 3. Notice the change in code inside the Main () method.

Listing 4

using System;

interface Interdemo
{
  bool Show();
}

interface Interdemo1
{
  bool Display();
}

class Interimpas:Interdemo
{
  public bool Show()
  {
    Console.WriteLine("Show() method Implemented");
    return true;
  }

  public static void Main(string[] args)
  {
    Interimpas inter = new Interimpas();
    inter.Show();

    Interdemo1 id = inter as Interdemo1;

    if(null!=id)
    {

      bool ok = id.Display();
      Console.WriteLine("Method Implemented");
    }

    else
    {
      Console.WriteLine("Method not implemented");
    }
  }
}

Suppose you are declaring same method definitions in two different interfaces. The compiler will naturally show an error due to the ambiguity of the implemented method. Even if you use the "is" keyword, the compiler still will show warnings. To avoid this, you have to follow the syntax as shown in Listing 5:

Listing 5

Void <interface name>.<method name>
{
  //Body goes here
}

Listing 6 illustrates the application of the preceding concept in detail:

Listing 6

using System;

interface Interdemo
{
  void Show();
}

interface Interdemo1
{
  void Show();
}


class Interclash:Interdemo,Interdemo1
{
  void Interdemo.Show()
  {
    Console.WriteLine("Show() method Implemented");
  }

  void Interdemo1.Show()
  {
    Console.WriteLine("Display() method Implemented");
  }

  public static void Main(string[] args)
  {
    Interclash inter = new Interclash();
    inter.Interdemo.Show();
    inter.Interdemo1.Show();
  }
}
-------------------------------------------------------------------------------------------------------------------

About the Author

Anand Narayanaswamy works as a freelance Web/Software developer and technical writer. He runs and maintains learnxpress.com, and provides free technical support to users. His areas of interest include Web development, Software development using Visual Basic, and in the design and preparation of courseware, technical articles, and tutorials.







 

posted on 2005-11-17 17:10 夢在天涯 閱讀(571) 評論(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

搜索

  •  

積分與排名

  • 積分 - 1818845
  • 排名 - 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>
              亚洲日本成人| 亚洲天堂av在线免费| 午夜激情一区| 欧美一区二区免费观在线| 午夜亚洲福利| 欧美人成在线视频| 国产精品爱啪在线线免费观看| 欧美色精品在线视频| 国产主播一区| 亚洲精品国产精品乱码不99按摩| 夜夜躁日日躁狠狠久久88av| 久久成人精品电影| 欧美黄免费看| 亚洲女性裸体视频| 欧美福利电影网| 国产一区二区三区在线观看网站| 日韩视频二区| 欧美国产精品劲爆| 欧美一区二区三区免费大片| 国产综合视频| 亚洲精品麻豆| 国产亚洲欧美日韩日本| 亚洲国产婷婷香蕉久久久久久| 亚洲一区二区三区影院| 欧美美女喷水视频| 久久国产精品免费一区| 亚洲网在线观看| 欧美日韩精品国产| 亚洲毛片视频| 欧美一级午夜免费电影| 亚洲国产精品ⅴa在线观看 | 欧美日韩二区三区| 久久国产日韩欧美| 欧美日本三级| 麻豆久久婷婷| 香蕉视频成人在线观看| 亚洲欧洲一区二区三区在线观看 | 亚洲国产一区二区a毛片| 久久综合久久美利坚合众国| 亚洲欧美久久久久一区二区三区| 亚洲高清免费在线| 亚洲欧美精品在线| 99re6这里只有精品| 亚洲精品欧美| 欧美日韩一区高清| 亚洲一区尤物| 亚洲视频综合在线| 亚洲精品久久久久久久久久久久久| 亚洲一区二区免费在线| 99在线精品视频| 毛片精品免费在线观看| 亚洲国产免费| 久久久久国产精品一区| 在线视频观看日韩| 亚洲国产91| 欧美色欧美亚洲另类七区| 欧美二区在线看| 1024成人网色www| 久久精品一区二区三区不卡牛牛| 好看的亚洲午夜视频在线| 欧美电影免费观看大全| 国产综合视频| 久久精品国产综合精品| 久久久久国产精品一区| 欧美大片在线影院| 亚洲一区日本| 国产精品久久久久秋霞鲁丝| 久久精品99无色码中文字幕| 欧美视频不卡中文| 亚洲最新中文字幕| 国产裸体写真av一区二区| 久久久av水蜜桃| 国产亚洲精品高潮| 久久黄色网页| 午夜精品久久久久久久久久久久久| 欧美在线观看视频一区二区三区| 亚洲精品久久久久久一区二区| 久久婷婷国产综合精品青草| 亚洲一区久久久| 国产精品久久久久av免费| 嫩模写真一区二区三区三州| 国产精品美女午夜av| 午夜激情综合网| 美女精品在线观看| 国产伦精品一区二区三区免费迷 | 久热精品在线| 久久激情视频久久| 在线成人亚洲| 亚洲欧美一区二区在线观看| 欧美一区二区三区日韩视频| 欧美另类视频在线| 一区二区三区高清| 日韩午夜视频在线观看| 欧美日韩国产三区| 亚洲欧美色婷婷| 欧美高清视频在线观看| 亚洲视频免费在线| 黄色成人在线免费| 欧美三区在线视频| 久久免费99精品久久久久久| 亚洲人成网站色ww在线| 欧美一乱一性一交一视频| 在线免费不卡视频| 国产精品jizz在线观看美国| 久久精品噜噜噜成人av农村| 亚洲人成啪啪网站| 欧美一区二区大片| 亚洲久色影视| 国内揄拍国内精品少妇国语| 欧美精品激情在线| 亚洲三级电影全部在线观看高清| 亚洲欧美一区二区激情| 亚洲啪啪91| 国产一区二区精品丝袜| 欧美激情 亚洲a∨综合| 欧美有码在线观看视频| 亚洲欧洲日本一区二区三区| 亚洲精品网站在线播放gif| 国产精品视频福利| 香蕉av777xxx色综合一区| 亚洲欧洲久久| 女女同性女同一区二区三区91| 亚洲欧美日韩系列| 国产手机视频一区二区| 午夜精品久久久| 亚洲精品欧美一区二区三区| 麻豆国产精品va在线观看不卡| 亚洲男人的天堂在线aⅴ视频| 亚洲日韩第九十九页| 韩国视频理论视频久久| 国产乱码精品一区二区三区av| 欧美精品一区二区视频| 美国十次了思思久久精品导航| 欧美一级欧美一级在线播放| 亚洲视频一区二区免费在线观看| 午夜精品视频一区| 亚洲午夜视频在线| 国产一区二区在线观看免费| 国产精品久久网| 欧美日韩一区高清| 欧美色大人视频| 欧美日韩激情小视频| 欧美激情综合在线| 欧美美女操人视频| 欧美日韩精品一区二区天天拍小说 | 久久深夜福利| 久久综合影视| 麻豆久久久9性大片| 久久人人爽人人| 欧美大胆a视频| 欧美韩日一区二区| 欧美日本国产| 欧美四级电影网站| 国产麻豆午夜三级精品| 国产精品一区在线观看| 国产偷国产偷精品高清尤物| 国产亚洲成av人片在线观看桃 | 欧美日韩中国免费专区在线看| 欧美日韩伦理在线免费| 欧美午夜精品| 国产色爱av资源综合区| 国产一区二区三区久久久| 亚洲第一福利视频| 99亚洲视频| 久久成人精品| 欧美激情一区二区久久久| 亚洲人成77777在线观看网| 日韩一级片网址| 欧美国产精品一区| 亚洲乱码国产乱码精品精| 亚洲天堂成人| 久久久欧美精品sm网站| 欧美极品aⅴ影院| 国产精品自在线| 亚洲国产精品激情在线观看| 99精品国产在热久久下载| 亚洲欧美日韩另类精品一区二区三区| 久久精品免视看| 亚洲国产日韩在线一区模特| 一区二区三区免费网站| 久久精品网址| 欧美午夜精品电影| 亚洲春色另类小说| 亚洲一区二区三区精品在线| 久久女同精品一区二区| 久久成人精品无人区| 亚洲国产美女| 欧美一区二区三区免费观看| 欧美成人精品在线| 国产精品亚发布| 99国产精品久久久久老师| 欧美在线日韩精品| 最新亚洲电影| 久久国产色av| 国产精品国产三级国产aⅴ入口| 亚洲福利视频网站| 久久精品国产清高在天天线 | 亚洲国产精品久久久久秋霞不卡 | 欧美日韩免费视频| 伊人久久久大香线蕉综合直播|