Perform Android Like Search in conjunction with Expression-Bodied Property of C# 6.0

Rajnilari2015
Posted by in C# category on for Beginner level | Points: 250 | Views : 526 red flag
Rating: 5 out of 5  
 1 vote(s)

Sometime back, in the DNF forum a question has been asked about implementing an search like Android one. This post is an attempt to provide an answer for the same by providing a demonstration of using the Expression-Bodied Property of C# 6.0


 Download source code for Perform Android Like Search in conjunction with Expression-Bodied Property of C# 6.0

Introduction

Sometime back, in the DNF forum a question has been asked about implementing an search like Android one. This post is an attempt to provide an answer for the same by providing a demonstration of using the Expression-Bodied Property of C# 6.0

Using the code

Let us first create a Person class and expose a property say PersonName and populate some data to it using the Expression-Bodied Property of C# 6.0 as shown below

public class Person
{
    //Kindly note that the PersonName has been populated by using expression-bodied property of C#6.0
    public List<string> PersonName => new List<string>
    {
        "Rajlakshmi Biswas",
        "Niladri Biswas",
        "Arina Biswas",
        "RNA Team",
        "Mahesh Nagar",
        "Manish Nagar",
        "Shan Mani",
        "Ghanshaym rama"
        
    };
}

Kindly notice the way the string values has been set to the PersonName property using the Expression-Bodied Property(=>) of C# 6.0. It's the Lambda way.

IL DASM reveals the below, however

Person is an instance class and has a property name PersonName but it is really a getter method. So Expression-Bodied Properties are syntactic sugar for the getter methods.Let's look at the same from another perspective.

Though we have not explicitly invoked the Add() method of the collection, but the compiler did that implicitly.

Now it's time to invoke the PersonName property.Let's do in the below way

 var personNames = new Person().PersonName;

Also let's have another look that the PersonName property is read-only

Now once we have the collection at our disposal, we can write the below lines of code

//get the Person names
var personNames = new Person().PersonName;

//set the search term
string toSearch = "Sh";

//set the delimeter
string[] stringSeparator = new string[] { " " };

The next objective is to loop through each item of the collection and split that item based on the delimiter provided. If in any of the delimited values Starts with the search criteria then we need to display the output as asked in the question.

So let us go ahead and implement the below piece of code

static void Main(string[] args)
{
    //get the Person names
    var personNames = new Person().PersonName;

    //set the search term
    string toSearch = "Sh";

    //set the delimeter
    string[] stringSeparator = new string[] { " " };

    personNames

    .ForEach(str => //loop each item in the collection

            //notice that it is statement lambda
    {

        if (str .Split(stringSeparator, StringSplitOptions.None) //split the string into parts as per the delimeter

                  .Any(i => i.StartsWith(toSearch)) //if any of the test condition satisfies

            ) Console.WriteLine(str.Replace(toSearch, "<b>" + toSearch + "</b>")); //perfom the action

    });

    Console.ReadKey();

}

Notice the use of Any() Extension method. It returns a Boolean value if any element of the provided data source matches the test condition.The output is as under

Change: string toSearch = "Ma"; and the result

References

a) Properties
b) Enumerable.Any

Conclusion

So in this article, we learnt

  1. Use of Expression-Bodied Property
  2. What Expression-Bodied Property is at the compiler level
  3. How to use Any Extension Method for searching an element inside a collection
  4. Usage of Statement Lambda

Hope this will be helpful.Thanks for reading.Zipped file attached.

Recommendation
Read Nested Class In C# after this article.
Page copy protected against web site content infringement by Copyscape

About the Author

Rajnilari2015
Full Name: Niladri Biswas (RNA Team)
Member Level: Platinum
Member Status: Member,Microsoft_MVP,MVP
Member Since: 3/17/2015 2:41:06 AM
Country: India
-- Thanks & Regards, RNA Team


Login to vote for this post.

Comments or Responses

Login to post response

Comment using Facebook(Author doesn't get notification)