Type Inference Examples

Ahh type inferece. If you do any searching around stacktrace, code project, slashdot, etc.., you will find plenty of arguments for using strong typed variables or using the var keyword when declaring local variables. I’ve recently been using var keyword more in my code after reading C# In-Depth by Jon Skeet. The C# compiler does a good job if making sure things are safe at compile time. For example var stringList = new List<string>() is the same as List<string> stringList = new List<string>(). So it all comes down to personal preference when using C# .NET.

TypeInferenceSolution TypeInferenceProgram

 

Program.cs Code Contents

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Reflection;
using System.Xml.Linq;
using System.Diagnostics;

namespace MyLinq
{
    class Program
    {
        /// <summary>
        /// Demonstrating examples of type inference in the C# .NET language.
        /// </summary>
        /// <param name="args"></param>

        static void Main(string[] args)
        {
            //comprehensionQuerySyntax();
            //extensionMethodSyntax();
            typeInferenceExamples();
            moreInferenceExamples();

            Console.ReadLine();
        }

        //Private Methods
        //
        private static void typeInferenceExamples()
        {
            Console.WriteLine("****Running Some Type Inference Examples");

            // Sometimes I'm finding myself declaring my variables in two ways. One way is strongly typed and the other
            // with the var keyword. While there are certain loose rules around the way you declare variables it eventually
            // comes down to your coding style and/or policies.
            
            // One way to declare since I know what the type is going to be. You can see that at compile time
            // the data type is known.
            var varMovieReviewContext = new MovieReviewsDataContext();
            Console.WriteLine("Var movieReviewContext type is: {0}", varMovieReviewContext.GetType());

            // Another way is to strongly declare the type.
            MovieReviewsDataContext strongMovieReviewDataContext = new MovieReviewsDataContext();
            Console.WriteLine("Strong movieReviewDataContext type is: {0}", strongMovieReviewDataContext.GetType());

            Console.WriteLine();
        }

        private static void moreInferenceExamples()
        {
            // More examples and some functionality being applied.
            var varMovieReviewContext = new MovieReviewsDataContext();
            MovieReviewsDataContext strongMovieReviewDataContext = new MovieReviewsDataContext();

            var varMovieReviews = varMovieReviewContext.Movies.Where(r => r.ReleaseDate >= 2009);
            IEnumerable<Movie> strongMovieReviews = strongMovieReviewDataContext.Movies.Where(r => r.ReleaseDate >= 2009);
            Console.WriteLine("Var movieReviews type is: {0}", varMovieReviews.GetType());
            Console.WriteLine("Strong movieReviews type is: {0}", strongMovieReviews.GetType());

            var employees = createEmployeeArray();
            Console.WriteLine("Var employees type is: {0}", employees.GetType());

            Console.WriteLine();
        }

        private static Employee[] createEmployeeArray()
        {
            Employee[] employees = new Employee[]
            {
                new Employee { ID = 1, Name = "Daniel", HireDate = new DateTime(2015, 10, 15) },
                new Employee { ID = 2, Name = "Jeff", HireDate = new DateTime(2010, 12, 10) },
                new Employee { ID = 3, Name = "Sara", HireDate = new DateTime(2001, 1, 22) },
                new Employee { ID = 4, Name = "Mike", HireDate = new DateTime(2011, 4, 12)}
            };
            return employees;
        }
    }
}

Like I described earlier. I’ve created two methods demonstrating some examples of type inference. I’m showing examples and proving that it doesn’t matter if you strongly type your variables or use the var keyword. Once the code is compiled the compiler does a good job of figuring things out.

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

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

Google photo

You are commenting using your Google 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