#638 – Defining and Using a Partial struct

In addition to classes, structs can also be partial, and contain partial methods.  Just like partial classes, a partial struct is one that is split across multiple files.  A partial method in a partial struct is a method that is declared in one portion of the struct and, optionally, implemented in another.

Like a partial method in a class, a partial method in a struct is implicitly private.

    // Dog-1.cs
    public partial struct DogDimensions
    {
        public double Height, Width, TailLength;

        partial void CustomPrintDimensions();
    }

 

    // Dog-2.cs
    public partial struct DogDimensions
    {
        partial void CustomPrintDimensions()
        {
            Console.WriteLine(
                string.Format("Dog is {0} in high, {1} in wide, and has a tail that's {2} in long",
                    Height, Width, TailLength));
        }

        public void DumpDimensions()
        {
            CustomPrintDimensions();
        }
    }

 

            DogDimensions dims;
            dims.Height = 14.0;
            dims.Width = 8.0;
            dims.TailLength = 24.0;
            dims.DumpDimensions();