C# Notes 17 6 2022

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 5

fReference parameters

 
Reference parameters also known as “ref” parameters are one of the most common and oldest
way of returning multiple values from a function. As the name suggests, they are passed as
reference to the function and after the function executes, the updated value of the passed
reference variable is returned back to the calling method. It is important to note that reference
parameters must be defined, initialized and assigned before they are passed to function else you
may encounter a compile time error. Reference parameters are defined in the function signature
as
public int MultipleReturns(int a, int b, ref int max)  
{  
 if(a<b)  
 {  
    max=a;  
    return b;  
 }  
 else  
 {  
    max=b;  
    return a;  
 }  

In the above example we have defined the function using 2 integer parameters a & b and a ref
parameter max. The function returns the minimum value between a & B and also assigns the maximum
value to the output parameter. You can call the function as below

int a=10, b=20,max=0;  
int min = MultipleReturns(a,b,ref max);  
Console.WriteLine("Minimum Value: " + min);  
Console.WriteLine("Maximum Value: " + max); 

Output Parameters
Output parameters also known as “out” parameters and are similar to reference parameters. As
the name suggests, they are passed to the function as parameters and the calling method expects
some values to be passed back in the parameter from the function. Output parameters are defined
in the function as below

public int MultipleReturns(int a, int b, out int max)  
{  
 if(a<b)  
 {  
     max=a;  
   return b;  
 }  
 else  
 {  
     max=b;  
     return a;  
 }  

In the above code we have defined the function signature using 2 integer parameters a & b and
an out parameter max. The function returns the minimum value between a & B and also assigns
the maximum value to the output parameter. If the function MultipleReturns() does not set any
value to max variable inside the body, a compile time error generates. Hence, it is mandatory to
assign values to out parameters in the function body. This is also to be noted that you cannot
define 2 functions with same signature but having difference of only ref & out parameters, else
the compiler will throw an error. You can call the function as below

int a=10, b=20,max=0;  
int min = MultipleReturns(a,b,out max);  
Console.WriteLine("Minimum Value: " + min);  
Console.WriteLine("Maximum Value: " + max); 

Returning an object of Class/Struct Type


 
Returning multiple values via arrays has a limitation wherein we can return multiple values of
only the same type. For example, if we want to return a string as well as integer, it won't be
possible using the 2nd approach. Returning an object of class/struct type is the most robust way
of returning multiple values from a function. Here the function will return an object of a
class/struct that can further encapsulate n number of properties within them. We'll be using a
simple MinMax class to demonstrate this idea. Let us rewrite that function to return an object
now:
struct MinMax  
{  
     public int min;  
     public int max;  
}   
public MinMax MultipleReturns(int a, int b)  
{  
       MinMax values = new MinMax();  
       values.min = a < b ? a : b;  
       values.max = a > b ? a : b;  
       return values;  

We can call the function using the following code:

int a=10, b=20;  
MinMax results = MultipleReturns(a,b);  
Console.WriteLine("Minimum Value: " + results.min);  
Console.WriteLine("Maximum Value: " + results.max); 

The need of encapsulation is to protect or prevent the code (data) from accidental corruption due
to the silly little errors that we are all prone to make. In Object oriented programming data is
treated as a critical element in the program development and data is packed closely to the
functions that operate on it and protects it from accidental modification from outside functions.

Encapsulation provides a way to protect data from accidental corruption. Rather than defining
the data in the form of public, we can declare those fields as private. The Private data are
manipulated indirectly by two ways. Let us see some example programs in C# to demonstrate
Encapsulation by those two methods. The first method is using a pair of conventional accessor
and mutator methods. Another one method is using a named property. Whatever be the method
our aim is to use the data without any damage or change.

ENCAPSULATION USING ACCESSORS AND MUTATORS

Let us see an example of Department class. To manipulate the data in that class (String
departname) we define an accessor (get method) and mutator (set method).

using system;  
public class Department 
{  
    private string departname;
    // Accessor.  
    public string GetDepartname() 
{  
        return departname;  
    }  
    // Mutator.  
    public void SetDepartname(string a) 
{  
        departname = a;  
    }  

public static int Main(string[] args)
 {  
  Department d = new Department();  
d.SetDepartname("ELECTRONICS");  
  Console.WriteLine("The Department is :" + d.GetDepartname());  
    return 0;  

}  

Like the above way we can protect the private data from the outside world. Here we use two
separate methods to assign and get the required data.

ENCAPSULATION USING PROPERTIES

Properties are a new language feature introduced with C#. Only a few languages support this
property. Properties in C# helps in protect a field in a class by reading and writing to it. The first
method itself is good but Encapsulation can be accomplished much smoother with properties.

using system;  
public class Department {  
   private string departname;  
    public string Departname {  
        get {  
            return departname;  
        }  
        set {  
            departname = value;  
        }  
    }  
}  
public class Departmentmain { 
 public static int Main(string[] args) {  
       Department d = new Department();  
        d.departname = "Communication";  
        Console.WriteLine("The Department is :{0}", d.Departname);  
        return 0;  
    }  
}  

From the above example we see the usage of Encapsulation by using properties. The property
has two accessor get and set. The get accessor returns the value of the some property field. The
set accessor sets the value of the some property field with the contents of "value". Properties can
be made read-only. This is accomplished by having only a get accessor in the property
implementation.

READ ONLY PROPERTY


using system;  
public class ReadDepartment {  
    private string departname;  
    public ReadDepartment(string avalue) {  
        departname = avalue;  
   }  
    public string Departname {  
        get {  
            return departname;  
        }  
    }  
}  
public class ReadDepartmain {  
    public static int Main(string[] args) {  
        ReadDepartment d = new ReadDepartment("COMPUTERSCIENCE");  
        Console.WriteLine("The Department is: {0}", d.Departname);  
        return 0;  
    }  

WRITE ONLY PROPERTY
using system;  
public class WriteDepartment {  
    private string departname;  
    public string Departname {  
        set {  
            departname = value;  
            Console.WriteLine("The Department is :{0}", departname);  
        }  
    }  
}  
public class WriteDepartmain {  
    public static int Main(string[] args) {  
        WriteDepartment d = new WriteDepartment();  
        d.departname = "COMPUTERSCIENCE";  
        return 0;  
    }  
}  

You might also like