Pass by Reference,
const, readonly, struct
Pass by Reference Parameters
The parameters we have seen so far are value parameters
their arguments are passed by value
if you change the value of a parameter in function, corresponding
argument does NOT change in the caller function
Let’s see an example: PassByRef.cs.
Pass by Reference (ref & out)
Pass by reference:
ref
Need to assign an initial value to the parameter
out
No need to assign an initial value to the parameter
static void FunctionOutRef(ref int c, out int d)
{
d = c + 1;
c = c + 1;
}
static void Main(string[] args)
{
int num3 = 5, num4;
FunctionOutRef(ref num3, out num4);
}
Underlying Mechanisms
For value parameters, the arguments’ values are copied
into parameters
arguments and parameters have different memory locations
double Average (int a, int b)
10 15
copy value copy value
10 15
main function
Average(num1, num2)
Underlying Mechanisms
For reference parameters, the parameter and the
argument share the same memory location
parameter is an alias of argument
double average (ref int a, int b)
15
refers to the same
memory location
copy value
10 15
main function
average(ref num1, num2)
Example: Swap
Write a function that swaps the values of its two integer
parameters
Example: Swap
void swap(ref int a, ref int b)
{
int temp;
temp = a;
a = b;
b = temp;
}
How do we use this in main?
int a=5, b=8;
swap(ref a, ref b); // a becomes 8, b becomes 5
swap(ref a, ref 5); // syntax error, arguments must be
variables
Passing Arrays as Parameters
To pass an array argument to a method, specify the name of
the array without any brackets. For a method to receive an
array reference through a method call, the method’s
parameter list must specify an array parameter.
When an argument to a method is an entire array or an
individual array element of a reference type, the called
method receives a copy of the reference.
When an argument to a method is an individual array element
of a value type, the called method receives a copy of the
element’s value.
To pass an individual array element to a method, use the
indexed name of the array as an argument in the method call.
Example: ArrayReferenceTest.cs
const
Sometimes very useful
provides self documentation
re-use the same value across the class or program
avoid accidental value changes
Like variables, but their value is assigned at declaration and
can never change afterwards
declared by using const before the type name (any type is OK)
public const double PI = 3.14159;
const int LENGTH = 6;
later you can use their value
Console.WriteLine(MathClass.PI * 4 * 4);
but cannot change their value
MathClass.PI = 3.14; causes a syntax error
const
Cannot use const with reference types.
Exception: string
public const string CLASSNAME = “IT 528”;
readonly
const is set at compile time
readonly is set at runtime
readonly can be used for reference types as well
Constructor can set readonly fields but not any
other methods
public class GradeBook
{
private readonly string courseName = “”;
public GradeBook(string name)
{
courseName = name;
}
}
struct (structure)
struct is very similar to a class
Like classes, struct’s can have methods and properties
with access modifiers.
Struct members are accessed via (.) operator similarly.
So they are almost the same with one difference:
Class is a reference type whereas struct is a value type.
Example: mystruct.cs