C# Parameter Programming Example

In this chapter you will learn:
In this chapter you will see some programming examples of using parameter in C#.
Qu 1: Write a program to show difference between value type parameter and reference type parameter.
Answer
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

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

Output

Enter a number: 9
Value Type
---------------------------------
Previous Value: 9
Current Value: 9

---------------------------------
Reference Type
---------------------------------
Previous Value: 9
Current Value: 10
__
 
Qu2: Write a program in which accept two argument as parameter from the user and returns four output value as add, subtract, multiplication and division.
Answer
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Example2
{
    class Program
    {
        public static void parameter(int num1, int num2, out int add, out int sub, out int mul, out float div)
        {
            add = num1 + num2;
            sub = num1 - num2;
            mul = num1 * num2;
            div = (float)num1 / num2;
        }
        static void Main(string[] args)
        {
            int num1, num2;
            int add, sub, mul;
            float div;
            Console.Write("Enter 1st number\t");
            num1 = Convert.ToInt32(Console.ReadLine());
            Console.Write("\nEnter 2nd number\t");
            num2 = Convert.ToInt32(Console.ReadLine());

            Program.parameter(num1, num2, out add, out sub, out mul, out div);
            Console.WriteLine("\n\n{0} + {1} = {2}", num1, num2, add);
            Console.WriteLine("{0} - {1} = {2}", num1, num2, sub);
            Console.WriteLine("{0} * {1} = {2}", num1, num2, mul);
            Console.WriteLine("{0} / {1} = {2}", num1, num2, div);

            Console.ReadLine();
        }
    }
}

Answer

Enter 1st number       7
Enter 2nd number       9

7 +9 = 16
7 - 9 = -2
7 * 9 = 63
7 / 9 = 0.7777778
__
Qu3: Write a program in which pass two number as command line argument and displays the multiplication of both number.
Answer
a.Write the following command on notepad and save as commandline.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Example3
{
    class Program
    {
        static void Main(string[] args)
        {
            int num1, num2, result;
            num1 = Convert.ToInt32(args[0]);
            num2 = Convert.ToInt32(args[1]);
            result = num1 * num2;
            Console.WriteLine("{0} x {1} = {2}", num1, num2, result);
        }
    }
}

b.Open visual studio command prompt and set current path of your program.
c.Compile it as csc commandline.cs
d.Now execute program as commandline 5 4

Output

Setting environment for using Microsoft Visual Studio 2008 x86 tools.
C:\Program Files\MIcrosoft Visual Studio 9.0\VC>d:

D:\>csc commandline.cs
Microsoft <R> Visual C# 2008 Compiler version 3.5.30729.1
for Microsoft <R> .NET Framework version 3.5
Copyright <C> Microsoft Corporation. All rights reserved.

D:\>commandline 5 4
5 x 4 = 20

D:\> __

Summary

In this chapter you studied some programming examples of parameter. In next chapter you will do some programming exercises of parameter.

More Articles

 

Share your thought