Using out in methods


Using out in methods

Using out in methods


public class student
{
public int sno;
public string sname;
public double totalmarks;
public string coursetojoin;
public double fees;

public void getdiscount(out double discount)
{
discount = fees * ( 0.01 * totalmarks );

}



}

namespace ConsoleApplication22
{

class Program
{
static void Main(string[] args)
{
student stu = new student()
{
coursetojoin =".NET",
sno =101,
sname ="Ganesh",
totalmarks =30,fees =50000
};

double discount;
stu .getdiscount (out discount );
Console.WriteLine("{0}", discount);
stu.fees = stu.fees - discount;

Console.WriteLine("{0}{1}{2}", stu.sname, stu.coursetojoin, stu.fees);


}
}
}




Explanation:

The code snippet demonstrates how to use out in methods.
See the usage of out in


public void getdiscount(out double discount)
{
discount = fees * ( 0.01 * totalmarks );

}

And when calling the method

stu .getdiscount (out discount );

the datatype is given in declaring a method and when calling only out and variablename are given


Comments

Author: Sanjay20 Jul 2010 Member Level: Silver   Points : 2

Hi krishnavenikaladi,

Example of ref keyword

namespace out_keyword
{
class cube
{
static void Main(string[] args)
{
int num = 8;
outcube(out num);
Console.WriteLine("Value of num using out keyword:{0}",num);
}


static void outcube(out int inum)
{
inum=3;
inum = inum * inum;
}
}
}

-Sanjay D Tank.



  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: