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());//因为装箱会是性能降低

相关文章:

  • 2021-09-13
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-12-25
  • 2022-01-28
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-10-20
  • 2021-08-29
  • 2021-06-26
  • 2022-12-23
  • 2022-12-23
  • 2021-11-23
相关资源
相似解决方案