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.
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.
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.
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.
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.
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 of new 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 :)
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With