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

C++ Programmer's Cookbook

{C++ 基礎(chǔ)} {C++ 高級(jí)} {C#界面,C++核心算法} {設(shè)計(jì)模式} {C#基礎(chǔ)}

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 夢(mèng)在天涯 閱讀(569) 評(píng)論(0)  編輯 收藏 引用 所屬分類: C#/.NET

公告

EMail:itech001#126.com

導(dǎo)航

統(tǒng)計(jì)

  • 隨筆 - 461
  • 文章 - 4
  • 評(píng)論 - 746
  • 引用 - 0

常用鏈接

隨筆分類

隨筆檔案

收藏夾

Blogs

c#(csharp)

C++(cpp)

Enlish

Forums(bbs)

My self

Often go

Useful Webs

Xml/Uml/html

搜索

  •  

積分與排名

  • 積分 - 1814985
  • 排名 - 5

最新評(píng)論

閱讀排行榜

青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品
  • <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>
              亚洲一区二区三区成人在线视频精品 | 国产原创一区二区| 久热精品视频在线| 香蕉久久夜色精品国产使用方法| 正在播放欧美视频| 午夜视频在线观看一区二区三区| 亚洲欧美日韩国产另类专区| 亚洲欧美日韩国产成人| 欧美一区二区啪啪| 久久婷婷国产麻豆91天堂| 免费国产一区二区| 欧美久久影院| 国产精品久久久久影院色老大| 国产精品一区毛片| 今天的高清视频免费播放成人| 激情欧美一区二区三区在线观看| 一区视频在线| 一本色道久久综合一区 | 久久精品国产v日韩v亚洲| 久久久五月婷婷| 欧美日本在线视频| 国产精品毛片在线看| 黄色综合网站| 一区二区精品在线观看| 国产一区二区三区免费在线观看| 亚洲国产精品悠悠久久琪琪| 99在线热播精品免费| 亚洲欧美日韩国产一区| 亚洲综合国产| 欧美福利视频在线| 91久久精品www人人做人人爽| 欧美精品一区二区精品网| 久久久久久综合| 久久久91精品国产一区二区三区 | 欧美激情第4页| 亚洲一级黄色| 久久av一区二区| 欧美激情一区二区三区成人| 亚洲精品视频免费观看| 久久漫画官网| 国产日韩精品一区二区| 99精品视频一区| 久久综合综合久久综合| 妖精成人www高清在线观看| 老色鬼久久亚洲一区二区 | 欧美激情亚洲精品| 亚洲国产精品悠悠久久琪琪| 久久久久88色偷偷免费| 在线视频日韩| 欧美日韩三区| 一二三四社区欧美黄| 亚洲高清自拍| 久久精品国产清高在天天线| 国产精品色在线| 亚洲综合色在线| 夜夜嗨av一区二区三区网站四季av | 亚洲看片一区| 久久av一区| 亚洲一区日韩在线| 欧美日韩一区二区三区在线| 亚洲电影在线看| 久久久精品日韩| 亚洲国产日韩在线一区模特| 蜜臀久久99精品久久久久久9| 亚洲国产一二三| 亚洲欧美日韩国产综合| 久久99伊人| 久久亚洲春色中文字幕久久久| 亚洲免费婷婷| 久久久亚洲精品一区二区三区| 免费一区二区三区| 国产精品av免费在线观看| 国产日本欧美一区二区三区| 在线电影院国产精品| 亚洲一区国产视频| 欧美插天视频在线播放| 亚洲私人影院| 欧美+亚洲+精品+三区| 国产一区二三区| 亚洲一区二区三区中文字幕在线| 久久伊伊香蕉| 亚洲欧美另类中文字幕| 免费短视频成人日韩| 国产精品丝袜91| 国产精品你懂的在线| 久久精品国产99精品国产亚洲性色 | 亚洲精品一区二区三区四区高清 | 久久成人免费视频| 亚洲麻豆av| 你懂的国产精品永久在线| 韩国av一区二区| 久久狠狠一本精品综合网| 日韩一级网站| 欧美国产精品日韩| 亚洲国产精品va| 毛片精品免费在线观看| 欧美日韩在线精品一区二区三区| 亚洲精品一级| 亚洲二区在线视频| 蜜桃av久久久亚洲精品| 1769国产精品| 欧美大片专区| 蜜桃伊人久久| 亚洲精品一二三| 亚洲日韩中文字幕在线播放| 久久国产精品色婷婷| 一本色道久久| 一区二区精品在线| 国产精品视频久久一区| 亚洲精品国久久99热| 亚洲精品女人| 久久久蜜桃精品| 伊人久久久大香线蕉综合直播| 99在线精品视频在线观看| 久久国产婷婷国产香蕉| 最新日韩在线| 欧美日韩精品久久久| 一本一道久久综合狠狠老精东影业 | 久热国产精品| 久久婷婷亚洲| 狠狠色2019综合网| 亚洲国产成人高清精品| 免费国产一区二区| 妖精视频成人观看www| 亚洲毛片播放| 国产精品人人爽人人做我的可爱| 午夜精品久久久久99热蜜桃导演| 亚洲综合色在线| 亚洲电影有码| 亚洲精品视频免费观看| 日韩五码在线| 亚洲欧美激情一区二区| 一区二区三区在线视频免费观看 | 久久精品视频在线| 亚洲影院高清在线| 欧美怡红院视频一区二区三区| 久久精品日韩欧美| 亚洲剧情一区二区| 亚洲网站啪啪| 亚洲国产合集| 亚洲小说欧美另类社区| 激情六月婷婷综合| 亚洲蜜桃精久久久久久久| 国产精品任我爽爆在线播放| 一本一本久久a久久精品综合妖精 一本一本久久a久久精品综合麻豆 | 国产精品vip| 蜜臀91精品一区二区三区| 欧美高清在线视频| 久久国产福利| 欧美精品一区在线| 久久久久久久久久码影片| 欧美日韩精品免费观看视频| 亚洲人成在线观看| 99精品99| 国产女人水真多18毛片18精品视频| 欧美激情网站在线观看| 99亚洲视频| 欧美金8天国| 亚洲成人自拍视频| 国产精品一区二区你懂得| 午夜精品999| 免费的成人av| 亚洲一区二区三区在线| 美女脱光内衣内裤视频久久影院| 午夜在线观看欧美| 欧美午夜视频在线| 欧美国产一区二区| 国产综合视频| 亚洲欧美久久久| 午夜国产精品影院在线观看| 亚洲免费影院| 亚洲国产成人精品女人久久久 | 欧美日韩精品在线播放| 美日韩在线观看| 狠狠狠色丁香婷婷综合激情| 亚洲欧美国产精品专区久久| 中文日韩电影网站| 欧美日韩国产精品一卡| 亚洲黄色一区二区三区| 欧美视频1区| 久久这里只精品最新地址| 久久精品国产清自在天天线| 国产精品一区视频网站| 亚洲在线视频免费观看| 亚洲欧美欧美一区二区三区| 欧美日韩视频免费播放| 一道本一区二区| 亚洲欧美不卡| 久久久久久午夜| 欧美成人精品影院| 亚洲精品久久久久久久久久久 | 亚洲欧美日本视频在线观看| 国产一区二区三区观看| 久久精品国产99精品国产亚洲性色| 久久久噜噜噜久久久| 亚洲国产日本| 欧美日韩成人在线播放| 亚洲视频在线观看视频| 欧美怡红院视频一区二区三区| 欧美成人午夜免费视在线看片|