c# Convert struct to another struct

后端 未结 3 1989
别那么骄傲
别那么骄傲 2020-12-19 08:12

Is there any way, how to convert this:

namespace Library
{
    public struct Content
    {
        int a;
        int b;
    }
}

I have str

3条回答
  •  不思量自难忘°
    2020-12-19 08:35

    You have several options, including:

    • You could define an explicit (or implicit) conversion operator from one type to the other. Note that this implies that one library (the one defining the conversion operator) must take a dependency on the other.
    • You could define your own utility method (possibly an extension method) that converts either type to the other. In this case, your code to do the conversion would need to change to invoke the utility method rather than performing a cast.
    • You could just new up a Library2.Content and pass in the values of your Library.Content to the constructor.

提交回复
热议问题