please explain this program

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • gayathriram
    New Member
    • Feb 2008
    • 6

    please explain this program

    Code:
    class test
    {
            static void show(params object[] b)
           {
              console.WriteLine(b.GetType().FullName);
    
             console.WriteLine(b.length);
             console.WriteLine(b[0]);
        }
      public static void Main()
      {
       Object a[]={1,"hi",4000};
       show((Object)a);
    }
    
    }
    Output
    ======
    System.Object[]
    1
    System.Object[]

    How its coming i don't Know.Please explain this.

    What is the use of GetType()

    What is the use of FullName
    Last edited by Plater; Feb 19 '08, 03:01 PM. Reason: added [code] tags
  • int08h
    New Member
    • Apr 2007
    • 28

    #2
    OK, I will explain all key points

    first, the method name
    static void show(params object[] b)
    the "params" keyword allows a method to have variable-count arguments, you can call it like
    static void show(1, 2, 3)
    this invocation gives the method 3 arguments (1, 2 and 3)
    in the method arguments is encapsulated in an array, so b is an array of object

    second, the GetType() method returns the type, like:
    string str;
    so str.GetType() will return System.String
    int i;
    i.GetType() will return System.Int32

    third, the FullName property of type Type gives the full name (namespace+type ) of the type, some thing as
    string is System.String
    int is System.Int

    so the first line outputs the full name of the type of arugment b, it is previously said that b is an array of object, so its type is
    System.Object[]

    forth, considering the length of b, it is a bit more complex
    Object a[]={1,"hi",4000};
    show(a);
    the length will be 3(1, "hi" and 4000)
    however, you conert a (array of object) to a object, so in method test, b only contains an object, this object is an array of object (everything is an object)

    so if you add a line said:
    Console.Writeli ne(((object[])b[0]).Length)
    it will be 3

    at last, the last output line System.Object[]
    since it is previously said that b in this case is a array of object in which the first element is also an array of object, therefore b[0] is Object[]
    the last output calls ToString() method of b[0], so it calls ToString() on an instance of object[], it just returns "System.Obj ect[]"

    Comment

    • gayathriram
      New Member
      • Feb 2008
      • 6

      #3
      Thank You now i understood

      Comment

      Working...