Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

ignoring a method's return type 1

Status
Not open for further replies.

dragonwell

Programmer
Oct 21, 2002
863
US
If you call a method that returns an object as if if returned void, what happens to the returned object?

Example:
Code:
public class MethodHolder
{
    private int testValue=0;
    
    public int TestValue
    {
        get{return testValue;}
    }

    public string MakeString()
    {
        //a side effect
        testValue = 1;
        
        return "Return String";
    }
}

public class TestProgram
{
    public static void Main()
    {
        MethodHolder mh = new MethodHolder();
    
        //call the method without getting the return value
        mh.GetString();

        //only using it to get side effect
        Console.Write(mh.TestValue.ToString());
    }
}

So, when I call
Code:
GetString()
in the example without referencing it's returning string object, where does the return object go? Does it ever exist?

Just something I'm wondering about.

Thanks,



David
[pipe]
 
Hi David,

The return object is created in the method normally and then returned, and then no variable is assigned to it, so it becomes garbage and will eventually be garbage collected. The method you call will execute the same no matter what you do with the return value.

Hope this helps,

regards,
Blaxo
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top