Reputation: 81342
People generally agree that C# and VB.net are more or less the exact same thing. But if you've developed in both, you'll notice that VB.net is a lot more forgiving when it comes to type conversion.
For example take this
int someInt = 5;
string someString = "12345";
someString.Replace(someInt, "");
The above code will fail, but will work if replaced with:
int someInt = 5;
string someString = "12345";
someString.Replace(Convert.ToString(someInt), "");
Update
Better example:
Dim i As Integer = 1
Dim j As String = "1"
If i = j Then
MessageBox.Show("Bad comparison")
End If
VB.net is much more forgiving and does not require you to type cast all over the place.
So my question is: Is this explicit type casting going to still be required in future versions of C#, or will the compiler automatically be able to determine what types are required. And, does this mean VB.net is more advanced (because it already does this type conversion for you naturally), or does this mean VB.net is more prone to erroneous code or uses objects all over the place, behind the scenes?
Thanks in advance...
Upvotes: 0
Views: 238
Reputation: 10103
There is a reason for forcing you to cast to string. There is not an unique way to cast a number into a string. Imagine if you were converting a double into a string, you could allow 2 digits after the decimal point or none, these will give you different strings.
For an integer, I understand your frustration, since Convert.ToString(int) will return what most of people would agree by converting an integer into a string.
But one could argue that you want to have leading '0's in front. 5 could be 0005 4 could be 0004 ... etc
Forcing people to use conversion operators/functions which are using your locales and have unique definitions will reduce the number of bugs you will have in large projects.
Upvotes: 1
Reputation: 28425
Don't make explicit conversions and your code might have more bugs. There are situations where you will not be aware that the type was converted to something else and this is bad.
I think behind the scene double is converted to string by calling ToString() method from double (method that is inherited from System.Object)
Upvotes: 1
Reputation: 20120
sorry but I just tried what you just said in vb.net with my default setting, it doesn't work, I need to cast 5 to string too
default setting is;
option explicit on
option strict on
option infer on
I could live with infer off, but never with explicit or strict OFF. Never.
these 2, while off, gave a really bad name to vb/asp/vbs/etc. and you want that into C#?
edit
with your new example I get this error
Error 1 Option Strict On disallows implicit conversions from 'String' to 'Double'. C:\Documents and Settings**\Local Settings\Application Data\Temporary Projects\ConsoleApplication1\Module1.vb 6 16 ConsoleApplication1
Upvotes: 6
Reputation: 47799
The rules set in place for casting will not likely change in future versions of C#. In either case, this does not mean that one language is more advanced than the other just because one might require less casts. And as Fredou mentioned, you likely have the non-strict settings enabled for vb.net which in some cases (as you mentioned) would use latebound objects.
that said, if you prefer the vb.net syntax and type system, then by all means use it :-P it's a full CLR language so you can interop with all the others with no issues
Upvotes: 2