enable "option explicit" c#

Posted on 2007-10-07
Medium Priority
Last Modified: 2010-05-18
Hi there,

I have a new computer and started coding a new project, C#.

I noticed i did not apply ToString() to a int variable when combining it with string.

int maxLength = 50;
throw new Exception.EntityException_FieldLength("Last Name is limited to " + maxLength + " characters");

This compiles! How do i fix it so the compiler tells me to fix the code? For the life of me i cannot see the option in the editor.

I would expect to have to write
throw new Exception.EntityException_FieldLength("Last Name is limited to " + maxLength-->.ToString()<-- + " characters");

Question by:Valimai
  • 2
LVL 30

Accepted Solution

anarki_jimbel earned 1000 total points
ID: 20032308
It is not Visual Basic, it's C# and it is a bit different.

Basicly, "+" operator is overloaded so that if you are trying to concatenate a string and a number - it's OK.

Explanation from http://www.thescripts.com/forum/thread577964.html:
mmm... I took a look to the IL produced.
C# compiler to concatenate strings call string.Concat().
Since there is not string.Concat(string, int) the called overload is
string.Concat(object, object).
If you look inside it (I used Reflector) you can see

return (arg0.ToString() + arg1.ToString());

So this is the discovered secret :)
LVL 30

Assisted Solution

anarki_jimbel earned 1000 total points
ID: 20032364
I didn't answer how can you switch on http://msdn2.microsoft.com/en-us/library/aa288467(VS.71).aspxtion explicit". There is no such an option. However if you really need on some a reason you may consider overriding the "+" operator like in:

( Honestly, not sure if it is possible in case of strings and numbers).

By the way, other languages like Java also permit that behaviour.

Author Comment

ID: 20032418
THanks for the articles and it does appear that the code works as designed.

I just can't believe that I could always do this with c#:
int myNumber = 5;
Response.Write("The number is " + myNumber);

No need to respond to this one. I will keep the question open for another day to see if anyone else has a view. Otherwise i will accept your answer.

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
High user turnover can cause old/redundant user data to consume valuable space. UserResourceCleanup was developed to address this by automatically deleting user folders when the user account is deleted.
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Planning to migrate your EDB file(s) to a new or an existing Outlook PST file? This video will guide you how to convert EDB file(s) to PST. Besides this, it also describes, how one can easily search any item(s) from multiple folders or mailboxes…

612 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question