Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > C#
|
C# Programming questions specific to the Microsoft C# language. See also the forum Beginning Visual C# to discuss that specific Wrox book and code.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old March 30th, 2008, 04:58 AM
Authorized User
 
Join Date: Aug 2007
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
Default can we pass integer in a method which takes object

If a class takes object type parameter and we pass an integer dirctly, what will be the output

e.g a class xyz takes object type as input parameter
xyz(object o);

and we need to pass an integer through xyz.
xyz(2);

 then what will be the apprach in c#


 
Old March 30th, 2008, 05:05 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

What will the 'output' be? Depends what the function does doesn't it?

You can do this in C# because everything in the CLR is derived from Object. int is an abreviation for the System.Int32 class.

However int is whats called a 'value type' and so something called 'boxing' will take place which takes up more memory and processor time - this is why generic classes for value types are a good thing instead of just using an ArrayList. Search for more information on any of these topics you want to know more about.

/- Sam Judson : Wrox Technical Editor -/
 
Old March 31st, 2008, 03:40 AM
Authorized User
 
Join Date: Aug 2007
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi

    Sam,

             Thanks for your reply. So, we dont have to explicitly convert the integer type to reference type(object)before passing it to xyz(object o) right??

 
Old March 31st, 2008, 03:47 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

Have you actually tried it? I suggest with this kind of really simple question you just knock up a 4 line program that test your theory. The following are the lines of code that need to be added to a standard console application:

xyz(1);

public static void xyz(object o)
{
}

/- Sam Judson : Wrox Technical Editor -/
 
Old March 31st, 2008, 04:02 AM
Friend of Wrox
 
Join Date: Mar 2007
Posts: 432
Thanks: 0
Thanked 1 Time in 1 Post
Default

Sam is right, sometimes the best way to learn is to just "roll your sleaves up" and get your hands dirty with some code. Examine the behaviour and see what its doing. You are then in a position to either understand why, or not. If you do, great, if not, then forums are an ideal place to ask "Why" rather than "What".

Put simply, the answer to this question is obvious is you have an understanding of the fundamentals of Object-Orientated programming.

Follow Sam's advice, play with some code, see the results, find out why it does what it does! A lot of the fun of coding comes from discovery!

Rob
The Developing Developer
Currently Working Towards: MCAD C#
My Blog: http://www.robzyc.spaces.live.com
<center>"Nothing can stop the man with the right mental attitude from achieving his goal;
nothing on earth can help the man with the wrong mental attitude".

Thomas Jefferson</center>





Similar Threads
Thread Thread Starter Forum Replies Last Post
xslt.Load method in c# takes about 8 seconds pradeepn XSLT 2 August 31st, 2007 03:53 PM
Method reffering to an object beccali JSP Basics 1 March 23rd, 2007 07:24 AM
Class Method takes Forever to run. hexOffender VB.NET 2002/2003 Basics 1 April 11th, 2006 09:18 AM
How to Pass c++ object across ATL DLL ann123 Visual C++ 0 January 30th, 2006 06:55 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.