How to Convert String to Type in C#
In this post, I’ll explain how to convert string to type
or get the data type in C#. To determine the value type, we’ll utilize the .GetType
function.
Get type
of String
Value in C#
The example below gets the runtime
type of a string and other values and proceeds to get each value’s type.
Create a class named StringtoType
and a Main()
method.
class StringtoType {
public static void Main() {}
}
Next, make an Object[]
type variable called allvalues
and give it some values like "Abc"
(as a string) and 89
(as a byte).
object[] allvalues = { "Abc",
(long)193527,
"Happy Programming",
(byte)89,
'Z',
(sbyte)-11,
"Zeelandia is 8th continent",
27.9,
"I am a string line",
(int)20,
'7' };
Then, using a foreach
loop, verify each entry in the array to determine its type.
foreach (var data in allvalues) {
}
Initialize a variable named t
of Type
of type inside the foreach
loop. Variable t
will hold data type of every value present in array allvalues
with the help of data.GetType()
method.
Type t = data.GetType();
After that, we’ll apply the if
condition to check if every value will be a string.
if (t.Equals(typeof(string)))
If a value is found to be a string, a message will be displayed like below.
'Happy Programming' is a String
After converting a string to Type
, and other types of data like byte
, sbyte
, int
and double
etc. with the help of following else if
checks:
else if (t.Equals(typeof(sbyte))) Console.WriteLine(" '{0}' is a Signed Byte", data);
else if (t.Equals(typeof(byte))) Console.WriteLine(" '{0}' is a Byte", data);
else if (t.Equals(typeof(int))) Console.WriteLine(" '{0}' is an Integer of 32-bit", data);
else if (t.Equals(typeof(long))) Console.WriteLine(" '{0}' is an Integer of 64-bit", data);
else if (t.Equals(typeof(double))) Console.WriteLine("'{0}' is a double ", data);
Finally, if there are found other than byte
, sbyte
, int
, double
and long
types of data in array allvalues
, it will simply show a message like this:
'Z' is another type of data
Full Code Example:
using System;
using System.Diagnostics;
class StringtoType {
public static void Main() {
object[] allvalues = { "Abc",
(long)193527,
"Happy Programming",
(byte)89,
'Z',
(sbyte)-11,
"Zeelandia is 8th continent",
27.9,
"I am a string line",
(int)20,
'7' };
foreach (var data in allvalues) {
Type t = data.GetType();
if (t.Equals(typeof(string)))
Console.WriteLine(" '{0}' is a String", data);
else if (t.Equals(typeof(sbyte)))
Console.WriteLine(" '{0}' is a Signed Byte", data);
else if (t.Equals(typeof(byte)))
Console.WriteLine(" '{0}' is a Byte", data);
else if (t.Equals(typeof(int)))
Console.WriteLine(" '{0}' is an Integer of 32-bit", data);
else if (t.Equals(typeof(long)))
Console.WriteLine(" '{0}' is an Integer of 64-bit", data);
else if (t.Equals(typeof(double)))
Console.WriteLine("'{0}' is a double", data);
else
Console.WriteLine("'{0}' is another type of data", data);
}
}
}
Output:
'Abc' is a String
'193527' is an Integer of 64-bit
'Happy Programming' is a String
'89' is a Byte
'Z' is another type of data
'-11' is a Signed Byte
'Zeelandia is 8th continent' is a String
'27.9' is a double
'I am a string line' is a String
'20' is an Integer of 32-bit
'7' is another type of data
Compare type
Objects in C#
A Type
object with a type is distinct. Two Type
object identifiers correspond to the same object if they represent the same type.
It enables Type
objects to be compared using reference equality. The example below compares Type
objects that contain several integer values to see if they are of the same type.
At first, Initialize int
, double
, and long
type of variables inside the Main()
method, and assign them some values.
int Value1 = 2723;
double Value2 = 123.56;
int Value3 = 1747;
long Value4 = 123456789;
Initialize a variable named t
of type Type
in variable t
we’ll store data type of value 1 named Value1
by the help of .GetType
method.
Type t = Value1.GetType();
Now, with the help of Object.ReferenceEquals(t, Value2.GetType())
function, we’ll compare types of all objects with Value1
.
Console.WriteLine("Data type of Value1 and Value2 are equal: {0}",
Object.ReferenceEquals(t, Value2.GetType()));
Console.WriteLine("Data type of Value1 and Value3 are equal: {0}",
Object.ReferenceEquals(t, Value3.GetType()));
Console.WriteLine("Data type of Value1 and Value4 are equal: {0}",
Object.ReferenceEquals(t, Value4.GetType()));
Full Code Example:
using System;
using System.Diagnostics;
class CompareTypeObjects {
public static void Main() {
int Value1 = 2723;
double Value2 = 123.56;
int Value3 = 1747;
long Value4 = 123456789;
Type t = Value1.GetType();
Console.WriteLine("The data type of Value1 and Value2 are equal: {0}",
Object.ReferenceEquals(t, Value2.GetType()));
Console.WriteLine("The data type of Value1 and Value3 are equal: {0}",
Object.ReferenceEquals(t, Value3.GetType()));
Console.WriteLine("The data type of Value1 and Value4 are equal: {0}",
Object.ReferenceEquals(t, Value4.GetType()));
}
}
Output:
The data type of Value1 and Value2 are equal: False
The data type of Value1 and Value3 are equal: True
The data type of Value1 and Value4 are equal: False
I have been working as a Flutter app developer for a year now. Firebase and SQLite have been crucial in the development of my android apps. I have experience with C#, Windows Form Based C#, C, Java, PHP on WampServer, and HTML/CSS on MYSQL, and I have authored articles on their theory and issue solving. I'm a senior in an undergraduate program for a bachelor's degree in Information Technology.
LinkedIn