ReadOnly Fields :
- The ReadOnly members are just like constants according to the objects.
- The Readonly modifier can be used for fields only.
- Once you initialize the value of ReadOnly data members, the object can‘t change that value, but it can access the value.
- Note : The read only field‘s values can be changed inside of the class only.
- To declare the read only fields:
Syntax : access_specifier readonly data_type variable_name = value;
C# Coding on ReadOnly Fields :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ReadOnlyDataMembersDemo
{
class Sample
{
//read only data member
public readonly string CompanyName = "Software IT Technologies";
}
class Program
{
static void Main(string[] args)
{
Sample s = new Sample();
Console.WriteLine(s.CompanyName);
//s.CompanyName = "TCS"; // is not allowed bcoz it is the readonly member.
Console.Read();
}
}
}
Ref Parameters :
- The reference parameters are similar to the normal parameters.
- The only difference between the normal parameters and reference parameters is: When the value is changed in the reference parameter, would automatically affect the actual parameter in the calling portion.
- Implementation: Use "ref" keyword in calling portion and also in method definition.
- Rule : The actual parameter at the calling portion should be a variable and can‘t be a constant.
- This is just like Call by reference concept in C/C++.
C# Coding on Ref Parameters :
namespace RefParameterDemo
{
class SampleClass
{
public void FirstMethod(int x, int y)
{
x++;
y++;
}
public void SecondMethod(int x, ref int y)
{
x++;
y++;
}
}
class Program
{
static void Main(string[] args)
{
int a = 10, b = 20;
SampleClass sc = new SampleClass();
Console.WriteLine(a + ", " + b);
sc.FirstMethod(a, b);
Console.WriteLine(a + ", " + b);
sc.SecondMethod(a, ref b);
Console.WriteLine(a + ", " + b);
Console.Read();
}
}
}
OUT Parameters :
- This is to return multiple values from a method.
- The "out" parameter is similar to "ref" parameter; but the difference between these two parameters is, "out" parameter does not carries any value from calling portion to the method definition; but it carries the return value to the calling portion.
- Implementation : Use "out" keyword in calling portion and also in method definition.
- So, the "out" may not be initialized, when you call the method.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace OutParametersDemo
{
class Test
{
public string GetBigAndSmall(string s1, string s2, out string small)
{
string big;
if (s1.Length > s2.Length)
big = s1;
else
big = s2;
if (s1.Length < s2.Length)
small = s1;
else
small = s2;
return (big);
}
}
class Program
{
static void Main(string[] args)
{
Test t = new Test();
string s1 = "dotnet";
string s2 = "framework";
string small; //no need of initialization
string big;
big = t.GetBigAndSmall(s1, s2, out small);
Console.WriteLine("Small string: " + small);
Console.WriteLine("Big string: " + big);
Console.Read();
}
}
}
Next Articles -
"this" keyword | "Named" Parameter's
0 Comments