2
Answers

NP79 Base Type & ToString()

Maha

Maha

16y
1.9k
1

Hi Guys

 

NP79 Base Type & ToString()

 

Out put of Console.WriteLine(o.GetType()); is System.Int32, when BaseType added that means out put of Console.WriteLine(o.GetType().BaseType); is System.ValueType. Please explain the reason.

 

ToString() Returns a string representation of a given object, using the namespace.class name. But here ToString() Returns 99 that means Console.WriteLine(o.ToString()); is giving an out put 99. Please explain the reason.

 

Also explain GetTypeCode() as well. 

 

Thank you

 

using System;

class Boxer

{

   // Helper f(x) to illustrate automatic boxing.

   public static void UseThisObject(object o)

   {

      Console.WriteLine(o.GetType());

      Console.WriteLine(o.GetType().BaseType);

 

      Console.WriteLine(o.ToString());

      Console.WriteLine("Value of o is: {0}", o);

 

      // Need to explicitly unbox to get at members of

      // System.Int32. (GetTypeCode() returns a value

      // representing the underlying “type of intrinsic type”.

      Console.WriteLine(((int)o).GetTypeCode());

   }

   static void Main(string[] args)

   {

      int x = 99;

      UseThisObject(x); // Automatic boxing.

   }

}

/*

System.Int32

System.ValueType

99

Value of o is: 99

Int32

*/

Answers (2)