-7

How can I get the real type that is in the string in c#? For example, the method gets a string- "123" and it need to find it's real type- int, and convert this string to an int variable. Is there a simple way to do it? Thanks in advance.

Maksim Simkin
  • 9,561
  • 4
  • 36
  • 49
h.c
  • 11
  • 2
  • 2
    What is the real type of "0"? It could be int, short, long, float, double or decimal... – Matthew Watson Jan 06 '17 at 09:31
  • What are you trying to say? – Pranav Bilurkar Jan 06 '17 at 09:31
  • Well, you'd have to define the PRIORITY list for this, e.g. `"123"` can be both `int` and `long` (not to mention a few others :)). So essentially your app needs a convention, and then you'd write one big if else a la `int i; if (int.TryParse("123", out i)){return i;} bool b; if (bool.TryParse("123", out b) {return b;}` etc – zaitsman Jan 06 '17 at 09:31
  • 2
    in simple words, string is always a string even if it has numeric values. check this: http://stackoverflow.com/questions/9809340/how-to-check-if-isnumeric – Jonathan Applebaum Jan 06 '17 at 09:32

1 Answers1

2

No, it's not possible. In your case, there are different types, that could be possible: int, short, long...

You should decide to what type you want to parse your string.

Maksim Simkin
  • 9,561
  • 4
  • 36
  • 49