Knowledge Management Banner

Knowledge Management Banner

C# Tutotial : Comments , Loops , Methods / functions : Part 6

Comments in c#

Sing line comment
  • e.g. //Your comment
Multiline comment
  • e.g.  /* Your comment */
XML documentation, to give description for class
  • Just type /// above class or method, it will automatically open description tag
  • e.g.
/// <summary>
/// Your description
/// </summary>
/// <param name="args"></param>


Loops in C#
  • While loop
  • do loop
  • for loop
  • foreach loop
  • do while loop
Do While loop
do some thing while checking some thing.
e.g.
            var a = 5;
            var b = 10;

            do
            {
                a++;
            }
            while (a == b);

In above e.g. do will be executed 5 times until a == b i.e. a = 10 and b = 10.


Method / Functions in c#

[attributes]
access-modifiers returnType methodName(parameters)
{
method body
}
  1. static method is invoked by using class name.
  2. instance method is invoked by creating object of class.
Method parameters types
  • Value parameter
             static void SquareIt(int x)
            // The parameter x is passed by value.
            // Changes to x will not affect the original value of x.
            {
                x *= x;
                System.Console.WriteLine("The value inside the method: {0}", x);
            }
            static void Main()
            {
                int n = 5;
                System.Console.WriteLine("The value before calling the method: {0}", n);

                SquareIt(n);  // Passing the variable by value.
                System.Console.WriteLine("The value after calling the method: {0}", n);
            }

In above e.g. n is passed to the function SquareIt, in which change in value of x 
will not affect n
  • Reference parameter
             static void SquareIt(ref int x)
            // The parameter x is passed by value.
            // Changes to x will not affect the original value of x.
            {
                x *= x;
                System.Console.WriteLine("The value inside the method: {0}", x);
            }
            static void Main()
            {
                int n = 5;
                System.Console.WriteLine("The value before calling the method: {0}", n);

                SquareIt(ref n);  // Passing the variable by value.
                System.Console.WriteLine("The value after calling the method: {0}", n);
            }

In above e.g. n is passed to the function SquareIt, in which change in value of x 
will affect n, where value of  n == value of x
  • out parameter
        static void Main(string[] args)
        {
            var a = 0;
            GetVal(out a);
            var b = a;
        }

        static void GetVal(out int n)
        {
            var Sum = 5;
            n = Sum;
        }

In above e.g. value of a will be the value assigned in GetVal function
  • parameter arrays
         static void Main(string[] args)
        {
            string[] MyStr = { "A", "B", "C" };
            PrintArr(MyStr);
        }

        static void PrintArr(string[] arr)
        {
            Console.WriteLine(arr[2].ToString());
        }

In above e.g. array string is passed to a function.

Static and Instance method

public class Circle
{
int radius;
int diameter;
}

When ever a class is initiated by instance it consumes memory every time

public class Circle
{
static int radius;
static int diameter;
}

If the method or property is static than it always consumes the same memory.





No comments

Powered by Blogger.