How can I get the data type of a variable in C #?

How to find out what type of data is stored in a variable? (e.g. int, string, char, etc.)

I now have something like this:

using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Testing { class Program { static void Main() { Person someone = new Person(); someone.setName(22); int n = someone.getName(); Console.WriteLine(n.typeOf()); } } class Person { public int name; public void setName(int name) { this.name = name; } public int getName() { return this.name; } } } 
+43
c # types
Jul 24. 2018-12-12T00:
source share
10 answers

Other answers give good help in this matter, but there is an important and subtle problem that none of them address directly. There are two ways to consider a type in C #: a static type and a runtime type.

The static type is the type of the variable in the source code. Therefore, this is the concept of compilation time. This is the type that you see in the tooltip when you hover over a variable or property in your development environment.

The runtime type is the type of object in memory. Therefore, this is the concept of runtime. This is the type returned by the GetType() method.

The runtime type of an object often differs from the static type of a variable, property, or method that holds or returns it. For example, you might have code like this:

 object o = "Some string"; 

The static type of the variable object , but at run time, the referent type of the variable string . Therefore, the following line will print "System.String" on the console:

 Console.WriteLine(o.GetType()); 

But if you hover over the o variable in your development environment, you will see the System.Object type (or the equivalent keyword object ).

For variables of type values, such as int , double , System.Guid , you know that the runtime type will always be the same as the static type, since value types cannot serve as a base class for another type; the value type is guaranteed to be the most derived type in the inheritance chain. This is also true for private reference types: if the static type is a private reference type, the runtime value must be either an instance of this type or null .

Conversely, if the static type of a variable is an abstract type, then it is guaranteed that the static type and the type of the runtime will be different.

To illustrate this in code:

 // int is a value type int i = 0; // Prints True for any value of i Console.WriteLine(i.GetType() == typeof(int)); // string is a sealed reference type string s = "Foo"; // Prints True for any value of s Console.WriteLine(s == null || s.GetType() == typeof(string)); // object is an unsealed reference type object o = new FileInfo("C:\\f.txt"); // Prints False, but could be true for some values of o Console.WriteLine(o == null || o.GetType() == typeof(object)); // FileSystemInfo is an abstract type FileSystemInfo fsi = new DirectoryInfo("C:\\"); // Prints False for all non-null values of fsi Console.WriteLine(fsi == null || fsi.GetType() == typeof(FileSystemInfo)); 
+60
Jul 24 2018-12-12T00:
source share

In general, you are unlikely to ever need to do type comparisons unless you are doing something with reflection or interfaces. However:

If you know the type you want to compare with, use the is or as operators:

 if( unknownObject is TypeIKnow ) { // run code here 

The as statement executes a cast that returns null if it fails, not an exception:

 TypeIKnow typed = unknownObject as TypeIKnow; 

If you do not know the type and want to get information about the type of the runtime, use the .GetType () method:

 Type typeInformation = unknownObject.GetType(); 
+13
Jul 24. 2018-12-12T00:
source share

Its very simple

 variable.GetType().Name 

it will return your data type of your variable

+7
Mar 28 '14 at 18:43
source share
+4
Jul 24 2018-12-12T00:
source share

Just hold the cursor over the member you are interested in and look at the hint - it will show memeber type:

enter image description here

+4
Jul 24 2018-12-12T00:
source share

PHP and C # are syntactically related, but completely different, while I could answer the question at face value (see this article http://msdn.microsoft.com/en-us/library/58918ffs(v=vs. 71) .aspx ) I strongly advise you to get a copy of the CLR through C # (third or second edition) by Jeffrey Richter and read it. His best book related to programming, I think I have ever read and answered almost all of your questions related to type, and gave you a very deep understanding of what is happening under the hood!

+1
Jul 24 2018-12-12T00:
source share

GetType() method

 int n=34; Console.WriteLine(n.GetType()); string name="Smome"; Console.WriteLine(name.GetType()); 
0
Jul 24 2018-12-12T00:
source share

One option is to use a helper extension method, for example:

 public static class MyExtensions { public static System.Type Type<T>(this T v)=>typeof(T); } var i=0; console.WriteLine(i.Type().FullName); 
0
Jul 25 '16 at 20:50
source share

check out one of the easy ways to do this

 // Read string from console string line = Console.ReadLine(); int valueInt; float valueFloat; if (int.TryParse(line, out valueInt)) // Try to parse the string as an integer { Console.Write("This input is of type Integer."); } else if (float.TryParse(line, out valueFloat)) { Console.Write("This input is of type Float."); } else { Console.WriteLine("This input is of type string."); } 
0
Aug 09 '17 at 12:06 on
source share

Normally, you don’t need to check the type of the variable manually, because you have defined it before, and the compiler will check it for you.

-2
Jul 24 2018-12-12T00:
source share



All Articles