0

There are many similar questions here but I could not understand why can't I use them in C# but in other languages. How does this code work if I am not using try catch block but expects me to initialize the variables when I am using it. How does memory allocation takes place in this case. P.S. I am a beginner and some of these thing make limited sense to me.

using System;

public class Example
{
   public static void Main()
    {
    int user=0, pass=0;
    do
    {
        Console.WriteLine("Enter the username");
        try
        {
            user = Convert.ToInt32(Console.ReadLine());

            Console.WriteLine("Enter the password");
            pass = Convert.ToInt32(Console.ReadLine());
            if (user == 12 && pass == 1234)
                Console.WriteLine("Correct");
            else
                Console.WriteLine("Invalid Username or Password");
            Console.ReadKey();
        }
        catch (FormatException)
        {
            Console.WriteLine("Invalid Format");
        }
        catch (OverflowException)
        {
            Console.WriteLine("Overflow");
        }

    }
    while (user != 12 && pass != 1234);

}



    }
CodeBeginner
  • 43
  • 1
  • 3
  • One thing of note is that "why can't I use them in c# but in other languages [I can]" is answered with "because c# is a different language". This behaviour is part of the design philosophy of c#. They could easily have made it work without explicit initialisation but they didn't. Answering why is not really in the scope of stack overflow beyond saying they thought it was a better idea than the alternative. – Chris Jun 30 '15 at 16:58
  • [This](http://stackoverflow.com/questions/30816496/why-do-local-variables-require-initialization-but-fields-do-not/30816696#30816696) question explains some. – Yuval Itzchakov Jun 30 '15 at 17:13

2 Answers2

4

In C#, it is required to initialize variables before you access them. In your example, if you don't have try/catch block, variables user and pass will be initialized with

    user = Convert.ToInt32(Console.ReadLine());

and

    pass = Convert.ToInt32(Console.ReadLine());

before line where you access them with

    while (user != 12 && pass != 1234);

However, if you use try/catch block, as in your example, and FormatException or OverflowException is thrown in

    Convert.ToInt32(Console.ReadLine());

then variables will be uninitialized once you access them in

    while (user != 12 && pass != 1234);
Jakiša Tomić
  • 290
  • 1
  • 7
0

Because if you dont initialize variables before try/catch. you have to specify them in each block

        int user, pass;
        do
        {
            Console.WriteLine("Enter the username");
            try
            {
                user = Convert.ToInt32(Console.ReadLine());// it will not be initialized if process fails.
                pass = Convert.ToInt32(Console.ReadLine());// it will not be initialized if process fails.
            }
            catch (FormatException)
            {
                // what is user and pass?
                Console.WriteLine("Invalid Format");
            }
            catch (OverflowException)
            {
                // what is user and pass?
                Console.WriteLine("Overflow");
            }

        } while (user != 12 && pass != 1234);

Because there is possibility of fail in each block. so you have to initialize them in each block If you dont do it before try catch.

if you dont use try catch:

        int user, pass;
        do
        {
            Console.WriteLine("Enter the username");
            user = Convert.ToInt32(Console.ReadLine()); // it will be initialized any way.
            pass = Convert.ToInt32(Console.ReadLine()); // it will be initialized any way.


        } while (user != 12 && pass != 1234);

Convert.ToInt32(Console.ReadLine()); then what would be the value of user or pass?

M.kazem Akhgary
  • 18,645
  • 8
  • 57
  • 118
  • 2
    There's no need to initialize in every path of code if it was already initialized in an enclosing block. – Sehnsucht Jun 30 '15 at 16:46
  • i know. i am making it clear for user. he asked why it needs to be initialized if i use try catch? if you dont use try catch you dont need to initialized in an enclosing block. @Sehnsucht – M.kazem Akhgary Jun 30 '15 at 16:49
  • i dont understand witch part im wrong ? @Sehnsucht – M.kazem Akhgary Jun 30 '15 at 16:54
  • 1
    nothing was "wrong" but you should maybe state more clearly that any variable need to be assigned a value prior to access __whatever code path was used__. That has little to do with try/catch ; if the do/while loop started with an if block in which the variable were initialized the same problem will occur because in the code path where we don't enter that if we arrive at a point where variable wouldn't be initialized. – Sehnsucht Jun 30 '15 at 17:01
  • yes the general answer is that each variable must be initialized before using its value for any purpose. the same thing happens with if/else conditions.. switch case etc... @Sehnsucht – M.kazem Akhgary Jun 30 '15 at 17:07