Total Pageviews

Tuesday, 20 December 2011

ref keyword in C#

By using 'ref' keyword we keeps the same address of two different variable.

without ref Scenario : When you does at normal functioning means without use of ref keyword, pass argument value from calling method to method defination at that time there is different addresses for both argument and parameter.

ref Scenario : ref parameter in method defination refers the same address of argument at calling method and parameter at method defination.

Below are some images that will explore ref keyword program with flow :

Look at Snap no. 1: Program will start from Main (..) method , here I am assigning a text to string variable.



Look at Snap no. 2: From previous snap, there is wrong statement so, i want to correct that statement by using ref keyword.


Look at Snap no. 3: calling method
method(ref femaleVoiceIsLikeMale);
here, argumnet name femaleVoiceIsLikeMale followed by ref keyword, means that argument value goes into perticuler memory loaction picturized as below :

Female voice is exactly equal to Male voice : Wrong statement
- value
                                        200000
- address

argument value and address passing to method body parameter at class ref_keyword.


Look at Snap no. 4: argument value and address passed to method body parameter, but here I am going to change the argument value that was passed from calling method as :
 "Female voice is exactly different than Male voice : Correct statement"


 Look at Snap no. 5:

 Now memory location value part will reflect as below [take refrence here from Snap no 3]:
  
Female voice is exactly different than Male voice : Correct statement
- value
                                        200000
- address


Greetings !!! address [20000] remain same but value got changed.


 Look at Snap no. 6:

 returning changed value at same address ..

 Look at Snap no. 7: Here we proved smart use of ref keyword
 Output :




Copy Full Code:
namespace ConsoleApplication1
{
    class ref_keyword
    {
        public static void method(ref string femaleVoiceIsNotLikeMale)
        {
            femaleVoiceIsNotLikeMale = "Female voice is exactly different than Male voice : Correct statement";
        }
        public static void Main(string[] args)
        {
            string femaleVoiceIsLikeMale = "Female voice is exactly equal to Male voice : Wrong statement";

            method(ref femaleVoiceIsLikeMale);

            Console.WriteLine(femaleVoiceIsLikeMale);

            Console.ReadKey();
        }
    }
}

Thursday, 15 December 2011

Indexer in C#

By means of indexer you can index to your class object.I would say it nothing but an array.More flexible to accessing the class simple array syntax.

Look at snap No.1 : Here indexer Demo object is created as ' i ' of class Indexer.



Look at snap No.2 : Here I am trying to assign value 5 at perticuler index [3] of Object ' i ' of class Indexer.


Look at snap No.3 : How value is going to set at set {..} accessor of  ' this ' indxer place, at class Indexer.



Look at snap No.4 : Value 5 assingned at perticuler index [3] of Object ' i ' of class Indexer.



Look at snap No. 5 : Here I am trying to assign value 6 at perticuler index [5] of Object ' i ' of class Indexer.



Look at snap No. 6 : How value is going to set at set {..} accessor of  ' this ' indxer place, at class Indexer.



Look at snap No. 7 : Value 6 assingned at perticuler index [5] of Object ' i ' of class Indexer.



Look at snap No. 8 : for loop rotation to reach at last index position at intArray[..] decleared at Indexer class.That will print 11 number of time from 0 - 10 at commond prompt.


Look at snap No. 9 : Here set values are returning from get {..} accessor of  indexer ' this ' place at Indexer class.

 



Look at snap No. 10 : Values are set at prefered index location of Object ' i ' of class indexer.


  Look at snap No. 10 : Indexer Output at Command prompt.




Copy Full Code:
namespace ConsoleApplication1
{
    class Indexer
    {

        int[] intArray = new int[11];

        public int this[int index]
        {

            get
            {

                return intArray[index];

            }

            set
            {

                intArray[index] = value;

            }

        }


        public class Demo
        {

            static void Main(string[] args)
            {

                Indexer i = new Indexer();

                i[3] = 5;

                i[5] = 6;



                for (int j = 0; j <= 10; j++)
                {

                    Console.WriteLine("Value at Index of [{0}]={1}", j, i[j]);

                }

                Console.ReadKey();

            }

        }

    }


}

Tuesday, 13 December 2011

Virtual function in C# .

Virtual function is useful in C# where you have feasibility to modify the function by means of override the same function at derived class place.Look into provided images step by step.








                                        
                                          Here is output at command prompt.!!!

                                         

Copy Full Code:
namespace ConsoleApplication1
{
    class Program
    {
        string n = string.Empty;
        virtual public string myData(string s)
        {
            n = s + "Kant";
            return n;
        }

        class Program1 : Program
        {
            public override string myData(string s)
            {
                return base.myData(s) + " Patil";
            }
        }

        static void Main(string[] args)
        {
            Program1 p = new Program1();
            Console.WriteLine(p.myData("Shashi"));
            Console.ReadKey();
        }
    }
}