0% found this document useful (0 votes)
24 views

LAB Manual 6-Enums and Refoutparameters

The document discusses enums, reference/out parameters, and call-by-value vs call-by-reference in C#. It includes examples of using enums to represent months and their integer values. It also shows examples of call-by-value which does not change the original arguments, and call-by-reference which does change the original arguments passed to a method. Additionally, it demonstrates using the out parameter keyword to return a value from a method while also passing the value back to the calling method.

Uploaded by

Hamad Raza
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views

LAB Manual 6-Enums and Refoutparameters

The document discusses enums, reference/out parameters, and call-by-value vs call-by-reference in C#. It includes examples of using enums to represent months and their integer values. It also shows examples of call-by-value which does not change the original arguments, and call-by-reference which does change the original arguments passed to a method. Additionally, it demonstrates using the out parameter keyword to return a value from a method while also passing the value back to the calling method.

Uploaded by

Hamad Raza
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 5

Enums, ref/out parameters

Example
// C# program to illustrate the enums with their default values
using System;
namespace ConsoleApplication1 {
// making an enumerator 'month'
enum month
{ jan, feb, mar, apr, may, jun, jul, aug, sep,oct,nov,dec}
class Program
{
static void Main(string[] args)
{
// getting the integer values of data members..
Console.WriteLine("The value of jan in month " + "enum is " + (int)month.jan);
Console.WriteLine("The value of feb in month " + "enum is " + (int)month.feb);
Console.WriteLine("The value of mar in month " + "enum is " + (int)month.mar);
Console.WriteLine("The value of apr in month " + "enum is " + (int)month.apr);
Console.WriteLine("The value of may in month " + "enum is " + (int)month.may);
}
}}
//Call-by-Value and Call-by-reference Example
namespace Parameters
{ using System;
class Test{
public int a, b;
public Test()
{}
public Test(int i, int j)
{
a = i;
b = j;
}
/* Pass an object. Now, ob.a and ob.b in object used in the call will be changed. */
public void Change(Test ob)
{
ob.a = ob.a + ob.b;
ob.b = -ob.b;
}
/* This method causes no change to the arguments used in the call. */
public void NoChange(int i, int j)
{
i = i + j;
j = -j;
}
}
class Program
{
static void Main(string[] args)
{
//call by value
Console.WriteLine("call by value:\n ");
Test obj = new Test();
int a = 15, b = 20;
Console.WriteLine("a and b before call: " + a + " " + b);
obj.NoChange(a, b);
Console.WriteLine("a and b after call: " + a + " " + b);
//call by reference
Console.WriteLine("call by reference:\n ");
Test ob = new Test(15, 20);
Console.WriteLine("ob.a and ob.b before call: " + ob.a + " " + ob.b);
ob.Change(ob);
Console.WriteLine("ob.a and ob.b after call: " + ob.a + " " + ob.b);
Console.ReadKey();}}}
Example
// Swap two values.
using System;
class ValueSwap
{
// This method now changes its arguments.
public void Swap(ref int a, ref int b)
{
int t;
t = a;
a = b;
b = t;
}
}
class ValueSwapDemo
{
static void Main()
{
ValueSwap ob = new ValueSwap();
int x = 10, y = 20;
Console.WriteLine("x and y before call: " + x + " " + y);
ob.Swap(ref x, ref y);
Console.WriteLine("x and y after call: " + x + " " + y);
}
}
The output from the program is shown here:
x and y before call: 10 20
x and y after call: 20 10
Example
class Decompose
{
/* Decompose a floating-point value into its
integer and fractional parts. */
public int GetParts(double n, out double frac)
{
int whole;
whole = (int)n;
frac = n - whole; // pass fractional part back through frac
return whole; // return integer portion
}
}
class UseOut
{
static void Main()
{
Decompose ob = new Decompose();
int i;
double f;
i = ob.GetParts(10.125, out f);
Console.WriteLine("Integer portion is " + i);
Console.WriteLine("Fractional part is " + f);
}
}

The output from the program is shown here:


Integer portion is 10
Fractional part is 0.125

You might also like