Operator + cannot be applied to operands of type string and method group, C# error


Cause

This error comes up when you tries to append two strings with the “+” sign. As the + sign in C# is a arithmetical operator so you cannot use this too append two string type values. To use “+” the values or variables must be if numeric type.

As C# uses doesn’t supports implicit typecasting, for string concatenation use “&” sign.
Example

string varTest = “”;
string str1 = “abc”;
int str2=21;

varTest = str1 + str2;

The above stqatmet will cause in error stating “Operator + cannot be applied to operands of type string and method group“. Because we are trying to add a string variable in a numeric variable.

To remove the error you must convert all the numeric (integer) datatype or variables to string.

varTest = str1 & str2.ToString();

The output of the above code will be abc21

Conclusion
We must convert all values to a uniform datatype before storing them into a variable.

Advertisements

About dotnetcoderoom

Working as an Onsite coordinator in USA. Handling UAT, CR (Change Requests), Product Support/Development, Build Management. Supervise and assist in live product implementation (at client side) and training. Providing technical direction for the development, design, and system integration. Closely monitoring and working with offshore team for Enhancements, Bug Fixing, Enforce coding standards, perform code reviews and mentors junior developers. Proven track of timely delivery of enterprise level web based applications. Skilled in a variety of software languages, tools, and methodologies, with a special expertise in Microsoft .NET technologies. MCTS .Net 4.0 Web applications MCTS .Net 2.0 Web applications GNIIT, MCA, B.Com Specialties 8+ Years of experience in .NET Technologies. Expertise in .NET, ASP.NET, MVC, C#, VB.NET, WCF, Web Services, MS SQL, JQuery. Excellent R&D and troubleshooting skills. Extensive knowledge in E-Commerce and Healthcare domain. View all posts by dotnetcoderoom

6 responses to “Operator + cannot be applied to operands of type string and method group, C# error

  • Nick

    Also, the ony time I’ve ever seen that error is when you try to use the plus operator with a value and a method (ie, you forgot to use parens). Example:

    int y = 5 + SomeClassInstance.SomeMethod;

    That will raise an the error you show. The line should read:

    int y = 5 + SomeClassInstance.SomeMethod(); // Notice the parens at the end.

  • MR

    I think you also can get this if you inadvertently try to iterate through an IEnumerable with a for loop.

  • pradeep kodley

    i got this error..Operator ‘>=’ cannot be applied to operands of type ‘method group’ and ‘int’

    when i use –
    if(Convert.ToInt32(lblScore.Text).ToString >= 30)

    why is so..and how would i rectify…give proper solution plz

    • dotnetcoderoom

      Modify your code as below:

      if(Convert.ToInt32(lblScore.Text) >= 30)

      The error is coming because you are trying to compare a string value with an integer value.
      So I removed the ToString method from your code.

  • 2010 in review « Dotnetcoderoom Blog

    […] Operator + cannot be applied to operands of type string and method group, C# error September 2008 4 comments 3 […]

  • pallavi

    public string generateAccountNum()
    {
    const int MaxNumbers = 13;
    Random r = new Random();
    System.Text.StringBuilder sb = new StringBuilder();
    int size = MaxNumbers;
    String legalNumber = “0123456789”;
    for (int i = 0; i < size; i++)
    {
    sb.Append(legalNumber.Substring(r.Next(0,legalNumber-1)+ 1, 1));
    }
    return sb.ToString ();
    }

    hi am getting error like this
    sb.Append(legalNumber.Substring(r.Next(0,legalNumber-1)+ 1, 1));
    operator '-' cannot be applied to operands of type string and int
    help me for this error

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: