Tuesday, 2 July 2013

CSHARP SAMPLE PROGRAMS - K. MANOHAR GOUD

9866550012

CSHARP SAMPLE PROGRAMS

1.

Program that uses new operator: C#

using System;

class Perl
{
    public Perl()
    {
 // Public parameterless constructor.
 Console.WriteLine("New Perl()");
    }
    public Perl(int a, int b, int c)
    {
 // Public parameterful constructor.
 Console.WriteLine("New Perl(a, b, c)");
    }
}

class Program
{
    static void Main()
    {
 // Create a new instance of the Perl type.
 // ... Use the new operator.
 // ... Then reassign to another new object instance.
 Perl perl = new Perl();
 perl = new Perl(1, 2, 3);

 // Another Perl instance.
 Perl perl2 = null;
 perl2 = new Perl();

 // Instantiate the Program class.
 // ... No constructor is declared, so there is a default.
 Program program = new Program();
 Console.WriteLine(program != null);
    }
}

Output

New Perl()
New Perl(a, b, c)
New Perl()
True
2.
Program that demonstrates parameter passing: C#

using System;

class Program
{
    static void Main()
    {
 int val = 0;

 Example1(val);
 Console.WriteLine(val); // Still 0!

 Example2(ref val);
 Console.WriteLine(val); // Now 2!

 Example3(out val);
 Console.WriteLine(val); // Now 3!
    }

    static void Example1(int value)
    {
 value = 1;
    }

    static void Example2(ref int value)
    {
 value = 2;
    }

    static void Example3(out int value)
    {
 value = 3;
    }
}

Output

0
2
3
3.
Program 1: C#

using System;

class Program
{
    static void Main()
    {
 // Argument = 5
 // Argument = Sam
 Perls(5, "Sam");
    }

    static void Perls(int id, string name)
    {
 Console.WriteLine(id);
 Console.WriteLine(name);
    }
}

Output

5
Sam
4.
Program 2: C#

using System;

class Program
{
    static void Main()
    {
 Perls(5, "Sam");
    }

    static void Perls(int id, string name)
    {
 // Parameter = id
 // Parameter = name
 Console.WriteLine(id);
 Console.WriteLine(name);
    }
}

Output

5
Sam

5.
Program that uses ref and out: C#

using System;

class Program
{
    static void Main()
    {
 string value1 = "cat"; // Assign string value
 SetString1(ref value1); // Pass as reference parameter
 Console.WriteLine(value1); // Write result

 string value2; // Unassigned string
 SetString2(1, out value2); // Pass as out parameter
 Console.WriteLine(value2); // Write result
    }

    static void SetString1(ref string value)
    {
 if (value == "cat") // Test parameter value
 {
     Console.WriteLine("Is cat");
 }
 value = "dog"; // Assign parameter to new value
    }

    static void SetString2(int number, out string value)
    {
 if (number == 1) // Check int parameter
 {
     value = "one"; // Assign out parameter
 }
 else
 {
     value = "carrot"; // Assign out parameter
 }
    }
}

Output

Is cat
dog
one
6.
Program that uses ref argument: C#

using System;

class Program
{
    static void Main()
    {
 string v1 = "dot";
 string v2 = "net";
 string v3 = "perls";

 NullIf(ref v1);
 NullIf(ref v2);
 NullIf(ref v3);

 Console.WriteLine(v1);
 Console.WriteLine(v2);
 Console.WriteLine(v3);
    }

    static void NullIf(ref string value)
    {
 if (value == "net")
 {
     value = null;
 }
    }
}

Output

dot

perls

No comments:

Post a Comment