Tuesday, August 29, 2017

3. Write a program to demonstrate Operator overloading.

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

namespace program
{
 class OpOver
{
int x, y, z; // 3-D coordinates
public OpOver() { x = y = z = 0; }
public OpOver(int i, int j, int k) { x = i; y = j; z = k; }
// Overload binary + for object + object.
public static OpOver operator +(OpOver op1, OpOver op2)
{
OpOver result = new OpOver();
 /* This adds together the coordinates of the two points  and returns the result. */
result.x = op1.x + op2.x;
result.y = op1.y + op2.y;
result.z = op1.z + op2.z;
return result;
}
// Overload binary + for object + int.
public static OpOver operator +(OpOver op1, int op2)
{
OpOver result = new OpOver();
result.x = op1.x + op2;
result.y = op1.y + op2;
result.z = op1.z + op2;
return result;
}
// Show X, Y, Z coordinates.
public void show()
{
Console.WriteLine(x + ", " + y + ", " + z);
}
}
public class Prog3
{
public static void Main()
{
OpOver a = new OpOver(1, 2, 3);
OpOver b = new OpOver(10, 10, 10);
OpOver c = new OpOver();
Console.Write("Here is a: ");
a.show();
Console.WriteLine();
Console.Write("Here is b: ");
b.show();
Console.WriteLine();
c = a + b; // object + object
Console.Write("Result of a + b: ");
c.show();
Console.WriteLine();
c = b + 10; // object + int
Console.Write("Result of b + 10: ");
c.show();
Console.Read();
}
}
}

2. Write a Program in C# to demonstrate boxing and Unboxing.

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

namespace boxing
{
class Program
{
static void Main(string[] args)
{
int m = 10;
object a = m;
try
{
Console.WriteLine("Value of m is :" + a);
object n = 20;
int b = (int)n;
Console.WriteLine("Value of n is :" + b);
System.Console.WriteLine("Boxing and unboxing is done");
Console.ReadLine();
}
catch (System.InvalidCastException e)
{
System.Console.WriteLine("Error Incorrect unboxing" + e.Message);
}
}
}
}
//converting from value type to reference type is known as BOXING
//converting from reference type to value type is known as UNBOXING

1. Write a Program in C# to demonstrate Command line arguments processing.

 namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
 {
  System.Console.WriteLine("\nNumber of command line arguments : " + args.Length);
  System.Console.WriteLine("Command line arguments are : \t");
  for (int i = 0; i < args.Length; i++)
  {
  System.Console.WriteLine(args[i]+"\t");
 }
 System.Console.ReadKey(); //system is built in package
  }      
  }
}
 //writeline -- curson will move on to nextline || write -- execte in  current line
//readline --  || readkey --
// try to do without any argument in string and in Main M must be capital