Quick Nooby Question, C#

Status
Not open for further replies.

Oreo

-Deactivated-
Messages
5,723
Location
England
Okay so i'm running through some tutorials, and the guy goes on to explain how to do a very basic calculator.

So he goes though how to do it all and i end up with this:


int firstTextBox = 0;
int secondTextBox = 0;
int result = 0;

firstTextBox = int.Parse (textBox1.Text);
secondTextBox = int.Parse (textBox2.Text);

result = firstTextBox + secondTextBox;
label1.Text = result.ToString();

It works, but i'm just wondering that howcome no matter what i change the:


int firstTextBox = 0;
int secondTextBox = 0;
int result = 0;

0 numbers to it still works, even if i do this:


int firstTextBox = 7;
int secondTextBox =12;
int result = 4;

It will still work properly as a calculator, so i guess what im asking out of curiosity is what do them three 0's actually represent ?
 
The zero's are being set as a safety measure. Before they are set to zero there's just random garbage from memory in each of those variables.

The reason it doesn't matter what you initialize them to is because you're setting them to the values of what's in the text boxes right after.
 
Before they are set to zero there's just random garbage from memory in each of those variables.
This is C# we're talking about here. Class variables are initialized to 0 (or null if an object, and false if a boolean) by the CLR when the class is instantiated, so there's never any random garbage in the values. If the programmer manually initializes the values of class variables to 0, the C# compiler will emit the extra instructions to set memory to 0 again, which is inefficient. So it's not wrong, but it's definitely inefficient.
 
Status
Not open for further replies.
Back
Top Bottom