C#: Type Inference

Please see my other C# articles.

Writing flexible, elegant code for data storage.

When writing business logic, it’s often difficult to determine at design-time what type an object will need to be or what type of data it will store.
Thankfully, Microsoft has provided an elegant solution by allowing developers to write code that will store any type of data without declaring its type.

To illustrate this feature, I’ll begin with a simple array of string objects, but notice that aside from that type of data used, no where do I explicitly state that the “localVar” variable is of a String type.
Upon executing my controller which calls the default Index() view, you will see the compiler correctly deduced the variable’s data type as String and displayed the results.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s