Thursday, 14 February 2013

Every member of a class(variable or method) it it is non--static can be accessed under the Main method only by using object of class. A variable of a class is a copy of the class which is not initialized.
                        obj ----> null

An object of a class is a copy of a class which is initialized using new keyword and the memory is allocated for an object of class.
                        obj --->100=x

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CSharpConsole
{
    class First
    {
        int x = 100;
        static void Main()
        {
            First obj = new First();
            Console.WriteLine(obj.x);    //obj is a object or instance 
            
            Console.ReadLine();
        }
     }
}
// It will print the value of x=100

Wednesday, 13 February 2013

Posted Questions In mindqjobs.

        Today Sudeep told us to post some questions along with their answers on the site mindqjobs.com. The questions were divided into various sections like manual testing, SQL, Quick test professional, LoadRunner etc. We posted the questions so that any person who is registered in minqjobs can view them and can improve his/her technical knowledge.

Tuesday, 12 February 2013

Calling members of another class


//calling methods of another class by creating object

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CSharpConsole
{
    class Testclasses
    {
        static void Main()
        {
            Method_ex m1= new Method_ex();     // m1 is a object created
            Params p1=new Params();            // p1 is a object created
            m1.Test1();                        // calling methods
            m1.Test2(6, 12);
            Console.WriteLine(m1.Test3());
            Console.WriteLine(m1.Test4("Hello"));
            p1.AddNums(100, 75, 50);

            int x = 0, y = 0;
            p1.Math1(100, 25, ref x, ref y);
            Console.WriteLine(x + "   " + y);
            p1.Math2(100, 50, out x, out y);
            Console.WriteLine(x+ "   "+y);
            Console.ReadLine();              // waiting to key press
        }

    }
}

Operator Overloading



// Write a prorram to print the sum of complex numbers


using System;

public struct Complex
{
    public int real;
    public int imaginary;

    public Complex(int real, int imaginary)
    {
        this.real = real;
        this.imaginary = imaginary;
    }

    // Declare which operator to overload (+), the types 
    // that can be added (two Complex objects), and the 
    // return type (Complex):
    public static Complex operator +(Complex c1, Complex c2)
    {
        return new Complex(c1.real + c2.real, c1.imaginary + c2.imaginary);
    }
    // Override the ToString method to display an complex number in the suitable format:
    public override string ToString()
    {
        return (String.Format("{0} + {1}i", real, imaginary));
    }

    public static void Main()
    {
        Complex num1 = new Complex(2, 3);
        Complex num2 = new Complex(3, 4);

        // Add two Complex objects (num1 and num2) through the
        // overloaded plus operator:
        Complex sum = num1 + num2;

        // Print the numbers and the sum using the overriden ToString method:
        Console.WriteLine("First complex number:  {0}", num1);
        Console.WriteLine("Second complex number: {0}", num2);
        Console.WriteLine("The sum of the two numbers: {0}", sum);
        Console.ReadKey();

    }
}

Parameters in CSharp

      Parameters are defined so that the methods can be made more dynamic. Parameters of a methods can be of 2 types.
  •  Input parameters.
  • Output parameters.
       Input parameters are used for bringing values in the method for execution. Whereas output parameters are used for carrying a value ouut of the method after execution of the method
       By default every parameter we pass to a method is an input parameter and if we want to define a parameter as output parameter we need to prefix the parameter with ref or out keyword.

      public void test (int x,ref int y,out int z)

       Here x is an input parameter and y& z are output parameters.

Note: By using return types also we can send results out of a method, but only a single result whereas if we are sending results out of a method using output parameters we have a chance of sending more than 1 result at the time of execution.

Monday, 11 February 2013

Using Parameters


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CSharpConsole
{
    class Params
    {
        //method with default value to parameterss
        public void AddNums(int x, int y = 50, int z = 25)
        {
            Console.WriteLine(x + y + z);

        }
        //method with both input and output parameterss
        public void Math1(int a, int b, ref int c, ref int d)
        {
            c = a + b;
            d = a * b;
        }
        public void Math2(int a, int b, out int c, out int d)
        {
            c = a + b;
            d = a * b;
        }
        static void Main()
        {
            Params p = new Params();
            // Calling method with default values to parameters
            p.AddNums(100);
            p.AddNums(100, 100);
            p.AddNums(100,z: 100);
            p.AddNums(100, 100, 100);

            // Calling methods with input and output parameters
            int x = 0, y = 0;
            p.Math1(100, 50, ref x, ref y);
            Console.WriteLine(x + "   " + y);

            int m, n;
            p.Math2(200, 25, out m, out n);
            Console.WriteLine(m + "   " + n);
            Console.ReadLine();

        }
    }
}

// Program to print the following pattern
//   *****
//   *****
//   *****
//   *****
//   *****


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace pattern_4
{
    class Program
    {
        static void Main(string[] args)
        {
            int i,j, k;

            for (i = 1; i <= 5; i++)
            {
                for (j = 1; j <= i; j++)
                {
                    Console.Write("");
                }
                for (k = 1; k <= 5; k++)
                {
                    Console.Write("*");
                }
                Console.WriteLine();

            }
            Console.ReadKey();
        }
    }
}