String Manipulators And Extractors – Part II
Before proceeding with class indexers, let usquickly look at the following piece of code which compares two stringsbut in a slightly different manner in that each string is scanned intoa char variable and then compared. Since each character has to becompared, each character needs to be extracted from the strings. Andsince, a 'char' ('a') cannot be compared with a string or an arraywithout casting it, the character needs to be first scanned into a'char' type variable and then compared. The comparison in this exampleis also based on case i.e. it is case sensitive.
To introduce a little bit of class indexers, Iwill need to draw attention to the concept of properties of a class.Properties can be understood as attributes of aclass. A class indexer at first look looks like a cross between aconstructor and a property. It has the name of a class but is differentfrom the constructor in that it is followed by the 'this' keyword and aparameter which looks like a subscript out of an array and is completedby a get, let or a set method just like in a property. Butthe major attraction of the class indexer is that it can be manipulatedjust like an array and the major difference between a property and aclass indexer is that a property does not have port a 'this' keywordand a class indexer does !
My next example in the following article willfully tackle this glorious extension of the finest of the finercharacteristics of OOPS – Class Indexer. The point to note aboutMicrosoft's languages or products is that it has a WYWIWYG(What YouWish Is What You Get !). A seasoned programmer's wishes are nearlyalways fulfilled if he wishes with a Microsoft's product andMicrosoft's claim that the class indexer will fulfill the wishes ofsuch a programmer may well be justified !
//Class that compares two strings and returns
//a message "Yes" on match and "No" on no match
using System;
class compare{
public void comparestr(string s,string s1){
for (int i=0;i<5;i++){
char ch=s[i];
char ch1=s1[i];
if (ch==ch1){
Console.WriteLine("Yes");
}
else{
Console.WriteLine("No");
}
}
}
}
class abc{
public static void Main(){
compare c1=new compare();
compare c2=new compare();
c1.comparestr("c-sharp","C-Sharp");
}
}












No comments yet... Be the first to leave a reply!