Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How good is the C# type inference?

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.

like image 347
thr Avatar asked Jan 26 '09 14:01

thr


People also ask

Is C better than C+?

C++ is a more object-oriented high-level programming language which requires fixed construction and principles. However, it is easier to code. C programming language does not adhere to the encapsulation concept and allows easy data manipulation from outside code. C++ is a more secure programming language.

How useful is C?

Being a middle-level language, C reduces the gap between the low-level and high-level languages. It can be used for writing operating systems as well as doing application level programming. Helps to understand the fundamentals of Computer Theories.

Why is C the best?

The C programming language doesn't seem to have an expiration date. It's closeness to the hardware, great portability and deterministic usage of resources makes it ideal for low level development for such things as operating system kernels and embedded software.

Why is C so popular and powerful?

The C programming language is so popular because it is known as the mother of all programming languages. This language is widely flexible to use memory management. C is the best option for system level programming language.


2 Answers

There are a few main kinds of type inference in C#:

  • Implicitly typed local variables:

    • Only for local variables
    • Only when the value is assigned as part of the declaration
    • Value cannot be null
    • Value cannot be a lambda expression, anonymous method or method group (without a cast)
    • The compile-time type of the value is used for the type of the variable
    • Any further uses of the variable are only checked against the type determined by the initial declaration+assignment; they don't contribute to the inference itself.
  • 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.

    • Would be really handy to have this for generic types as well as generic methods
    • Really handy anyway - LINQ would be hard or impossible to use without it
    • Anonymous types would be fairly useless without it
    • Really complicated rules, even the spec is wrong in a few places
  • Lambda expression parameter type inference

    • Compiler tries to work out the types of the parameters for lambda expressions based on the context in which it's used
    • Usually works pretty well, in my experience
  • Array type inference, e.g. new[] { "Hi", "there" } instead of new string[] { "Hi", "there" }

    • Various small restrictions, nothing major

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 :)

like image 81
Jon Skeet Avatar answered Oct 08 '22 00:10

Jon Skeet


It can only be used for local variables, but it can detect the type in many different forms.

var myVar = SomeMethodThatReturnsInt(); //will know it's an int
var myIntList = new List<int>(); //this works too (although this is technically not type inference)
var myOwnVar = new { Name = "John", Age = 100 }; // will create own type and infer that

EDIT: One more example of Tye Inference is with Lambdas. IE:

var myList = new List<int>();
//add some values to list

int x = myList.Find(i => i == 5); // compiler can infer that i is an int.
like image 42
BFree Avatar answered Oct 07 '22 23:10

BFree