Tag Archives: C#

C# Explicit and Implicit Variable Typing

There are typically multiple ways to do something in a programming language. And with C# there are two ways to set the type of a variable or object — explicitly and implicitly. Here is some code to demonstrate the two ways. I’ll explain a bit more after the code.

using System;

namespace ExplicitImplicitTyping
{
    class Program
    {
        static void Main(string[] args)
        {
            // Examples of EXPLICITLY setting the type of variables and objects
            string myString = "This is a string of characters";
            int myInteger = 12345;
            double myDouble = 123.45;
            Car myCar = new Car();

            // Examples of IMPLICITLY setting the type of variables and objects
            var mySecondString = "This is a string of characters";
            var mySecondInteger = 12345;
            var mySecondDouble = 123.45;
            var mySecondCar = new Car();
        }
    }

    class Car
    {
        public string Make { get; set; }
    }
}

 

Notice that the only difference between the two sets of declarations is that with the explicit declarations you use the type keywords (string, int, double, etc.), but when implicitly declaring the variables you use the var keyword.

So what’s going on here? When you explicitly declare a variable’s type you are telling the compiler which type to use for that variable so it can set aside a space in memory that is large enough to hold that variable. When you use the var keyword you are making the compiler figure out the type for you. If the compiler were human, and spoke English, it would say something like, “Ok, I see you’ve created a variable with the name of myString. And I see that you’ve set its value to a bunch of characters between two double quotes. Based on what I’ve been programmed to do, that fits the definition of a string. So I’m going to declare your myString variable as type string.”

My understanding is that the var keyword was implemented in C# for no other reason than to speed up code generation. “Hey, you’re using Visual Studio, so you must know how to program and set your variables, so we’re going to let the compiler do this little step for you by using the var keyword so you can get on to other things.”

One last thing. According to the MSDN documentation, an implicitly typed variable using the var keyword “…is strongly typed just as if you had declared the type yourself…”. That means that once the compiler determines and sets the variable type, the type will not change unless you cast it to another type. The is different than some other programming languages.

Happy coding!

Getting a C# Start

To learn anything knew, you have to start with the basics. And to develop for Microsoft technologies a good place to start is learning the C# programming language. Below you’ll find a couple of video resources that I personally have found to be very good. These aren’t the only beginner resources available, but I’ve enjoyed them, have learned from them, and highly recommend them.

If you’ve never programmed a single line of code before, don’t worry. You have a lot to learn, but it’s doable. Just remember that a programming language is no different than a human spoken language. You learn it one word (keyword) at a time with lots and lots and lots of repetition. And so it goes with programming. First you’ll learn the keywords and the syntax (the punctuation) and the mathematical operators (just like regular math). Then you’ll learn how to write lines of code (the sentences), and then blocks of code (the paragraphs), etc. Pretty soon you’ll be writing decent sized programs that do something fun and cool and useful.

My point is to not get hung up on everything you have to learn. Programming languages have gotten large and complex, but only because they have become so capable. You didn’t learn to write a best-selling book when you were 4 years old and you’re not going to learn to write the next operating system in your first year of programming. So give yourself a break. Sure, learn as quickly as you can, but don’t feel like you have to be a master programmer in a few weeks, months, or years. It’s not going to happen. Be realistic! Otherwise you’ll get discouraged and give up because you’ll convince yourself you can’t do it. And that’s utter nonsense. You CAN do it! And you WILL do it, if you stick with us here at WinDevsDotNet!

Personally, I learn best by listening, seeing, and doing. So here are two video courses to get you started. These are from Bob Tabor, the founder of LearnVisualStudio.Net. Lucky for us Microsoft has apparently contracted with Bob to offer a few of his courses for free. I hope they paid him!

C# Fundamentals for Absolute Beginners at the Microsoft Virtual Academy. I’m in the process of taking this course and so far it’s been great. Bob does a nice job of taking it slow and pointing out the “hows” and “whys”. He assumes you know absolutely nothing about programming. This series was recorded towards the end of 2013 and posted in early 2014.

C# Fundamentals: Development for Absolute Beginners over on Channel9 is also from Bob Tabor. This series is similar to the one above, but appears to have been recorded back in 2011. I remember going through this series a couple of years ago and really liking it. So I don’t think you can go wrong with either of these.

Check back soon for more learning resources. I am in the process of developing a learning plan for you to keep you (and me) on track. Comment below with any questions or comments, or follow us on Twitter and tweet away.