0

How to get type of an input text in csharp

How to get type of an input text in csharp For example: string s = Console.ReadLine(); How to get best type of s? for example if s is ="42" output int or if s is = "hello" output string Thanks

22nd Nov 2020, 11:03 AM
Martin Jefferson
Martin Jefferson - avatar
4 Answers
+ 2
Of course there is a way. TryParse probably fits your needs best since it returns a bool. For explanation, examples and alternatives refer to this: https://www.tutorialsteacher.com/articles/convert-string-to-int
22nd Nov 2020, 11:59 AM
Benjamin JĂŒrgens
Benjamin JĂŒrgens - avatar
+ 1
s will never be 42, it can only be "42", still a string. Use cast/ convert if you want something else. There is no such thing as best type in general. It depends on the application, so you have to define and implement it yourself
22nd Nov 2020, 11:15 AM
Benjamin JĂŒrgens
Benjamin JĂŒrgens - avatar
0
Benjamin JĂŒrgens it looks there's no way to do it?
22nd Nov 2020, 11:20 AM
Martin Jefferson
Martin Jefferson - avatar