1
    public struct S1
    {
        public int MyProperty { get; set; }
        public int MyProperty2 { get; set; }
    }

    public struct S2
    {
        public int MyProperty3 { get; set; }
        public int MyProperty4 { get; set; }
    }

I have two different structs namely S1,S2. Can someone add extension method without writing the metod for both of the structs? I mean I just want to write one-method and all structs will have this extension method. To me, since structs cannot be inherited by some special class, there is no solution. Any ideas, or do I think right- there is no way?

  • This question's answer shows how: https://stackoverflow.com/a/29109671/119549. Basically your extension method can use a generic type. – Jacob Aug 03 '18 at 22:31
  • @Jacob Yes exact answer for my question. Thanks. – Mehmet Erkol Aug 03 '18 at 22:34
  • Since there's not really an answer in that link that applies to every struct at once, I'll go ahead and include this here in case anyone else comes looking: to have the extension method apply to *all* structs, use something like this: `static void Foo(this T target) where T : struct`. You'll need to use a constraint to prevent it from working on non-struct types. – TehPers Aug 03 '18 at 22:50

0 Answers0