找回密码
 立即注册

QQ登录

只需一步,快速开始

Arthas

葡萄城公司职员

8

主题

296

帖子

5140

积分

葡萄城公司职员

积分
5140

活字格认证

Arthas
葡萄城公司职员   /  发表于:2011-4-1 11:05  /   查看:5828  /  回复:5
Arthas说:要有优雅。
于是便有了优雅。

tbugs说:
要有能指向自身类型的struct
于是就有了指向自身类型的struct。

struct可以实现接口的。 可以绕过那个该死的编译器限制。
试试是否符合你的要求。

参见:
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Diagnostics;

  6. namespace WriteForTBugs
  7. {
  8.     class Program
  9.     {
  10.         static void Main(string[] args)
  11.         {
  12.             Point parent = new Point()
  13.             {
  14.                 Width = 5,
  15.                 Height = 3
  16.             };
  17.             Point child = new Point()
  18.             {
  19.                 Width = 7,
  20.                 Height = 8,
  21.                 parent =  parent
  22.             };

  23.             Console.WriteLine(child.parent.Width);
  24.             Console.WriteLine(child.parent.Height);
  25.             
  26.             Point[] source = new Point[] { parent};
  27.             Point[] aim = new Point[1];
  28.             Array.Copy(source, aim, 1);
  29.             parent.Width = 7;
  30.             parent.Height = 7;
  31.             Debug.Equals(aim[0].Width, 5);
  32.             Debug.Equals(aim[0].Height, 3);
  33.         }
  34.     }

  35.     public interface IPoint
  36.     {
  37.         int Width
  38.         {
  39.             get;
  40.             set;
  41.         }
  42.         int Height
  43.         {
  44.             get;
  45.             set;
  46.         }
  47.     }
  48.     public  struct Point : IPoint
  49.     {
  50.         public int Width
  51.         {
  52.             get;
  53.             set;
  54.         }
  55.         public int Height
  56.         {
  57.             get;
  58.             set;
  59.         }

  60.         public IPoint parent
  61.         {
  62.             get;
  63.             set;
  64.         }
  65.     }
  66. }
复制代码
扯淡第一高手

5 个回复

倒序浏览
tbugs
中级会员   /  发表于:2011-4-1 13:31:00
沙发

回复 1# Arthas 的帖子

做人就需要做一个有激情的人~我认为arthas绝对是一个激情澎湃的热血好青年,你这个东西,我需要晚上回去测一下,哈哈~很感谢提供线索。
路,在此绽放。
回复 使用道具 举报
robert
金牌服务用户   /  发表于:2011-4-1 13:40:00
板凳
学习了!
原来这样也可以。
回复 使用道具 举报
neil
论坛元老   /  发表于:2011-4-2 15:46:00
地板

回复 1# Arthas 的帖子

这样虽然表面可以,但实际上会造成装箱和拆箱,
这会造成两个问题,第一是性能问题,第二,比较罕见,如果你试图在child上改变parent的width,你会发现实际的parent不能改成功。
举个例子:
Point parent = new Point()
            {
                Width = 5,
                Height = 3
            };
            Point child = new Point()
            {
                Width = 7,
                Height = 8,
                parent =  parent
            };

然后写一句: child.parent.Width = -999;
然后查看:child.parent.Width 是-999,  但是查看parent.width还是5


第二个问题虽然比较罕见,但是第一个性能问题可能比较棘手。
回复 使用道具 举报
Arthas
葡萄城公司职员   /  发表于:2011-4-2 18:13:00
5#

回复 4# neil 的帖子

第二个问题没有重现出来。
求步骤。
扯淡第一高手
回复 使用道具 举报
neil
论坛元老   /  发表于:2011-4-2 21:37:00
6#

回复 5# Arthas 的帖子

main  函数这样写:

static void Main(string[] args)
        {
            Point parent = new Point()
            {
                Width = 5,
                Height = 3
            };
            Point child = new Point()
            {
                Width = 7,
                Height = 8,
                parent = parent
            };


            child.parent.Width = -999;

            Console.WriteLine(child.parent.Width); // -999
            Console.WriteLine(parent.Width); // 5

           
        }
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 立即注册
返回顶部