Hi everybody,
I am a newbie in C#.
I have a fragment code but it doesn't run.
Here is my code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            int number;
            string type;

            number = 5;
            if (number % 2 == 1)
                type = "odd";
            else if (number % 2 == 0)
                type = "even";
            Console.WriteLine(type);
        }
    }
}

I received a error message: "Use of unassigned local variable 'type'"
But when I replace this fragment code:

else if (number % 2 == 0)
    type = "even";

by

else
    type = "even";

my code ran correctly.
Can you explain for me why?
Thanks for your helps.

Hi there :) I hope you'll like C# and stick to it because it's a wonderful language to learn ^^

The problem is that you don't initialize your "type" variable (you don't give any value when you create it), this has a consequence that if you try to use it somewhere it'll cause this error. The reason why this happens is that in your if-else structure only two cases are checked, if both of these cases are false then it should go to the else statement (what you don't have), so it will simply follow the next step which is using the unassigned variable. The reason why this seems illogical is that checking a number if it's odd or even boils down to two cases, but the compiler doesn't know that, the expression will be checked in runtime only.
The code is basically correct, the compiler is just cautious and it only makes sure that no unassigned value will be used.

I hope it makes sense, if you still don't understand please feel free to ask :)

You could also simplify your if-statement.
If you know that a number is even, it can never be odd, so you could rewrite your if-statement like this:

if (number % 2 == 0)                
    type = "even";
else
    type = "odd";

@CloneXpert: Yes, C# is indeed a wonderful language to learn! :)

I find myself curious what language(s) if any you used prior to learning C#.

The reason I ask this is because I notice that you don't use any braces with your if/else statements. While they 'technically' can be omitted (as is evident from the fact that your code works without them) I've found that people whose first language is C# usually use them like so:

if (number % 2 == 0)
{
    type = "even";
}
else
{
    type = "odd";
}

Whereas people who started with a language such as python let the indents do the work for them mostly. The only reason this came up is that it can make it easier to sort the conditions relating to each state within the if/else structure and ensure that code is being placed/executed in the correct places. Potentially, if not careful, the statement:

else if (number % 2 == 0)
    type = "even";
Console.WriteLine(type);

Could be misinterpreted as having the writeline only occurring during the 'else if' portion.

Again, just a curiosity was all :)

Relating to your question, I would definitely have to agree with ddanbe about simplifying your statements. For a multiple possibility if/else there is a definite use for 'else if' but for something where there are only ever 2 possibilities a straight up if/else is enough.

The one thing that concerns me is that, based on your original code, you should never have an unassigned value as there is only even or odd if dealing with an integer and it shouldn't be looking for a 3rd option. Unless you're somehow dealing with a negative integer... Brain not working at the moment but I think -5 % 2 = -1

The one thing that concerns me is that, based on your original code, you should never have an unassigned value as there is only even or odd if dealing with an integer and it shouldn't be looking for a 3rd option. Unless you're somehow dealing with a negative integer... Brain not working at the moment but I think -5 % 2 = -1

They wouldn't end up with an unassigned variable, but the compiler isn't smart enough to know that. It just sees the first if, says "ok, if true it has a value, let's check false. Of, false is another if statement, so if it's true ... Yep, it has a value. What about false, nope, there is no else so it wouldn't have a value. So I believe it is possible to get to a point where it does not have a value if both the if statements are false".

It doesn't understand that a number is either even or odd, just that every if has 2 choices, and that if it is 'possible' to reach a condition where it isn't assigned, then it isn't good code and won't compile.

@Everyone: Thanks for your comments. They really helped me a lot.
@CloneXpert: I agree with you. C# is a wonderful language (thought I have just learnt it few days ago).
@Lusiphur: I learned C before learning C#.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.