Jan 18, 2020

C# INDEXER

indexer, indexer in c#, c# foreach with index,c# list indexof,index of c#,
c# indexer

INDEXERS IN C#

“Indexer is a class member, which gives access to a value of class just like array, and class behaves like virtual array not like an original array.” It is used to provide access to the class’s fields outside form the class like properties but that is another case.

SYNTAX

[<modifier>] <type> this [<parameter list>]
{
  [get{statements}]                    //Get accessor
  [set{statements}]                    // Set accessor  
}
Syntax Explanation
            -       Here the modifier may be public private or other.
            -       In type what type of value will provide that may be string, int, decimal or double but the best type is object because it return all the possible type int string double all.
            -       Where this pointing that we are going to define a indexer on the current class in which we declaring indexer.
            -       And the last get and set block will include your logic of getting and setting the values of class. Either you can permit to getting only or setting only but you can permit the both like properties in c#.    
            -       Parameter list may be parameter type list which can be string type, double type, care type or int type etc. and will write like this <int index> or <string name>, but what is the difference between this means if you use int type the index parameter will be number and if you use string type the indexing parameter will be some name on behalf of name we can access the values to get and set. But there is problem with name that is c# is case sensitive and conditioner is most of it if we make any name case diff. to the original then the assignment does not success and return any null name, this issue will be solved by convert the given name into either upper case and check or convert into lower case and check see example to understand.
            We use ‘else if’ construct to provide get and set accessor to the values which will clear in example see below. According to the index number mean 0, 1, 2 or 1, 2, 3 the values will be accessible to get and set.

 c# Indexer examples 
example of index, index examples in c#,b+ tree example,c# static indexer,class indexer c#,c sharp list index
c# indexer examples


EXAMPLES 1 (INDEX TROUGH INTEGER’S VALUES)

            Suppose we have student entity/class and some attributes/fields then we will make two class one for main program and second for the declaring and initializing the values or attributes. Let see
Main class
namespace StudentIndexer
{
    class Program
    {
        static void Main(string[] args)
        {
            StdAttributes std = new StdAttributes(12, "Afreen", "8th", "dadar campus", "Morning", 1200.00);
            Console.WriteLine("Student  Roll Number =" + std[1]);
            Console.WriteLine("Student  Name =" + std[2]);
            Console.WriteLine("Student  Class =" + std[3]);
            Console.WriteLine("Student  Campus Name =" + std[4]);
            Console.WriteLine("Student  Timing =" + std[5]);
            Console.WriteLine("Student  Fees =" + std[6]);
            std[1] = 10; std[2] = "Arslan"; std[3] = "9th standard"; std[4] = "Malwarcampus"; std[5] = "Evening"; std[6] = 1450.00;
            Console.WriteLine();
            Console.WriteLine("Modified Student  Roll Number =" + std[1]);
            Console.WriteLine("Modified Student  Name =" + std[2]);
            Console.WriteLine("Modified Student  Class =" + std[3]);
            Console.WriteLine("Modified Student  Campus Name =" + std[4]);
            Console.WriteLine("Modified Student  Timing =" + std[5]);
            Console.WriteLine("Modified Student  Fees =" + std[6]);
            Console.ReadKey();
        }
    }
}

Attribute/fields class
namespace StudentIndexer
{
    class StdAttributes
    {
        int Stdno;
        string Stdname, Stdclass, Stdcampus, Stdtiming;
        double Stdfee;
        public StdAttributes(int Stdno,string Stdname,string Stdclass,string Stdcampus,string Stdtiming,double Stdfee)
        {
            this.Stdno = Stdno;
            this.Stdname = Stdname;
            this.Stdclass = Stdclass;
            this.Stdcampus = Stdcampus;
            this.Stdtiming = Stdtiming;
            this.Stdfee = Stdfee;
        }
        public object this[int index]
        {
            get
            {
                if (index == 1)
                    return Stdno;
                else if (index == 2)
                    return Stdname;
                else if (index == 3)
                    return Stdclass;
                else if (index == 4)
                    return Stdcampus;
                else if (index == 5)
                    return Stdtiming;
                else if (index == 6)
                    return Stdfee;
                return null;
            }
            set
            {
                if (index == 1)
                    Stdno =(int)value;
                else if (index == 2)
                    Stdname = (string)value;
                else if (index == 3)
                    Stdclass = (string)value;
                else if (index == 4)
                    Stdcampus = (string)value;
                else if (index == 5)
                    Stdtiming = (string)value;
                else if (index == 6)
                    Stdfee = (double)value;
                else;
            }
        }
    }

EXAMPLES (INDEX TROUGH STRING NAME)
Main class
namespace StudentIndexer
{
    class Program
    {
        static void Main(string[] args)
        {
            StdAttributes std = new StdAttributes(12, "Afreen", "8th", "dadar campus", "Morning", 1200.00);
            Console.WriteLine("Student  Roll Number =" + std["stdno"]);
            Console.WriteLine("Student  Name =" + std["stdname"]);
            Console.WriteLine("Student  Class =" + std["stdclass"]);
            Console.WriteLine("Student  Campus Name =" + std["Stdcampus"]);
            Console.WriteLine("Student  Timing =" + std["stdTiming"]);
            Console.WriteLine("Student  Fees =" + std["stdfee"]);
            std["stdno"] = 10; std["stdname"] = "Arslan"; std["stDclass"] = "9th standard"; std["stdcampus"] = "Malwarcampus"; std["stdtiming"] = "Evening"; std["stdfee"] = 1450.00;
            Console.WriteLine();
            Console.WriteLine("Modified Student  Roll Number =" + std["stdno"]);
            Console.WriteLine("Modified Student  Name =" + std["stdname"]);
            Console.WriteLine("Modified Student  Class =" + std["stDclass"]);
            Console.WriteLine("Modified Student  Campus Name =" + std["stdcampus"]);
            Console.WriteLine("Modified Student  Timing =" + std["stdtiming"]);
            Console.WriteLine("Modified Student  Fees =" + std["stdfee"]);
            Console.ReadKey();
        }
    }
}

Attributes/fields Class
namespace StudentIndexer
{
    class StdAttributes
    {
        int Stdno;
        string Stdname, Stdclass, Stdcampus, Stdtiming;
        double Stdfee;
        public StdAttributes(int Stdno,string Stdname,string Stdclass,string Stdcampus,string Stdtiming,double Stdfee)
        {
            this.Stdno = Stdno;
            this.Stdname = Stdname;
            this.Stdclass = Stdclass;
            this.Stdcampus = Stdcampus;
            this.Stdtiming = Stdtiming;
            this.Stdfee = Stdfee;
        }
        public object this[string name]
        {
            get
            {
                if (name.ToLower()== "stdno")
                    return Stdno;
                else if (name.ToLower() == "stdname")
                    return Stdname;
                else if (name.ToLower() == "stdclass")
                    return Stdclass;
                else if (name.ToLower() == "stdcampus")
                    return Stdcampus;
                else if (name.ToLower() == "stdtiming")
                    return Stdtiming;
                else if (name.ToLower() == "stdfee")
                    return Stdfee;
                return null;
            }
            set
            {
                if (name.ToLower() == "stdno")
                    Stdno =(int)value;
                else if (name.ToLower() == "stdname")
                    Stdname = (string)value;
                else if (name.ToLower() == "stdclass")
                    Stdclass = (string)value;
                else if (name.ToLower() == "stdcampus")
                    Stdcampus = (string)value;
                else if (name.ToLower() == "stdtiming")
                    Stdtiming = (string)value;
                else if (name.ToLower() == "stdfee")
                    Stdfee = (double)value;
                else;
            }
        }
    }

name.ToLower() =="stdno"   this keyword ToLower first change the given name/sentences into lower case and then  compare the given word and then decides results. 

0 comments:

Post a Comment

Please do not enter any spam link in the comment box