public class SampleClass { public int value; public SampleClass(int v) { value = v; } } // i want to access value like this SampleClass sc = new SampleClass(5); int i = sc;
有没有办法在C#中做到这一点?每次我需要访问该值时,我不想说sc.Value.@H_404_5@
解决方法
使用
implicit转换:
public class SampleClass { public int value; public SampleClass(int v) { value = v; } public static implicit operator int (SampleClass c) { return c.value; } }
你应该看看properties.@H_404_5@