Sowji's Blog

It's beautiful life
posts - 8, comments - 8, trackbacks - 0

My Links

Archives

Post Categories

Advertisements at BlogUnlimited.com

C# Concepts

The best example for ref usage is given below for swapping to strings. "ref" object mainly used to do deal with In and out parameters. In the example given below, by passing only two parameters you can see the swapped variables (Two output variables).

Multiple Interface Inheritance and the Abstract class Inheritance is also implemented to know the syntax how it should be declared.

Soon I will be addig some more explanation for below example.

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

namespace MyTest
{
    class SwappingStrings
    {
        static void SwapStrings(out string so1, out string so2, string s1, string s2)
        // The string parameter is passed by reference.
        // Any changes on parameters will affect the original variables.
        {
            string temp = s1;
            s1 = s2;
            s2 = temp;
            so1 = s1;
            so2 = s2;
            System.Console.WriteLine("Inside the method: {0} {1} {2} {3}", s1, s2, so1, so2);
        }
        static void Main(string[] args)
        {
            string str1 = "John";
            string str2 = "Smith";
            System.Console.WriteLine("Inside Main, before swapping: {0} {1}", str1, str2);

            SwapStrings(out str1, out str2, str1, str2);   // Passing strings by reference
            System.Console.WriteLine("Inside Main, after swapping: {0} {1}", str1, str2);
            Console.ReadLine();


            ITest1 t = new MultipleInterFaceClass();
            MultipleInterFaceClass t2 = t as MultipleInterFaceClass;
            ITest t3 = t2 as ITest;
            int a = 9;

            System.Console.WriteLine("Interface Inheritance output: {0} {1}", t3.AddDouble(a, a).ToString(), t.AddDouble(a, a).ToString());
            Console.ReadLine();


            AIICllass sub = new AIICllass();
            Atest baseClass = sub as Atest;
            System.Console.WriteLine(("Abstract Inheritance output: {0} {1}", sub.Concat1("2", "3").Tostring() , baseClass.Concat1("4","5")).Tostring();
            Console.ReadLine();
        }
    }

    public abstract class Atest
    {
        public abstract int Add(int a, int b);
        public abstract double AddDouble(double a, double b);

        public virtual string Concat(string a, string b)
        {
            return a + b;
        }

        internal string Concat1(string a, string b)
        {
            return a + b;
        }
    }

    public interface ITest
    {
        int Add(int a, int b);
        double AddDouble(double a, double b);
    }

    public interface ITest1
    {
        int AddInt(int a, int b);
        double AddDouble(double a, double b);    
    }

    public class Test : Atest
    {
        public override int Add(int a, int b)
        {
            // write appropriate implementation here or:  
            throw new NotImplementedException();
            // or possibly NotSupportedException() if the operation should can't ever be supported
        }
        public override double AddDouble(double a, double b)
        {
            // write appropriate implementation here or:  
            throw new NotImplementedException();
            // or possibly NotSupportedException() if the operation should can't ever be supported
        }

        public override string Concat(string a, string b)
        {
            return a + b;
        }
    }


    public class MultipleInterFaceClass : ITest, ITest1
    {

        #region ITest Members

        int ITest.Add(int a, int b)
        {
            return 2;
        }

        double ITest.AddDouble(double a, double b)
        {
            return 3;
        }

        #endregion

        #region ITest1 Members

        int ITest1.AddInt(int a, int b)
        {
            return 4;
        }

        double ITest1.AddDouble(double a, double b)
        {
            return 5;
        }

        #endregion
    }

    public class AIICllass : Atest, ITest, ITest1
    {
        public override int Add(int a, int b)
        {
            throw new NotImplementedException();
        }

        public override double AddDouble(double a, double b)
        {
            throw new NotImplementedException();
        }

        public string Concat1(string a, string b)
        {
            return a + b + 1;
        }

        #region ITest1

        int ITest1.AddInt(int a, int b)
        {
            throw new NotImplementedException();
        }

        double ITest1.AddDouble(double a, double b)
        {
            throw new NotImplementedException();
        }

        #endregion

        #region ITest Members

        int ITest.Add(int a, int b)
        {
            throw new NotImplementedException();
        }

        double ITest.AddDouble(double a, double b)
        {
            throw new NotImplementedException();
        }

        #endregion
    }

}

 

 Output :

 

Inside Main, before swapping: John Smith
Inside the method: Smith John Smith John
Inside Main, after swapping: Smith John

Interface Inheritance output: 3 5

Abstract Inheritance output : 231 45
 

Print | posted on Monday, November 28, 2011 5:22 PM |


Advertisements at BlogUnlimited.com

Feedback

# re: C# Concepts

Nice Post...Sowjanya.
how are you...are you in Hyd or bangaore ?
Me currently in US. send me your contact number to shyammaddi@gmail.com
6/7/2012 10:31 PM | Syam


Post Comment

Title  
Name  
Email
Url
Comment   
Please add 7 and 6 and type the answer here: