自定義轉換

using System;
using System.Collections.Generic;orm

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


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();
}
}ci

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();
}string

#endregion
}it

相關文章
相關標籤/搜索