Is there UI-independent Point struct in .NET?

耗尽温柔 提交于 2021-02-07 04:42:24

问题


I know several Point structs in .NET: System.Drawing.Point, System.Windows.Point, Sys.UI.Point, but all of them are in high-level UI libraries (GDI+, WPF, AJAX). I need a Point struct for calculations in my class library which I don't want to tie to any specific UI technology. Is there any UI-independent Point struct in .NET? Or I will need to create it myself? That is simple, I know, but sounds like reinventing the wheel.


回答1:


Reinvent the wheel. It will run smoother! Really, if it's just a tiny struct why depend on big assemblies, pulling in a lot of other stuff? Especially on constraint devices like phones... But pay attention on how to use classes and struct correctly if you want best performance.

This is a pretty good read and I sense that you want to read it dearly: Frank Savage on CLR performance.




回答2:


To the best of my knowledge there isn't, but as you stated it isn't something hard to implement yourself so I suggest you do that.

You may be tempted to use Tuple class as others suggested. While it can do the job it isn't something you'll want to reuse over and over. Furthermore you may run into comparison issues, depending on your app specifics.




回答3:


You can also use Tuple class for that purpose.




回答4:


You might want to use System.Drawing.Point or System.Drawing.PointF anyway. True, it can be converted to and from the appropriate GDI+ structure, but in itself, it's simply a .NET structure. It's pure managed struct, that just happens to be COM visible and convertible :)




回答5:


If you don't want to create your own Point class, you could consider using the Tuple class MSDN link. You could pass along a pair of coordinates of whatever type you want to your class library instead of forcing a user to instantiate a new class.

If you want to provide several helper methods to the consumer of your library, however, I would create your own class. Tuples are great for quick ways to pass around data.



来源:https://stackoverflow.com/questions/21531084/is-there-ui-independent-point-struct-in-net

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!