Giter Club home page Giter Club logo

indexers_csharp_practice's Introduction

Indexers in C#

An indexer allows an object to be indexed such as an array.

When you define an indexer for a class, this class behaves similar to a virtual array.

You can then access the instance of this class using the array access operator ([ ]).

In order to understand the scenario lets create a Person class.

Give some attributes like Id and name to it.

class Person

{

public int EmpId { get; set; }

public string EmpName { get; set; }

}

Now let's create a class Company.

We are already aware of the list class, lets create a Person type list.

After that set some initial values of Id and name using constructor.

In this way we have a “Person” as a data type in the list that we are going to create.

class Company

{

private List listPersons = new List();

public Company()

{

listPersons.Add(new Person { EmpId = 1, EmpName = "Awias" }) ;

listPersons.Add(new Person { EmpId = 2, EmpName = "Ali" });

listPersons.Add(new Person { EmpId = 3, EmpName = "Ahmad" });

}

}

Create Indexer

Now we have almost all the ingredients to create an indexer that can be useful in our example.

Indexers have a return type, they have parameters and get/set accessors.

Now I am going to create a string type indexer with parameter “EmpId”.

public string this[int empId]

After that we have to define get and set accessors.

In get accessor traverse the listPersons and find the id that matches with the parameter passed.

After that return the name against that Id.

get

{

foreach(var per in listPersons)

{

if(per.EmpId == empId)

{

return per.EmpName;

}

}

return null;

}

In set accessor we can find the object which has the id same as the passed parameter and change its name.

set

{

foreach (var per in listPersons)

{

if (per.EmpId == empId)

{

per.EmpName = value;

}

}

}

Overloaded Indexer

We can have multiple indexers in a class, but they must have differrnt parameters.

indexers_csharp_practice's People

Contributors

zyn10 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.