⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ex-08-06

📁 Programming Csharp Source Code(代码) Programming Csharp Source Code
💻
字号:
//Example 08-06: References on value types

using System;

// declare a simple interface
interface IStorable
{
   void Read();
   int Status { get;set;}

}

// Implement through a struct
public struct myStruct : IStorable
{

   public void Read()
   {
      Console.WriteLine(
         "Implementing IStorable.Read");        
   }

   public int Status
   {
      get
      {
         return status;
      }
      set
      {
         status = value;
      }
   }

   private int status;
}

public class Tester
{
 
   static void Main()
   {
      // create a myStruct object
      myStruct theStruct = new myStruct();
      theStruct.Status = -1;  // initialize
      Console.WriteLine(
         "theStruct.Status: {0}", theStruct.Status);

      // Change the value
      theStruct.Status = 2;
      Console.WriteLine("Changed object.");
      Console.WriteLine(
         "theStruct.Status: {0}", theStruct.Status);
        
      // cast to an IStorable
      // implicit box to a reference type
      IStorable isTemp = (IStorable) theStruct;

      // set the value through the interface reference
      isTemp.Status = 4;
      Console.WriteLine("Changed interface.");
      Console.WriteLine("theStruct.Status: {0}, isTemp: {1}", 
         theStruct.Status, isTemp.Status);

      // Change the value again
      theStruct.Status = 6;
      Console.WriteLine("Changed object.");
      Console.WriteLine("theStruct.Status: {0}, isTemp: {1}", 
         theStruct.Status, isTemp.Status);
   }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -