-2
  1. I want to get input from user and print the type of the input given by user.
  2. I have tried this.

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
class Solution
{

    static void Main(String[] args)
    {
        var userObj = Console.ReadLine();

        // if input is 5 it should print it is of type int.

        //if input is 5.4 it should print it is of type double.
        Console.WriteLine(userObj.GetType());// printing only string

    }
}

also tried this but always going false

using System;
class Solution
{

    static void Main(String[] args)
    {
        var userObj = Console.ReadLine();
        if (string.Format(userObj) == string .Format("0"))
        {
            Console.WriteLine("it is of type interger");
        }
    }
}
sikka karma
  • 115
  • 1
  • 12
  • You'll need to attempt to parse the string into each type. `5` is a perfectly valid value for a `double` as well as `int`, so the answer is ambiguous. Your code would turn into `if (int.TryParse(userObj, out var _)) { Console.WriteLine("int"); } else if (double.TryParse(...` and so on. – Rob Jun 23 '17 at 03:42
  • @Rob is right, since `Console.ReadLine()` always gives you a string, you would need to try parsing to each of the types you are looking for (e.g. DateTime, int, etc). [This answer](https://stackoverflow.com/a/36350473/6741868) could help you. – Keyur PATEL Jun 23 '17 at 03:44
  • @Rob thanks its working. – sikka karma Jun 23 '17 at 04:17

2 Answers2

1

You're misunderstanding how var works in C#. C# is a strongly-typed language and as such it's not the same as other languages that use var like JavaScript. Therefore, the variable declared as var already knows what type it is at compile time.

Console.ReadLine() returns a string, therefore the variable userObj in this sense WILL be a string. You will never get anything but a string type.

You can, however, try several things to see if you can convert it to another type. for example:

var userInput = Console.ReadLine();
int x;
if(int.TryParse(userInput, out x))
{
    Console.WriteLine("That's an int!");
}
McAden
  • 13,714
  • 5
  • 37
  • 63
  • This is working well, also expanded it for double and float. but got a review of poor design and language, upon submission in a code challenge, Is there any way to improve the design of this code or to make use of some other possible methods. – sikka karma Jun 23 '17 at 18:04
  • Without knowing the full details of your challenge there isn't a whole lot more to add except maybe a loop that asks again upon invalid input and expanding types to include other data types as you mention like floats/doubles. It's a pretty basic task. – McAden Jun 23 '17 at 19:37
0

Try parse some with some different number datatypes from biggest to smallest. I assume you want to store the number in the smallest one possible.

float var1;
double var2;
int var3;

if (float.TryParse(urMom, out var1))
    Console.WriteLine("Float");
else if (double.TryParse(urMom, out var2))
    Console.WriteLine("Double");
else if (int.TryParse(urMom, out var3))
    Console.WriteLine("Int");
RoundSauce3
  • 170
  • 1
  • 16
  • `urMom`, very mature. Also, you're using `TryParse` wrong, allow me to edit. – Keyur PATEL Jun 23 '17 at 03:57
  • 1
    This `var1 != null` produces a compiler warning because `float` can't ever be equal to null – Ilian Jun 23 '17 at 04:17
  • You can inline your declarations to make things a little more readable, e.g. `int.TryParse(longNumber, out int result); Trace.WriteLine(result);` – JBC Jun 23 '17 at 05:16
  • @JanesAbouChleih: Well, it depends on the version of the C# compiler you're using. I'd expect it to still work when targeting .NET 2.0... – Jon Skeet Jun 23 '17 at 06:11
  • @Jon Skeet oh, yeah, you're right. That's a C#7 language feature. My bad. – jAC Jun 23 '17 at 06:21