Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8870537
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T17:45:30+00:00 2026-06-14T17:45:30+00:00

I want to convert string to operator in c# **string type = +; int

  • 0

I want to convert string to operator in c#

        **string type = "+";
        int d = 22;
        int c = 33;
        int total;
        total = d + type + c;**

How to do that ?

Regards,

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T17:45:32+00:00Added an answer on June 14, 2026 at 5:45 pm

    You cannot convert a string to an “operator”. An operator is defined for its operands and it doesn’t really make sense to convert a string to an operator if you don’t know what your operands are.

    Supposing you do know what operands you have, the problem is no longer one of “conversion” per se but actually you are trying to build a parsing engine. This is a problem of considerable difficulty. Unless you actually are trying to create your own scripting language or something of this nature, it is probably just easier to use a lookup table, with each element in the table referring to some method that can be run on the operands.

    In C# it is possible to implement such a data structure using a simple switch statement (of course you can make this as fancy as you want ad infinitum but this is the simplest solution).

    switch( type )
    {
        case "+":
            return plusOperator(d, c);
        case "-":
            return minusOperator(d, c);
    }
    

    Then you would define suitable methods such as plusOperator and minusOperator to actually implement the logic of your program.

    This solution is reasonably dirty in that you are hard-coding certain behaviour but really if you want much more than this in terms of good system architecture it becomes a parsing problem. Should you go down this path the Gang of Four design patterns will make for good reference material (particularly patterns such as the Interpreter, Iterator and Composite)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to convert an string to an enum type using TValue, I googled
I want to convert string representation of real number to real type. I know
In java i have two variables String string; int integerValue; I want to convert
I want to convert String to Date in different formats. For example, I am
I want to convert a string to formatted text in C# in WPF application,
I want to convert a string '30/12/2012' to '2012/12/30'. My application is set to
i want to convert this string into DateTime. Tue Aug 19 15:05:05 +0000 2008
I want to convert a string representations of few dozen enum types to enum
I want to convert a String to an array of objects of Character class
I want to convert a string like 2012-03-08T00:00:00+01:00 into Date.. I tried to use

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.