c# - How to determine Type of value types -
c# - How to determine Type of value types -
this question has reply here:
how check if object not of particular type? 6 answershow find type
of value type in c#?
let's have:
string str; int value; double doublevalue;
is there method returns type of of these value types?
to clearer, trying this:
string str = "hello"; string typeofvalue = <call method returns type of variable `str`> if (typeofvalue == "string") { //do } else { //raise exception }
i want input user , raise exception if value entered not string
or int
or double
depending on conditions.
i have tried:
public class test { public static void main(string[] args) { int num; string value; console.writeline("enter value"); value = console.readline(); bool isnum = int32.tryparse(value, out num); if (isnum) { console.writeline("correct value entered."); } else { console.writeline("wrong value entered."); } console.readkey(); } }
but if type of value want check string
or else?
you can utilize gettype
on element in .net since exists @ object level :
var mystringtype = "string".gettype(); mystringtype == typeof(string) // true
gettype returns type
object, can readable human friendly name using name
property on type
.
c#
Comments
Post a Comment