Monday, 8 October 2018

Ref and out key word.


Ref and out key word :-

Ref and out key word use to passing the value as reference to the function,If you want to pass a variable as ref parameter you must need to initialize it, before you pass it as ref parameter to any method. Ref keyword will pass parameter as a reference this means when the value of parameter is changed in called method it get reflected in calling method also.


class Refkeyword
{
static void Main()
{
int i; // variable need to be initialized
i = 3;
Refsamplemethod(ref i);
Console.WriteLine(i);
}
public static void Refsamplemethod (ref int val1)
{
  val1 += 10;
}
}


Properties of ref keyword :-
  • ·         Must be initialized.
  • ·         Always return the single value.
  • ·         Ref keyword used to pass an argument in a method.


Out Key word :-
If you want to pass a variable as out parameter you don’t need to initialize it before you pass it as out parameter to method. Out keyword also will pass parameter as a reference but here out parameter must be initialized in called method before it return value to calling method .





class OutKeyword
{
static void Main()
{
int i,j; // No need to initialize variable
Outsample(out i, out j);
Console.WriteLine(i);
Console.WriteLine(j);
}
public static int Outsample(out int val1, out int val2)
{
val1 = 5;
val2 = 10;
return 0;
}
}







Properties of ref keyword :-
  • ·         No need to initialize.
  • ·         Return the multiple value.
  • ·         Out  keyword used to pass an argument in a method.


Note : Out and Ref work differently at run time but both are same at compile time. So, if you want to create two functions with the same names and same parameters but the only difference is Out and Ref keyword, then it will give a compile time error. We can say overloading is not possible in case of  the same functions and they have only one difference, i.e. Ref and Out keyword.


Tuesday, 2 October 2018

checked and unchecked in c#


Basically Checked and unchecked are operators of C#.
Checked and Unchecked operators enforce CLR ( Common Language Runtime ) to handle stack overflow control.

These operators checks code block which can cause overflow in an application.

Checked operator
unchecked operator


Checked

The checked keyword is used to control the overflow-checking context for integral-type arithmetic operations and conversions. It can be used as an operator or a statement according to the following forms.

The checked statement:

checked block

The statement block that contains the expressions to be evaluated in a checked statement.

The checked operator:

checked (expression)

The expression to be evaluated in a checked context. Notice that the expression must be in parentheses ( ).



Unchecked

The unchecked keyword is used to control the overflow-checking context for integral-type arithmetic operations and conversions. It can be used as an operator or a statement according to the following forms.

The unchecked statement :

unchecked block

The statement block that contains the expressions to be determined in an unchecked statement.

The unchecked operator :

unchecked (expression)

The expression to be determined in an unchecked context. Notice that the expression must be in parentheses ( ).

Example
namespace Checked_Uncheckedkeyword
{

    class Program
    {

        public short a = 40000;
        public short b = 20000;
        public short c;
        public int Add()
      {
            try
            {
                c = checked((short)(a + b));

            }
            catch (System.OverflowException e)
            {

                System.Console.WriteLine(e.ToString());

            }
            return c;
        }
        public int Mul()
        {
            try
        {
                checked
                {
                    c = (short)(a * b);
                }
            }
            catch (System.OverflowException e)
            {
                System.Console.WriteLine(e.ToString());
            }
            return c;
        }
        public int Add_Unchecked()
        {
            try
            {
                c = unchecked((short)(a + b));

            }
            catch (System.OverflowException e)
            {

                System.Console.WriteLine(e.ToString());
            }
            return c;
        }
        public int Mul_Unchecked()
        {
            try
            {
                unchecked
                {
                    c = (short)(a * b);
                }
            }
            catch (System.OverflowException e)
          {
                System.Console.WriteLine(e.ToString());
            }

            return c;

        }

        static void Main(string[] args)
        {
            Program p = new Program();
            // For checked
            Console.WriteLine("Checked output value is: {0}", p.Add());
            Console.WriteLine("Checked output value is: {0}", p.Mul());
            // For Unchecked
            Console.WriteLine("Checked output value is: {0}", p.Add_Unchecked());

            Console.WriteLine("Checked output value is: {0}", p.Mul_Unchecked());
            Console.ReadKey(true);
        }
    }
}