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

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

搜索

  •  

積分與排名

  • 積分 - 1814985
  • 排名 - 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精品国产一区二区三区| 亚洲精品社区| 亚洲日本无吗高清不卡| 亚洲第一在线综合网站| 精品成人一区二区三区| 国产自产v一区二区三区c| 国产欧美视频一区二区三区| 国产精品久久影院| 国产日韩欧美日韩大片| 一本色道久久综合亚洲精品不| 国产精品av免费在线观看| 欧美日韩性视频在线| 亚洲国产精品热久久| 国产精品久久久一区二区| 国产精品国产三级国产a| 国产精品你懂的在线欣赏| 国产亚洲视频在线| 亚洲国产成人porn| 亚洲一区二区四区| 久久国产精品毛片| 欧美成人嫩草网站| 一区二区三区日韩精品视频| 亚洲欧美日韩精品久久久| 欧美一区视频| 欧美激情国产精品| 国产欧美一区二区精品仙草咪| 伊人色综合久久天天| 亚洲最新在线| 久久亚洲春色中文字幕| 亚洲日本一区二区| 欧美一区二区三区日韩视频| 欧美国产精品v| 国产日韩欧美精品一区| 日韩写真在线| 久久久久久9999| 亚洲精品欧美日韩| 久久精品亚洲一区二区三区浴池| 欧美黄色成人网| 国内外成人免费视频| 99伊人成综合| 美女日韩欧美| 亚洲在线免费| 欧美激情亚洲自拍| 精品动漫一区| 欧美在线视频一区| 99国产精品自拍| 久久久综合免费视频| 国产精品久久久久久久久久久久久久 | 欧美大片免费| 欧美亚洲视频在线观看| 欧美网站在线| 一本一本久久a久久精品综合麻豆 一本一本久久a久久精品牛牛影视 | 老司机一区二区三区| 国产日韩亚洲欧美综合| 亚洲免费在线视频| 亚洲日本中文字幕免费在线不卡| 亚洲黄色影院| 久久久久久久国产| 亚洲欧美日韩高清| 国产精品美女xx| 中文在线不卡| 日韩一区二区久久| 欧美日本免费| 欧美天天在线| 亚洲国产成人在线| 欧美jizzhd精品欧美喷水| 欧美伊人精品成人久久综合97| 国产精品视频免费观看| 亚洲欧美日韩精品久久亚洲区 | 欧美亚洲免费| 亚洲欧美日韩精品久久久久| 国产精品久久久久免费a∨| 亚洲视频专区在线| 亚洲深夜激情| 国产女人aaa级久久久级| 久久成人国产| 久久久久久伊人| 亚洲欧洲偷拍精品| 亚洲国产导航| 欧美日韩不卡合集视频| 国产精品99久久久久久宅男| 一本一本久久| 国产伦精品一区二区三区在线观看 | 国产精品亚洲综合天堂夜夜| 亚洲香蕉伊综合在人在线视看| 日韩一区二区精品| 国产精品日韩欧美| 久久免费99精品久久久久久| 久久精品人人做人人综合| 影音先锋日韩资源| 亚洲国内精品| 国产精品欧美一区二区三区奶水| 久久福利毛片| 欧美福利电影在线观看| 亚洲欧美日韩国产成人精品影院| 欧美亚洲视频| 夜夜爽夜夜爽精品视频| 亚洲免费在线播放| 在线不卡亚洲| aⅴ色国产欧美| 国产一区二区精品久久| 亚洲国产99| 国产视频观看一区| 91久久精品国产| 国产专区精品视频| 亚洲精品视频在线播放| 国产一级久久| 99视频一区| 亚洲第一福利在线观看| 国产精品99久久久久久人 | 国产精品久久久久久影视| 亚洲视频在线播放| 久久久久这里只有精品| 午夜精品一区二区三区在线播放| 老司机免费视频一区二区三区| 亚洲综合国产精品| 欧美黄网免费在线观看| 亚洲高清资源| 亚洲自拍三区| av不卡在线看| 久久综合色婷婷| 欧美在线不卡| 国产精品高潮呻吟久久| 欧美成熟视频| 亚洲高清不卡在线| 久久精品中文| 美女视频黄 久久| 在线观看成人一级片| 久久狠狠婷婷| 老司机67194精品线观看| 国外精品视频| 久久久美女艺术照精彩视频福利播放| 久久精品国产清高在天天线 | 一区二区不卡在线视频 午夜欧美不卡在| 欧美一区二区三区男人的天堂| 亚洲欧美电影在线观看| 欧美视频一区在线| 一本色道久久加勒比88综合| 正在播放亚洲一区| 欧美日韩精品一区二区天天拍小说 | 久久久噜噜噜久久| 久久综合伊人77777| 国产精品久久福利| 亚洲韩国青草视频| 好吊成人免视频| 欧美亚洲一区| 亚洲欧美视频一区| 久久天堂国产精品| 欧美激情片在线观看| 亚洲精品欧美极品| 国内精品美女在线观看| 卡通动漫国产精品| 欧美影院久久久| 欧美激情精品久久久六区热门| 久久久精品999| 亚洲第一精品夜夜躁人人爽 | 亚洲国产日韩美| 国产精品久久久久久久app| 美脚丝袜一区二区三区在线观看| 亚洲国产美女| 欧美一区二区三区免费在线看| 日韩视频在线免费| 久久久噜噜噜久久中文字免| 亚洲欧美制服另类日韩| 狠狠干成人综合网| 亚洲小说欧美另类社区| 日韩午夜av电影| 乱码第一页成人| 亚洲图色在线| 亚洲婷婷国产精品电影人久久| 噜噜噜久久亚洲精品国产品小说| 久久成年人视频| 国产亚洲va综合人人澡精品| 亚洲视频在线观看网站| 欧美午夜无遮挡| 美女网站在线免费欧美精品| 久久久久久亚洲精品中文字幕 | 卡一卡二国产精品| 99v久久综合狠狠综合久久| 欧美成人精品在线观看| 免费日韩一区二区| 在线观看日韩av先锋影音电影院| 欧美日韩午夜视频在线观看| 亚洲精品之草原avav久久| 日韩天天综合| 欧美日韩免费| 免费黄网站欧美| 亚洲国产视频直播| 99国内精品| 国产精品国产一区二区| 欧美大片免费观看| 亚洲乱码国产乱码精品精| 在线中文字幕日韩| 国产精品美女视频网站| 欧美日韩中文字幕在线| 在线视频你懂得一区二区三区| 香蕉久久精品日日躁夜夜躁| 国产欧美韩国高清|