定义结构体

public struct Location    

{        

private int _xVal;        

private int _yVal;        

public Location(int xCoordinate, int yCoordinate)        

{           

       _xVal = xCoordinate;            

       _yVal = yCoordinate;        

}
public int xVal        

{            

get            

{               

 return xVal;           

 }            

set            

{               

 _xVal = value;            

}        

}        

public int yVal        

{            

get            

{               

 return _yVal;            

}            

set             

{               

 _yVal = value;            

}        

}        

public override string ToString()        

{            

return (String.Format("{0},{1}",_xVal,_yVal));        

}    

}

Location loct = new Location(200, 300);            

Console.WriteLine("location loct:",loct);//会调用ToString()方法

//结构体是值类型,Console.WriteLine(),输出的是一个对象,CLR自动会对loct装箱,Location重写了ToString(),会调用ToString()的方法。

个人建议Console.WriteLine("location loct:",loct.ToString());//因为装箱会是性能降低

原文地址:https://www.cnblogs.com/gull/p/1854277.html