How can I get the data type of a variable in C#?
Other answers offer good help with this question, but there is an important and subtle issue that none of them addresses directly. There are two ways of considering type in C#: static type and run-time type.
Static type is the type of a variable in your source code. It is therefore a compile-time concept. This is the type that you see in a tooltip when you hover over a variable or property in your development environment.
Run-time type is the type of an object in memory. It is therefore a run-time concept. This is the type returned by the GetType()
method.
An object's run-time type is frequently different from the static type of the variable, property, or method that holds or returns it. For example, you can have code like this:
object o = "Some string";
The static type of the variable is object
, but at run time, the type of the variable's referent is string
. Therefore, the next line will print "System.String" to the console:
Console.WriteLine(o.GetType()); // prints System.String
But, if you hover over the variable o
in your development environment, you'll see the type System.Object
(or the equivalent object
keyword).
For value-type variables, such as int
, double
, System.Guid
, you know that the run-time type will always be the same as the static type, because value types cannot serve as the base class for another type; the value type is guaranteed to be the most-derived type in its inheritance chain. This is also true for sealed reference types: if the static type is a sealed reference type, the run-time value must either be an instance of that type or null
.
Conversely, if the static type of the variable is an abstract type, then it is guaranteed that the static type and the runtime type will be different.
To illustrate that 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));
Another user edited this answer to incorporate a function that appears below in the comments, a generic helper method to use type inference to get a reference to a variable's static type at run time, thanks to typeof
:
Type GetStaticType<T>(T x) => typeof(T);
You can use this function in the example above:
Console.WriteLine(GetStaticType(o)); // prints System.Object
But this function is of limited utility unless you want to protect yourself against refactoring. When you are writing the call to GetStaticType
, you already know that o's static type is object. You might as well write
Console.WriteLine(typeof(object)); // also prints System.Object!
This reminds me of some code I encountered when I started my current job, something like
SomeMethod("".GetType().Name);
instead of
SomeMethod("String");
How to get a variable type in Typescript?
For :
abc:number|string;
Use the JavaScript operator typeof
:
if (typeof abc === "number") {
// do something
}
TypeScript understands typeof
/p>
This is called a typeguard.
More
For classes you would use instanceof
e.g.
class Foo {}
class Bar {}
// Later
if (fooOrBar instanceof Foo){
// TypeScript now knows that `fooOrBar` is `Foo`
}
There are also other type guards e.g. in
etc https://basarat.gitbooks.io/typescript/content/docs/types/typeGuard.html
How do you know a variable type in java?
a.getClass().getName()
How do I get the type of a variable?
For static assertions, C++11 introduced decltype
which is quite useful in certain scenarios.
Get the actual type from a Type variable
No, you cannot know the value of a Type
object at compile time, which is what you would need to do in order to use a Type
object as an actual type. Whatever you're doing that needs to use that Type
will need to do so dynamically, and not require having a type known at compile time.
how to get type of a variable in swift
You can check the type of any variable using is
keyword.
var a = 0
var b = "demo"
if (a is Int) {
print("It's an Int")
}
if (b is String) {
print("It's a String")
}
To compare any complex type, you can use below method:
if type(of: abc) == type(of: def) {
print("matching type")
} else {
print("something else")
}
How to get type of Julia variable?
You can get the type of any Julia object o
by typeof(o)
.
help?> typeof
search: typeof typejoin TypeError
typeof(x)
Get the concrete type of x.
Examples
≡≡≡≡≡≡≡≡≡≡
julia> a = 1//2;
julia> typeof(a)
Rational{Int64}
julia> M = [1 2; 3.5 4];
julia> typeof(M)
Array{Float64,2}
In your line of code all dots just mean "apply the operation element-wise". Therefore the types (and sizes) don't change.
Also, just to be precise here, Array{Int,3}
doesn't mean "array of 3 integers" but instead "3-dimensional array", which can have arbitrary extent in either of those dimensions. To get the extent or size of the array you can use size(x)
.
what is the best way to check variable type in javascript
The best way is to use the typeof
keyword.
typeof "hello" // "string"
The typeof
operator maps an operand to one of six values: "string"
, "number"
, "object"
, "function"
, "undefined"
and "boolean"
. The instanceof
method tests if the provided function's prototype is in the object's prototype chain.
This Wikibooks article along with this MDN articles does a pretty good job of summing up JavaScript's types.
How do you get the type of a variable in Ballerina?
You can use the typeof expression
for get the type of any variable in Ballerina.
import ballerina/io;
public function main() {
var x = 5;
io:println(typeof x);
}
Please refer to the "Typeof expression" section of the language specification below for more information.
https://ballerina.io/spec/lang/2019R3/#section_6.25
Related Topics
Find Out Whether a C++ Object Is Callable
How to Get the Hmodule for the Currently Executing Code
C++ Double Address Operator? (&&)
Installing C++ Libraries on Os X
Why Does Cudamalloc() Use Pointer to Pointer
C++ Return Value Without Return Statement
Good Tools for Creating a C/C++ Parser/Analyzer
How to Write C/C++ Code Correctly When Null Pointer Is Not All Bits Zero
Range Based For-Loop on Array Passed to Non-Main Function
Differencebetween a Const Reference and Normal Parameter
How to Parse Ini File with Boost
How to Call C++ Functions from Within Ruby
Calling a Function for Each Variadic Template Argument and an Array