dabcpp
dabcpp

Reputation: 3

C# group multiple structs and pass to function parameter?

I use a namespace with many structs inside, I have a method that I want to call, currently I am using "object" type as parameter to pass all my structs from the namespace (I am aware that I could make parent class to child class and use parent type as a filter, but I am interested into possibility of structs).

Is there a way to limit the passed Objects to only types of my namespace?

Thanks for any advice (I am a beginner).

Upvotes: 0

Views: 243

Answers (1)

Jesús López
Jesús López

Reputation: 9221

There is no way to limit by namespace, however you can limit the parameter type if you use generics and constraint them by interface. For example:

public interface MyInterface {}

public struct MyStruct : MyInterface {}

public struct MyOtherStruct: MyInterface {}

public class MyClass
{
   public void MyMethod<T>(T myParameter) where T: MyInterface
   {
   }
}

Upvotes: 1

Related Questions