How good is C# type inference? I read somewhere that it’s only for local variables? Does it work for class level attributes? For method signatures? Method return types? etc.
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
There are a few main kinds of type inference in C#:
Implicitly typed local variables:
Generic method type argument inference, i.e. you don’t specify the type arguments in a call to a generic method, the compiler figures them out based on the arguments.
Lambda expression parameter type inference
Array type inference, e.g.
new[] { 'Hi', 'there' }instead ofnew string[] { 'Hi', 'there' }I’ve probably forgotten some other features which might be called ‘type inference’. I suspect you’re mostly interested in the first, but the others might be relevant to you too 🙂