Type Inference in C#

Types of Inference in C#

C# is a strongly typed language, and the default type declaration is explicit type. For example, the following code snippet declares and initializes two variables. The first variable, name, is a string-type variable, and the second variable, age, is an integer-type variable.

string name = "Mahesh Chand"; int age = 25;

If I add this line of code below the code above

name = 32;

Then the compiler throws the following error.

Cannot implicitly convert type 'int' to 'string'.

C# 3.0 introduced a var keyword that can be used to declare implicit types. The purpose of implicit types is to store any type in a variable.

The following code snippet declares an implicit variable that stores a string.

var name = "Mahesh Chand";

Once an implicit type is defined, the compiler knows the type of the variable. You cannot assign another type of value to it. For example, if we write this code.

name = 25;

Then the compiler throws the following error.

Cannot implicitly convert type 'int' to 'string'.

In C#, the var keyword tells the compiler to use the type inference to determine the type of a variable. Type inference is heavily used in LINQ queries, so any type can be stored in the variable.

The following is the Author's class.

public class Author
{
    public string Name { get; set; }      // Author's name
    public string Book { get; set; }      // Book title
    public string Publisher { get; set; } // Publisher of the book
    public Int16 Year { get; set; }       // Year the book was published
    public double Price { get; set; }     // Price of the book
}

The following code creates a list of authors and uses LINQ to query the authors list.

List<Author> authorsList = new List<Author>
{
    new Author
    {
        Name = "Mahesh",
        Book = "ADO.NET Programming",
        Publisher = "Wrox",
        Year = 2007,
        Price = 44.95
    },
    new Author
    {
        Name = "Raj",
        Book = "LINQ Cookbook",
        Publisher = "APress",
        Year = 2010,
        Price = 49.95
    },
    new Author
    {
        Name = "Praveen",
        Book = "XML Code",
        Publisher = "Microsoft Press",
        Year = 2012,
        Price = 44.95
    }
};

var authorQuery = from author in authorsList
                  where author.Price == 44.95
                  select new { author.Name, author.Publisher };

foreach (var author in authorQuery)
{
    Console.WriteLine("Name={0}, Publisher={1}", author.Name, author.Publisher);
}

Summary

C# is a strongly typed language, but in C# 3.0, a new feature was introduced to minimize the impact of being a strongly typed language. The feature is called type inference, and the keyword used is the "var" keyword. In this article, we learned what type inference is and how it may be used in your code.


Similar Articles
Mindcracker
Founded in 2003, Mindcracker is the authority in custom software development and innovation. We put best practices into action. We deliver solutions based on consumer and industry analysis.