Reputation: 8926
TextBox.Text is string property
but if you assign an int, decimal ..etc to it it works!!
int x = 5;
Textbox1.Text = x; // it works
any body know why can tell me ?
Upvotes: 2
Views: 270
Reputation: 50028
Its because of Implicit conversion.
Here is the full list of implicit numberic conversions
As Sonny has pointed out, the following is true from here
If Option Strict is On, the above example raises a compiler error. If Option Strict is Off, however, the conversion is performed implicitly, even though this implicit conversion may cause an error at runtime. For this reason, you should always use Option Strict On.
Upvotes: 8
Reputation: 28586
as already pointed out this is because of implict conversion.
An other example:
?Console.WriteLine(1)
1
?Console.WriteLine("1")
1
because
Console.WriteLine(1) == Console.WriteLine(1.ToString())
Upvotes: 1
Reputation: 1498
Value types such as int, double, etc. have a ToString() method that is automatically called for you. You can see this by typing x.ToString(); What is really happening under the hood is that the value type is being "boxed" into a corresponding reference type (class) and the class has the ToString method.
Upvotes: 2
Reputation: 3593
C# will allow implicit conversion to String from a numeric type. So this works because it is equivalent to:
TextBox1.Text = x.ToString();
Upvotes: 2