CODEDIGEST
Home Articles CodeDigest Tutorials FAQs
Skip Navigation LinksHome » Article » Csharp Article » Parameters in C# – Part 6   You are not logged in.
Search
 

Technologies
 

Sponsors
 

CodeDigest Navigation
 

Technology News
No News Feeds available at this time.
 

Community News
No News Feeds available at this time.
 
Parameters in C# – Part 6

By Selva Kumar
Posted On Aug 11,2008
Article Rating: (Login)
Be first to rate
this article.
No of Comments: 0
Print this article.
Category: CSharp

Subscribe to our feed!

Parameters in C# – Part 6

 

Introduction

Parameters in C#, Part - 6 of this article series will help us understand, passing parameters in C#. In particular, this part will concentrate on Output parameters with an example wherever necessary.

 

Output parameters

Like reference parameters, output parameters don't create a new storage location, but use the storage location of the variable specified on the invocation. Output parameters need the out modifier as part of both the declaration and the invocation - that means it's always clear when you're passing something as an output parameter. Output parameters are very similar to reference parameters. The only differences are:

------> The variable specified on the invocation doesn't need to have been assigned a value before it is passed to the function member. If the function member completes normally, the variable is considered to be assigned afterwards (so you can then "read" it).

------> The parameter is considered initially unassigned (in other words, you must assign it a value before you can "read" it in the function member).

------> The parameter must be assigned a value before the function member completes normally. Here is some example code showing this, with an int parameter (int is a value type, but if you understood reference parameters properly, you should be able to see what the behaviour for reference types is):

void Foo (out int x)
{
    // Can't read x here - it's considered unassigned
    // Assignment - this must happen before the method can complete normally
    x = 10;
    // The value of x can now be read:
    int a = x;

}

...

// Declare a variable but don't assign a value to itint y;
// Pass it in as an output parameter, even though its value is
unassignedFoo (out y);
// It's now assigned a value, so we can write it out:Console.WriteLine (y);

 

ASP.Net Hosting

Recent Articles

EXAMPLE

using System;
using System.Text;

public class Example8
{
    static void Foo (out int x)
    {
        // Can't read x here - it's considered unassigned

        // Assignment - this must happen before the method can complete normally
        x = 10;

        // The value of x can now be read:
        int a = x;
    }

    public static void Main (string[] args)
    {
        // Declare a variable but don't assign a value to it
        int y;

        // Pass it in as an output parameter, even though its value is
unassigned
        Foo (out y);

        // It's now assigned a value, so we can write it out:
        Console.WriteLine (y);
    }

}

Output:
10

Conclusion

Thus, we have understood Output parameters in C# in detail. Don’t hesitate to post your queries in the feedback section of this article. We will see about Parameter arrays in the next part of this article series.

Thanks for reading this article!

 

Similar Articles
  • You can contribute to CodeDigest.Com:
    Article Feedback
    Title  
    Submitted By  
    Comment  
    Enter the verification number
     
    Comments