469,631 Members | 1,218 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,631 developers. It's quick & easy.

please explain this program

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
2 1078
int08h
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
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.

Similar topics

2 posts views Thread by Abhish | last post: by
1 post views Thread by Yash | last post: by
9 posts views Thread by hope | last post: by
12 posts views Thread by Sanjeev | last post: by
6 posts views Thread by Buck Rogers | last post: by
22 posts views Thread by Jaspreet | last post: by
reply views Thread by raa abdullah | last post: by
reply views Thread by wb5plj | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.