By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,028 Members | 1,092 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,028 IT Pros & Developers. It's quick & easy.

please explain this program

P: 6
Expand|Select|Wrap|Line Numbers
  1. class test
  2. {
  3.         static void show(params object[] b)
  4.        {
  5.           console.WriteLine(b.GetType().FullName);
  6.  
  7.          console.WriteLine(b.length);
  8.          console.WriteLine(b[0]);
  9.     }
  10.   public static void Main()
  11.   {
  12.    Object a[]={1,"hi",4000};
  13.    show((Object)a);
  14. }
  15.  
  16. }
  17.  
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
Feb 19 '08 #1
Share this Question
Share on Google+
2 Replies


P: 28
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.Writeline(((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.Object[]"
Feb 19 '08 #2

P: 6
Thank You now i understood
Feb 20 '08 #3

Post your reply

Sign in to post your reply or Sign up for a free account.