I have two lines of simple code.
int x = 12 * 30;
MessageBox.Show(x.ToString());
First line : I understand that x
is an instance
of type int.
Second line : So, I used the ToString
method of type int.
Question :
How can I put the value 12*30
into the instance x
without using the "field" of int type?
How can I get the value 12*30
from the instance x
without using the "field" of int type?
Am I missing something now?
Regards
EDIT 1)
Maybe,, I did not make myself clear enough.
I'd like to shed a light to my question with different direction.
For the first line) I heard that the "x" is an instance of type int. (C# 5.0 in a Nutshell pg.15 "Predefined Type Example"). And I thought, an instance can not have any kind of value (int/string whatever kind) directly. So, I thought it needs a field to put a value. That is my first question.
EDIT 2)
The 2nd question is basically identical to the 1st question. The instance "x" has value now (somewhere inside of it, I do not know where it is, the value is 360). As you can see it, it make a good result with "instance.method" style. I thought its style should be "field.method". That is my second question(I have some kind of cofusion with 2nd question though).
EDIT 3)
I just made my questions with wondering feelings. I do not want to make any kind of bizarre route to accomplish something. I just wondering how it works nicely even without any "FIELD". It is so inconsistent with other type's ways of (normal) working. It is so confusing.
Obviously, int
doesn't have a field to put the factors of the number that it is storing.
Ok, so first question: You can put stuff in the variable because it is an int
. int
variables are supposed to store numbers so you can use a number literal, such as 5
as a value for that variable. In your case, you don't put 12 * 30 into the variable, you put the result of 12 * 30 into the variable, which is 360. It's just like:
int x = 360;
They are exactly the same. There is no need to use fields of the Int32
type.
Second question: You cannot. Just as I said before, you are putting 360 into the variable, not a multiplication expression. That means the variable doesn't know that when you assign 360 to the variable, you actually used 12 * 30. It just knows that you want to store 360. That's all. So if you want to store the expression in a variable, don't use int. Instead, use expression trees. Jon Skeet's book, C# In Depth, introduces expression trees, you can have a look at that.
EDIT:
Now that you made it clearer, I can tell you that int
do have a field, but it's private. You can go to referencesource.microsoft.com to look at the source code of int. But why cn you just assign a value to an int? Because that is the beauty of primitive types. They just let you do this. It's all about the CLR.