enable "option explicit" c#

Posted on 2007-10-07
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
    LVL 29

    Accepted Solution

    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
    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 29

    Assisted Solution

    I didn't answer how can you switch on 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.
    LVL 1

    Author Comment

    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

    Better Security Awareness With Threat Intelligence

    See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

    Join & Write a Comment

    Bit flags and bit flag manipulation is perhaps one of the most underrated strategies in programming, likely because most programmers developing in high-level languages rely too much on the high-level features, and forget about the low-level ones. Th…
    Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
    Hi everyone! This is Experts Exchange customer support.  This quick video will show you how to change your primary email address.  If you have any questions, then please Write a Comment below!
    Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

    745 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

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now