initialize variable and assign int value to it and at run time assigning a string value it give error.
var _formatedBillCode = 101;
_formatedBillCode="20160919_101_ank"; // assigning string value
var would infer its type in compile time. Unlike JS, var here inferred its type in compile time when you defined it with an integer first.
Later if you move with changing its type, it would throw an error because at compile time, the first type it inferred was int. You can't change it to string.
Use dynamic types for your purpose in this case like the following if you need dynamic that much:
dynamic a = 1;
Console.WriteLine(a);
// Dynamic now has a different type.
a = new string[0];
Console.WriteLine(a);
// Assign to dynamic method result.
a = Test();
Console.WriteLine(a);
// Use dynamic field.
_y = "carrot";
// You can call anything on a dynamic variable,
// ... but it may result in a runtime error.
Console.WriteLine(_y.Error);
And in your case here;
dynamic_formatedBillCode = 101;
_formatedBillCode="20160919_101_ank"; // assigning string value
use dynamic to push datatype resolving to runtime.
dynamic _formatedBillCode = 101;
_formatedBillCode="20160919_101_ank";
You are trying to assign a string value to an int, that wont work without casting it. This will assign the second variable as a string with the integer cast to string:
var _formatedBillCodeInt = 101;
var _formatedBillCode="20160919_" + _formatedBillCodeInt.ToString() + "_ank"; // assigning string value
Or did I misunderstand the question?
varis resolved at compile time, not run time. You can't possibly change its initial type inference.