0

For Ex:

cw("enter anything");

//we dont know if user entered int or string or any other datatype so we would check as,

if(userEntered == int){}
if(userEntered==string){}

In other way: If user enters for example int value so we convert it and save it but if we dont know what user entered, how will we judge or detect?

Mostafiz
  • 7,243
  • 3
  • 28
  • 42

7 Answers7

1

For a console application taking in user input, assume its a string to begin with as string will be able to hold whatever the input is.

If needed, then you can parse it to another datatype such as ints or floats.

danielku97
  • 60
  • 8
1
public class MainClass
{
public static void Main(string[] args)
{
    String value = Console.ReadLine();
    var a = new MainClass();
    if a.IsNumeric(value){
    //your logic with numeric
    }
    else 
    {
    //your logic with string
    }
}

public Boolean IsNumeric(String value){
 try 
   var numericValue = Int64.Parse(value);
   return true;
 catch 
   return false;
}

In this case there is a separate function, which tries to convert it to number, if successful return true, otherwise false. With this you will avoid further code repetition to check if your value is numeric or not.

Coke
  • 965
  • 2
  • 9
  • 22
0

There is no strict rule for that, User just enters a string value. It could be null or empty string or any other string, which might or might not be convertible to int or decimal or DateTime or any other datatype. So you just have to parse the input data and check whether its convertible to a datatype or not.

Vivek Nuna
  • 25,472
  • 25
  • 109
  • 197
0
 Console.WriteLine("enter someting");
 string read = Console.ReadLine();

Console.Readline() read user input and will return a string

From here try to convert/parse it to other data type.

Timon Post
  • 2,779
  • 1
  • 17
  • 32
0

You can read user input through console.readline to check if it is a string or integer ,you can do something like this by default user input is string check the following code snippet

class MainClass
    {
        public static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
            string input = Console.ReadLine();
            Console.WriteLine(input);

            int num2;
            if (int.TryParse(input, out num2))
            {
                Console.WriteLine(num2);
            }
        }
}
Geeky
  • 7,420
  • 2
  • 24
  • 50
0

Here is an example to check if user enters integer or string value:

Console.WriteLine("Enter value:");
string stringValue = Console.ReadLine();

int intValue;
if(int.TryParse(stringValue, out intValue))
{
    // this is int! use intValue variable
}
else
{
    // this is string! use stringValue variable
}
Markiian Benovskyi
  • 2,137
  • 22
  • 29
0

Once you have your string input, you can use TryParse to see if it is an int or a decimal (among other things)...

string userEntered = Console.ReadLine();
int tempInt;
decimal tempDec;

if(int.TryParse(userEntered, out tempInt))
  MessageBox.Show("You entered an int: " + tempInt);
else if(decimal.TryParse(userEntered, out tempDec))
 MessageBox.Show("You entered a decimal: " + tempDec);
else MessageBox.Show("You entered a string: " + userEntered);
Chris Berger
  • 557
  • 4
  • 14