Sunday 10 February 2013

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

No comments:

Post a Comment