Saturday, August 14, 2010

Examples on Value Types and Reference Types


3.Program1:

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

namespace Csharp
{
    /// <summary>
    /// 1.Value Types  Struct
    /// </summary>
    class ValuetypeStruct
    {
        static void Main(string[] args)
        {
            Person p = new Person("Tony", "Allen", 32);
            Console.Write(p.ToString());
            Console.ReadLine();
        }
        struct Person
        {
            public string firstName;
            public string lastName;
            public int age;

            public Person(string _firstName, string _lastName, int_age)
            {
                firstName = _firstName;
                lastName = _lastName;
                age = _age;
            }

            public override string ToString()
            {
                return firstName + lastName + age;
            }
        }

    }
}





4.Program 2:

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

namespace Csharp
{
    /// <summary>
    /// Extending program1 to make use of enum
    /// </summary>
    class Enum
    {
        static void Main(string[] args)
        {
            Person p = new Person("Tony", "Allen", 32, Person.Genders.Male);
            Console.Write(p.ToString());
            Console.ReadLine();
        }

        struct Person
        {
            public string firstName;
            public string lastName;
            public int age;
            public Genders gender;

            public Person(string _firstName, string _lastName, int _age, Genders _gender)
            {
                firstName = _firstName;
                lastName = _lastName;
                age = _age;
                gender = _gender;
            }

            public override string ToString()
            {
                return firstName + " " + lastName + " ," + gender + ", age " + age;
            }

            public enum Genders : int { Male, Female };
        }

    }
}






5.Program 3:

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

namespace Csharp
{
    /// <summary>
    /// Example of Reference Type
    /// </summary>
    class Referencetypeclass
    {
        static void Main(string[] args)
        {
            Person p = new Person("Tony", "Allen", 32, Person.Genders.Male);
            Console.Write(p.ToString());
            Console.ReadLine();
        }

        class Person
        {
            public string firstName;
            public string lastName;
            public int age;
            public Genders gender;

            public Person(string _firstName, string _lastName, int _age, Genders _gender)
            {
                firstName = _firstName;
                lastName = _lastName;
                age = _age;
                gender = _gender;
            }

            public override string ToString()
            {
                return firstName + " " + lastName + " ," + gender + ", age " + age;
            }

            public enum Genders : int { Male, Female };
        }
    }
}

No comments:

Post a Comment