OLAP
ref-out parameter

ref-out parameter

It took me some time to realize the real potential of ref and out keywords, till the time I experience a condition which could only be simplified by implementing ref and out keyword.

Lets start by a simple code example and will evolve while moving forward.

  1. public int CalculateSavings(int principal)
  2. {
  3. int result=0;
  4. //Calculate savings and return result
  5. return result;
  6. }

Here we have a method ‘CalculateSavings’ which takes a input parameter ‘principal’ and returns ‘result’ as output parameter. Now, we got a requirement that this method should also return ‘interest’ but if we see that a method can only return only one output, then how can we achieve it?

We can achieve it by using ref or out keyword. Both are useful in this case, though there is a major difference which we will learn shortly.

Two output parameters ‘result’ and ‘interest’ can be achieved by the following code.

  1. int interest;
  2. public int CalculateSavings(int principal, out int interest)
  3. {
  4. int result=0;
  5. //set interest value;
  6. //Calculate savings and return result
  7. return result;
  8. }

Here, ‘out’ parameter is used to tell the compiler that value of variable ‘interest’ will also be available to the members outside of this method and is not limited to the method. This method only sets the value of out variable. Please note that, if value of ‘out’ variable is left unattended in this method and not set then it will result in compile time error.

We will achieve the same by ‘ref’ keyword.

  1. int interest=0;
  2. public int CalculateSavings(int principal, ref int interest)
  3. {
  4. int result=0;
  5. //set interest value;
  6. //Calculate savings and return result
  7. return result;
  8. }

I assume just declaring and initializing ’interest’ before the method will not satisfy you and you will search for more differences over the internet. I behaved similarly but no help till the time i got the requirement which introduces ‘ref’ to me.

Previously, i have mentioned if value of ‘out’ variable is left unattended in this method and not set then it will result in compile time error. But in case of ‘ref’, no compile time error is thrown whether ‘ref’ variable value is set or not.

Instead of looking for the difference between ‘ref’ and ‘out’ keyword, if 5 min. are dedicated for this exercise then surely one can easily solve any similar requirement.

Author: hershey

A passion for knowledge drives me to do programming, A passion for programming drives me to create something different, A passion for creation drives me to spread the knowledge.

Share This Post On

0 Comments

  1. nice superb explaination

    Post a Reply
  2. thanks for the tutorial
    I am having a problem.
    The feature “Create SQL server database” isn’t available , I can’t select it.
    Can you help, please ?

    Post a Reply

Submit a Comment

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

More from CodeSpread:

  • C#: My experience with const keywordC#: My experience with const keywordThere are numerous post on internet about 'what is const keyword in .net' or 'what is the difference between const keyword and read only' and I must say many might have already mugged it up like me...
  • Nullable .Net typesNullable .Net typesWe declare .net types in our project daily but still we do not recollect default values for each .net type so we end up doing null check for each data type. Its a basic mistake of a developer but ....
  • Asp.Net: More about CookiesAsp.Net: More about CookiesWe have seen, how our cookies looks in our last article Cookies Part 1. In this article, we will talk about their properties,limitations and technical part. Properties and Limitations Cook...
  • Asp.Net: Hidden Truth of CookiesAsp.Net: Hidden Truth of CookiesI know there are several articles on cookies as what are they? and how can they be created? but i always wanted to know more about them. My questions were where they are stored? how to track their ...
  • C# Code to send a MailC# Code to send a MailWhile working in a project, we always neglect the importance of the code which we have written many times and now end up in copy pasting the code from old projects. Lately, I saw a request in fo...
  • Why Multiple Inheritance is discouraged?Why Multiple Inheritance is discouraged?Hi Guys, This is Sathish Again,Today we will discuss why most of the higher languages eliminates Multiple Inheritance? Most of you guys already know that multiple inheritance is not available for...
  • Concepts: S O L I DConcepts: S O L I DSOLID - Object oriented programming principles. May be its easy to forget these principles  but for creating a reusable code or maintainable code, people will always have to come back and check ...
  • VB.Net: Download file from FTP and UnZipVB.Net: Download file from FTP and UnZipExperience: Many application requires data migration tasks to execute on daily basis. To achieve this we generally seek help of SSIS package, if at all we are making use of SQL sever. ...