码迷,mamicode.com
首页 > 其他好文 > 详细

自定义转换

时间:2015-11-17 10:53:02      阅读:138      评论:0      收藏:0      [点我收藏+]

标签:

using System;
using System.Collections.Generic;

class Person
{
public int Age;
public static explicit operator int(Person p)
{
return p.Age;
}
}


public class MyClass
{


public static void RunSnippet()
{
int i = 10;
Person p = new Person();
p.Age = 30;

i = (int) p;
Console.WriteLine(i);
}

#region Helper methods

public static void Main()
{
try
{
RunSnippet();
}
catch (Exception e)
{
string error = string.Format("---\nThe following error occurred while executing the snippet:\n{0}\n---", e.ToString());
Console.WriteLine(error);
}
finally
{
Console.Write("Press any key to continue...");
Console.ReadKey();
}
}

private static void WL(object text, params object[] args)
{
Console.WriteLine(text.ToString(), args);
}

private static void RL()
{
Console.ReadLine();
}

private static void Break()
{
System.Diagnostics.Debugger.Break();
}

#endregion
}

自定义转换

标签:

原文地址:http://www.cnblogs.com/jinweijie0527/p/4970878.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!