none
struct element RRS feed

  • Question

  • hi

    i want to know if there is something wrong with that struct , is the use of get/set correct?

    any tips will help.

    thanks in advance

    public struct Simple2DPolyline
        {
            Point[] Vertices { get; set; }
    
            Point StartPoint { get => Vertices.First(); }
    
            Point EndPoint { get => Vertices.Last(); }
    
    
            public Simple2DPolyline(Point[] vertices)
            {
                Vertices = vertices;
            }
        }


    • Edited by essamce Thursday, February 13, 2020 8:30 AM
    Thursday, February 13, 2020 8:30 AM

Answers

  • Hi essamce,

    You can also set Vertices as read-only.

    Point[] Vertices { get; }

    Best Regards,

    Timon


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by essamce Thursday, February 13, 2020 10:21 AM
    • Unmarked as answer by essamce Thursday, February 13, 2020 10:21 AM
    • Marked as answer by essamce Thursday, February 13, 2020 10:21 AM
    Thursday, February 13, 2020 9:48 AM

All replies

  • It seems ok but a minor adjustment you could do is make the vertices setter private as it would need to be initialized in constructor looks like. So make it :

    public struct Simple2DPolyline
    {
        Point[] Vertices { get;}
    
        Point StartPoint { get => Vertices.First(); }
    
        Point EndPoint { get => Vertices.Last(); }
    
    
        public Simple2DPolyline(Point[] vertices)
        {
            Vertices = vertices;
        }
    }


    [If a post helps to resolve your issue, please click the "Mark as Answer" of that post or click Answered"Vote as helpful" button of that post. By marking a post as Answered or Helpful, you help others find the answer faster. ]


    Blog | LinkedIn | Stack Overflow | Facebook
    profile for Ehsan Sajjad on Stack Exchange, a network of free, community-driven Q&A sites


    • Edited by Ehsan Sajjad Thursday, February 13, 2020 10:39 AM
    Thursday, February 13, 2020 9:13 AM
  • Hi essamce,

    You can also set Vertices as read-only.

    Point[] Vertices { get; }

    Best Regards,

    Timon


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by essamce Thursday, February 13, 2020 10:21 AM
    • Unmarked as answer by essamce Thursday, February 13, 2020 10:21 AM
    • Marked as answer by essamce Thursday, February 13, 2020 10:21 AM
    Thursday, February 13, 2020 9:48 AM

  •  Point[] Vertices { get; private set; }

    it gives error

    Severity Code Description Project File Line Suppression State
    Error CS0273 The accessibility modifier of the 'Simple2DPolyline.Vertices.set' accessor must be more restrictive than the property or indexer 'Simple2DPolyline.Vertices' cieStructralPlan F:\programming\01 C#\\\Simple2DPolyline.cs 14 Active


    • Edited by essamce Thursday, February 13, 2020 10:21 AM
    Thursday, February 13, 2020 10:20 AM