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();
        }
    }
}
  

Sunday, 10 February 2013


// Write a program to print the binary format of a number

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

namespace ConsoleApplication4
{
    class Program
    {
        static void Main(string[] args)
        {
            int num;
            Console.Write("Enter a Number : ");
            num = int.Parse(Console.ReadLine());
            int quot;

            string rem = "";

            while (num >= 1)
            {
                quot = num / 2;
                rem += (num % 2).ToString();
                num = quot;
            }

            // Reversing the  value
            string bin = "";
            for (int i = rem.Length - 1; i >= 0; i--)
            {
                bin = bin + rem[i];

            }

            Console.WriteLine("The Binary format for given number is {0}", bin);


            Console.Read();

        }
    }
}

Methods in CSharp


//Using Methods(Example)

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

namespace CSharpConsole
{
    class Method_ex
    {

        //Non value returning method without parameter
        public void Test1()
        {
            int m = 5;
            for (int i = 1; i <= 10; i++)
                Console.WriteLine("{0}*{1}={2}", m, i, m * i);
        }
        // Non value returning method with parameters
        public void Test2(int m, int n)
        {
            for (int i = 1; i <= n; i++)
                Console.WriteLine("{0}*{1}={2}", m, i, m * i);
 
        }
        //value returning method with out parameters
        public string Test3()                  //Static action
        {
            string str = ("Hello Leena");
            str = str.ToUpper();
            return str;
        }
        //value returning method with parameters
        
           
        public string Test4(string str)       
        {                        
            str = str.ToUpper();             //Dynamic Action
            return str;
        }
            
     static void Main(string[] args)
      {
         Method_ex m=new Method_ex();

        //calling non-value returning methods
         m.Test1();
         m.Test2(7,20);
        //Calling value returning methods
         string str=m.Test3();
         Console.WriteLine(str);
         Console.WriteLine(m.Test4("leena"));
         Console.ReadLine();     
         
      }
    }
}