in Technology by
Explain the difference among typeof, GetType, or is?

1 Answer

0 votes
by

All are different.

  • typeof takes a type name (which you specify at compile time).
  • GetType gets the runtime type of an instance.
  • is returns true if an instance is in the inheritance tree.

1.

Type t = typeof(obj1);
if (t == typeof(int))

This is illegal, because typeof only works on types, not on variables. I assume obj1 is a variable. So, in this way typeof is static, and does its work at compile time instead of runtime.

2.

if (obj1.GetType() == typeof(int))

This is true if obj1 is exactly of type int. If obj1 derives from int, the if condition will be false.

3.

if (obj1 is int)

This is true if obj1 is an int, or if it derives from a class called int, or if it implements an interface called int.

Related questions

0 votes
    What is the output you get for the following code? (function() { return typeof arguments; }) (); 1. arguments 2. array 3. object 4. undefined...
asked Feb 26, 2021 in Technology by JackTerrance
0 votes
    var i = 1; if (function f(){}) { i += typeof f; } x; 1. 1 2. undefined 3. Nan 4. Error...
asked Feb 25, 2021 in Technology by JackTerrance
0 votes
    What is the output you get for the following code? (function() { return typeof arguments; }) (); 1. undefined 2. array 3. arguments 4. object...
asked Feb 25, 2021 in Technology by JackTerrance
0 votes
    var i = 1; if (function f(){}) { i += typeof f; } x; 1) 1 2) undefined 3) Nan 4) Error...
asked Oct 9, 2020 in Technology by JackTerrance
0 votes
    What is the output you get for the following code? (function() { return typeof arguments; }) (); A) undefined B) array C) arguments D) object...
asked Oct 9, 2020 in Technology by JackTerrance
0 votes
    What is the return type of typeof for standard JavaScript objects? (a) xml (b) object (c) dom (d ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Oct 23, 2021 in Education by JackTerrance
0 votes
    Which of this method can be used to make the main thread to be executed last among all the threads? ... portion Multithreading of Java Select the correct answer from above options...
asked Mar 1, 2022 in Education by JackTerrance
0 votes
    A natural number is chosen at random from among the first 300. What Is the probability that the number so chosen is ... by 3 or 5. Select the correct answer from above options...
asked Nov 25, 2021 in Education by JackTerrance
0 votes
    Can you explain the difference between Data Warehousing and Business Intelligence?...
asked Feb 4, 2023 in Technology by JackTerrance
0 votes
    Explain the Difference Between Tableau Worksheet, Dashboard, Story, and Workbook?...
asked Mar 30, 2021 in Technology by JackTerrance
0 votes
    Explain the difference between *ptr++ and ++*ptr same?...
asked Jan 23, 2021 in Technology by JackTerrance
0 votes
    Explain the difference between SAP BASIS and SAP ABAP?...
asked Jan 5, 2021 in Technology by JackTerrance
0 votes
    Can you explain the difference between direct and alternating current?...
asked Dec 12, 2020 in Technology by JackTerrance
0 votes
    Explain the difference between Swift vs Objective-C.?...
asked Nov 30, 2020 in Technology by JackTerrance
0 votes
    Explain the difference between traditional Waterfall model and Agile testing?...
asked Nov 26, 2020 in Technology by Editorial Staff
...