Book Image

Learn C# in 7 days

By : Gaurav Aroraa
1 (1)
Book Image

Learn C# in 7 days

1 (1)
By: Gaurav Aroraa

Overview of this book

This book takes a unique approach to teach C# to absolute beginners. You’ll learn the basics of the language in seven days. It takes a practical approach to explain the important concepts that build the foundation of the C# programming language. The book begins by teaching you the basic fundamentals using real-world practical examples and gets you acquainted with C# programming. We cover some important features and nuances of the language in a hands-on way, helping you grasp the concepts in a fluid manner. Later, you’ll explore the concepts of Object-Oriented Programming (OOP) through a real-world example. Then we dive into advanced-level concepts such as generics and collections, and you’ll get acquainted with objects and LINQ. Towards the end, you’ll build an application that covers all the concepts explained in the book. By the end of this book, you will have next-level skills and a good knowledge of the fundamentals of C#.
Table of Contents (15 chapters)
Title Page
Credits
About the Author
About the Reviewer
www.PacktPub.com
Customer Feedback
Preface

Indexers


An indexer provides a way to access an object via an index like array. For instance, if we define an indexer for a class, that class works similarly to an array. This means the collection of this class can be accessed by index.

Note

Keywordthisis used to define an indexer. The main benefit of indexer is that we can set or retrieve the indexed value without explicitly specifying a type.

Consider the following code snippet:

public class PersonCollection
{
    private readonly string[] _persons = Persons();
    public bool this[string name] => IsValidPerson(name);
    private bool IsValidPerson(string name) =>
    _persons.Any(person => person == name);


    private static string[] Persons() => new[]
    {"Shivprasad","Denim","Vikas","Merint","Gaurav"};
}

The preceding code is a simpler one to represent the power of an indexer. We have a PersonCollection class having an indexer that makes this class accessible via indexer. Please refer to the following code:

private static void...