© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•2y ago•
1 reply
newobject

Inferring arguments to a method

Is there any way to get C# to infer "A" here correctly?

public interface IScene<A> {
    public void Init(A args);
}


class MyType : IScene<string> {
  public void Init(string args) { ... }
}

...

public T Spawn<T, A>(PackedScene scene, A args) where T : class, IScene<A> {
    T t = scene.Instantiate<T>();
    t.Init(args);
    return t;
}

...

MyType myType = Spawn<MyType>(scene, "hi");  // compiler complains about omitting type of A here
public interface IScene<A> {
    public void Init(A args);
}


class MyType : IScene<string> {
  public void Init(string args) { ... }
}

...

public T Spawn<T, A>(PackedScene scene, A args) where T : class, IScene<A> {
    T t = scene.Instantiate<T>();
    t.Init(args);
    return t;
}

...

MyType myType = Spawn<MyType>(scene, "hi");  // compiler complains about omitting type of A here


Why doesn't it infer that A is string in this case?
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

Delegate.CreateDelegate returns "ArgumentException: method arguments are incompatible"
C#CC# / help
2y ago
Creating a method that takes a generic method and its variadic parameters as arguments and call it i
C#CC# / help
11mo ago
✅ How to get arguments from one method and pass them to another extension?
C#CC# / help
3y ago
✅ Pass in arguments to a generic constructor
C#CC# / help
2y ago