stackoverflow1453

Programming&&Games&&Life

Archive for September, 2015

C# Indexer Example

using System;

namespace IndexExample3
{
    class Box
    {
        private string[] names = { "length", "width", "height" };
        private double[] dimensions = new double[3];

        public Box(double length, double width, double height)
        {
            dimensions[0] = length;
            dimensions[1] = width;
            dimensions[2] = height;
        }

        //get and set dimension by entering index of array in main
        public double this[int index]
        {
            get
            {
                return dimensions[index];
            }
            set
            {
                dimensions[index] = value;
            }

        }
        //get and set dimension by entering string 
        public double this[string p]
        {
            get
            {
                int j = 0;
                while (j < 3)
                {
                    if (names[j] == p)
                    {
                        return dimensions[j];
                    }
                    j++;
                }
                return -1;
            }
            set
            {
                int j = 0;
                while (j < 3)
                {
                    if (names[j] == p)
                    {
                        dimensions[j] = value;
                    }
                    j++;
                }
            }
        }
    }
}
///////////////////////////////////////
///////////////////////////////////////
using System;

namespace IndexExample3
{
    class Program
    {
        static void Main(string[] args)
        {
            Box mybox = new Box(5.21, 10.22,32.3311);
            Console.WriteLine(mybox[1]);
            Console.WriteLine(mybox["length"]);
            Console.WriteLine(mybox["width"]);
            Console.WriteLine(mybox["height"]);
            Console.WriteLine("/////");
            //set dimension by index
            mybox[1] = 11111.22;
            Console.WriteLine(mybox[1]);            
            Console.WriteLine(mybox["length"]);
            Console.WriteLine(mybox["width"]);
            Console.WriteLine(mybox["height"]);
            Console.WriteLine("/////");
            //set dimension by name
            mybox["length"] = 23;
            Console.WriteLine(mybox["length"]);
            Console.WriteLine(mybox["width"]);
            Console.WriteLine(mybox["height"]);

        }
    }
}

C# Overloaded Indexers

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

namespace IndexerExample
{
    class Konteyner
    {
        private string[] _myarr = new string[size];
        static public int size = 15;

        //Property indexer for setting array
        public string this[int indekis]
        {

            get
            {
                return _myarr[indekis];
            }

            set
            {
                _myarr[indekis] = value;

            }
        }
        //To get the index of the inputted string
        public int this[string x]
        {

            get
            {
                int j = 0;
                while (j < size)
                {
                    if (_myarr[j] == x)
                    {
                        return j;
                    }
                    j++;
                }
                return j;
            }
        }
    }
}
///////////////////////////////////////
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace IndexerExample
{
    class Program
    {
        static void Main(string[] args)
        {
            Konteyner mykont = new Konteyner();

            //fill array
            mykont[0] = "En";
            mykont[1] = "Büyük";
            mykont[2] = "Cimbom";
            
            //print output
            for (int i = 0; i < Konteyner.size; i++)
			{
                Console.WriteLine(mykont[i]);
			}
            
            Console.WriteLine(mykont["Cimbom"]);

        }
    }
}

C# Indexers

using System;

namespace Indexer
{
    class IndexedNames
    {
        static public int size = 10;
        private string[] namelist = new string[size];

        //initialize namelist (constructor)
        public IndexedNames()
        {
            for (int i = 0; i < size; i++)
            {
                namelist[i] = "N.A";
            }
        }
        //get and set each index
        public string this[int indeks]
        {
            get
            {
                string tmp;
                tmp = namelist[indeks];
                return tmp;
            }

            set
            {
                namelist[indeks] = value;
            }
        }

        //use a string as index 
        //Overloaded Indexers
        //Indexers can be overloaded. 
        //Indexers can also be declared with multiple parameters and each parameter may be a different type. 
        //It is not necessary that the indexes have to be integers. 
        //C# allows indexes to be of other types, for example, a string.
        public int this[string namea]
        {
            get
            {
                int j = 0;
                while (j < size)
                {
                    if (namelist[j] == namea)
                        return j;
                    j++;
                }
                return j;
            }

        }
    }
}
//////////////////////////////////////////

using System;
namespace Indexer

{
    class Program
    {
        static void Main(string[] args)
        {
            IndexedNames my = new IndexedNames();
            my[0] = "Zara";
            my[1] = "Riz";
            my[2] = "Nuha";
            my[3] = "Asif";
            my[4] = "Davinder";
            my[5] = "Sunil";
            my[6] = "Rubic";

            for (int i = 0; i < 10; i++)
            {
                Console.WriteLine(my[i]);
            }

            Console.WriteLine("///////////////////////////////");
            //get the index of "Asif"!
            Console.WriteLine(my["Asif"]);
        }
    }
}

This keyword

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

namespace ConsoleApplication5
{
    class Karsilastirma
    {
        //instance variable
        private int bardak=10;

        //method
        public void BuyukDonder(int bardak)
        {
            if (bardak>this.bardak)
            {
                Console.WriteLine("{0}>{1}",bardak, this.bardak);
            }
            else
            {
                Console.WriteLine("{0}<{1}", bardak, this.bardak);
            }
        }
    }
}
//////////////////////////////////////
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication5
{
    class Program
    {
        static void Main(string[] args)
        {
            Karsilastirma k = new Karsilastirma();

            k.BuyukDonder(53);
        }
    }
}

Composition example C#

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

namespace Override
{
    class date
    {
        private int year;
        private int month;
        private int day;

        public date(int theyear, int themonth, int theday)
        {
            year = theyear;
            month = themonth;
            day = theday;
        }

        public override string ToString()
        {
            return year+"/"+month+"/"+day;
        }

    }
}
//////////////////////////////////////////
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Override
{
    class Calisan
    {
        private string isim;
        private string soyisim;
        private date dogumtar;
        private date isegiristar;

        public Calisan(string theisim, string thesoyisim, int dogumyil, int dogumay, int dogumgun, int isegirisyil, int isegirisay, int isegirisgun)
        {
            isim = theisim;
            soyisim = thesoyisim;
            dogumtar = new date(dogumyil, dogumay, dogumgun);
            isegiristar = new date(isegirisyil, isegirisay, isegirisgun);
        }

        public override string ToString()
        {
            return isim+" "+soyisim+" "+dogumtar.ToString()+" "+isegiristar.ToString();
        }
    }
}
//////////////////////////////////////

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

namespace Override
{
    class Program
    {
        static void Main(string[] args)
        {        

            Calisan m = new Calisan("Abuzer", "Kadayif", 1980, 29, 11, 2015, 03, 21);
            Console.WriteLine(m);

        }
    }
}

C# Classes (basic)

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

namespace Tekrar
{
    class Program
    {
        static void Main(string[] args)
        {
            Zaman x = new Zaman();
            x.S = 11;
            x.D = 59;
            x.SN = 12;
            Console.WriteLine(x);

            x.SetTime(10, 23, 11);
            Console.WriteLine(x);
        }
    }
}

///////////////////////////////////////

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

namespace Tekrar
{
    class Zaman
    {
        private int saat;
        private int dakika;
        private int saniye;

        public Zaman()
        {
            saat = 0;
            dakika = 0;
            saniye = 0;
        }

        //For setting Class Variables (Getter and Setters)

        public int S
        {
            get { return saat; }
            set { saat = value; }
        }

        public int D
        {
            get { return dakika; }
            set { dakika = value; }
        }

        public int SN
        {
            get { return saniye; }
            set { saniye = value; }
        }

        public void SetTime(int hour, int minute, int second)
        {

            saat = hour;
            dakika = minute;
            saniye = second;

        }


        public override string ToString()
        {
            //return "saat: " + saat + " dakika:" + dakika + " saniye: " + saniye;
            return String.Format("{0:D2}:{1:D2}:{2:D2}", saat, dakika, saniye);
        }
    }
}