Tuesday, September 2, 2014

difference between value type parameter and reference type parameter.

using System;

namespace Example1
{
    class Program
    {
        public static void value(int num)
        {
            num++;
        }
        public static void reference(ref int num)
        {
            num++;
        }

        static void Main(string[] args)
        {
            int num;
            Console.Write("Enter a number:\t");
            num = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("\n\n\tValue Type");
            Console.WriteLine("----------------");
            Console.Write("\nPrevious Value:\t{0}", num);
            Program.value(num);
            Console.Write("\nCurrent Value:\t{0}", num);

            Console.WriteLine("\n\n\n----------------");
            Console.WriteLine("\tReference Type");
            Console.WriteLine("--------------------");
            Console.Write("\nPrevious Value:\t{0}", num);
            Program.reference(ref num);
            Console.Write("\nCurrent Value:\t{0}", num);
            Console.ReadLine();
        }
    }

}

C# Function

using System;

namespace Example1
{
  class calculation
   {
     static int num1, num2, result;
     public static void add()
      {
        Console.Write("Enter number 1st.\t");
        num1 = Convert.ToInt32(Console.ReadLine());

        Console.Write("Enter number 2nd.\t");
        num2 = Convert.ToInt32(Console.ReadLine());

        result = num1 + num2;

        Console.Write("\nAdd = {0}", result);
        Console.ReadLine();
      }
   }
  class Program
   {
     static void Main(string[] args)
      {
        calculation.add();
      }
   }

}

Access Specifier and get set accessor.

using System;

namespace Example1
{
    class Program
    {
        private void add()
        {
            int num1, num2, result;
            Console.Write("Enter a number:\t");
            num1 = Convert.ToInt32(Console.ReadLine());

            Console.Write("\nEnter second number:\t");
            num2 = Convert.ToInt32(Console.ReadLine());

            result = num1 + num2;
            Console.WriteLine("{0} + {1} = {2}", num1, num2, result);
        }
        static void Main(string[] args)
        {
            Program p = new Program();
            p.add(); //It is valid, because private add() is           in same class
            Console.ReadLine();
        }
    }
}


using System;

namespace Example2
{
    class input
    {
        private static int num1, num2, result;
        public void add()
        {
            result = num1 + num2;
            Console.WriteLine("\n\nAdd = {0}", result);
            Console.ReadLine();
        }

        // Creating property for storing value in num1
        public int Number1
        {
            get
            {
                return num1;
            }
            set
            {
                num1 = value;
            }
        }

        // Creating property for storing value in num2
        public int Number2
        {
            get
            {
                return num2;
            }
            set
            {
                num2 = value;
            }
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            input inp = new input();
            Console.Write("Enter number 1st:\t");
            inp.Number1 = Convert.ToInt32(Console.ReadLine());

            Console.Write("Enter number 2nd:\t");
            inp.Number2 = Convert.ToInt32(Console.ReadLine());

            inp.add();
        }
    }

}

C# Programming Examples of Variables and Data types




using System;
 
namespace Examples
{
  class Program
   {
     static void Main(string[] args)
      {
        string name;
        string city;
        sbyte age;
        int pin;
 
        // \n is used for line-break
        Console.WriteLine("Enter your name\n");
        name = Console.ReadLine();
 
        Console.WriteLine("Enter Your City\n");
        city = Console.ReadLine();
 
        Console.WriteLine("Enter your age\n");
        age = sbyte.Parse(Console.ReadLine());
 
        Console.WriteLine("Enter your pin\n");
        pin = Int32.Parse(Console.ReadLine());
 
        // Printing message to console
        //formatting output
        Console.WriteLine("=============="); 
        Console.WriteLine("Your Complete Address:");
        Console.WriteLine("============\n");
 
        Console.WriteLine("Name = {0}", name);
        Console.WriteLine("City = {0}", city);
        Console.WriteLine("Age = {0}", age);
        Console.WriteLine("Pin = {0}", pin);
        
Console.WriteLine("===============");
 
        Console.ReadLine();
      }
   }
}
O/P:

Enter your Name :Naresh
Enter Your City :Wgl
Enter Your age  :23
Enter Your Pin  :506164

=============================
Your Complete Address:
=============================
your Name :Naresh
Your City :Wgl
Your age  :23
Your Pin  :506164

How to debug or execute C# program?

Step 1: Open Notepad and write the following c# code carefully.


using System;
 
namespace Chapter1
{
  class Program
   {
     static void Main(string[] args)
      {
        Console.WriteLine("This is my First C# Program");
      }
   }
}


Step 2: Now save this program as Chapter1.cs. You must choose All Files in save as type box.
Step 3: Go to Start > All Programs > Microsoft Visual Studio 2005 or 2008 > Visual Studio Tools > Visual Studio 2005 or 2008 Command Prompt.
Step 4: Set Path in command prompt where your Chapter1.cs file is saved. Here we are using D:/Csharp>
Step 5: Now compile your program by following command:
  csc Chapter1.cs


You will see some information like compiler version number and framework information. There is no error, so you won’t get any error.
Step 6: Now, it’s turn to C sharp program. Just write your file name without extension and hit enter.
  Chapter1
Step 7: You will get the This is my First C# Program as output.

Output 

How to debug or execute C# program?

Step 1: Open Notepad and write the following c# code carefully.


using System;
 
namespace Chapter1
{
  class Program
   {
     static void Main(string[] args)
      {
        Console.WriteLine("This is my First C# Program");
      }
   }
}


Step 2: Now save this program as Chapter1.cs. You must choose All Files in save as type box.
Step 3: Go to Start > All Programs > Microsoft Visual Studio 2005 or 2008 > Visual Studio Tools > Visual Studio 2005 or 2008 Command Prompt.
Step 4: Set Path in command prompt where your Chapter1.cs file is saved. Here we are using D:/Csharp>
Step 5: Now compile your program by following command:
  csc Chapter1.cs


You will see some information like compiler version number and framework information. There is no error, so you won’t get any error.
Step 6: Now, it’s turn to C sharp program. Just write your file name without extension and hit enter.
  Chapter1
Step 7: You will get the This is my First C# Program as output.

Output 

program to print following output using for loop.

using System;
namespace Example2
{
    class Program
    {
        static void Main(string[] args)
        {
            int i, j;
            i = 0;
            j = 0;

            for (i = 1; i <= 5; i++)
            {
                for (j = 1; j <= i; j++)
                {
                    Console.Write(i);
                }
                Console.Write("\n");
            }
            Console.ReadLine();
        }
    }
}


O/P:

1
22
333
4444
55555

program to display table of given number.


using System;

namespace Examples1
{
    class Program
    {
        static void Main(string[] args)
        {
            int num, i, result;
            Console.Write("Enter a number\t");
            num = Convert.ToInt32(Console.ReadLine());

            for (i = 1; i <= 10; i++)
            {
                result = num * i;
                Console.WriteLine("{0} x {1} = {2}", num, i, result);
            }
            Console.ReadLine();
        }
    }
}

 O/P: 8 *1 =8
          - - - -
 --- -
 --- 
8 * 10=80