Lesson 10 : Operator Overloading , Strings and Regex




User-defined types such as classes,structures etc; often overload operators by defining a member function which is static using the operator keyword.We can not overload each and every operator by this approach

using System;
public class OperatorOverloading
{

//the two integer member variables of class OperatorOverloading
public int num1;
public int num2;

//OperatorOverloading() constructor will be called first in the Main() method

public OperatorOverloading(int num1, int num2)
{
/*The values for first call are num1=5 and num2=7.
For the second call of another instance make them num1=4 and num2=1 */

this.num1 = num1;
this.num2 = num2;
}

//This operator overload has the values of the first instance members as well as second instance
public staticOperatorOverloading operator +(OperatorOverloading ClassInstance1, OperatorOverloading ClassInstance2)
{

/*this line returns a new instance containing the sum of the two members for each of the two constructors
e.g return new OperatorOverloading(ClassInstance1.num1 + ClassInstance2.num1, ClassInstance1.num2 + ClassInstance2.num2);
will have the argument values such as OperatorOverloading(5+4,7+1) and its new instance
* returns the added values of the two instances
*/

return new OperatorOverloading(ClassInstance1.num1 + ClassInstance2.num1, ClassInstance1.num2 + ClassInstance2.num2);
}

//We are overriding the ToString () method by a specific format

public override string ToString()
{
return (String.Format(“{0}x + {1}x”, num1, num2));
}

}

class Program
{
static void Main()
{
OperatorOverloading number1 = new MethodOverloading(5, 7);
OperatorOverloading number2 = new MethodOverloading(4, 1);

OperatorOverloading sum = number1 + number2;

Console.WriteLine(“ttt Algebra Additionn”);
Console.WriteLine(“tttt” + number1);
Console.WriteLine(“tttt”+number2);

Console.WriteLine(“tttt———“);

Console.WriteLine(“tttt”+sum);

Console.ReadKey();

}
}

Output

A little bit of work for you.See other examples of operator overloading on this site.If any confusion.Ask me frankly through comments

Strings and Regular Expressions

Strings are the collections of texts.Unlike old C language; a string doesn’t terminate when it finds /0 character.
We can use both the keywords String or string in C#.Point to be noted that if we are concerned about security issues regarding string manipulation;we have to give importance to the String
keyword.
We have already declared as well as initialized strings in previous lessons.In order to refresh your memory;I will highlight them;

1. string str;
2. string str=”Hello”;
3. string str=String.Empty;
4. string str=@”C:MyDocuments”;
5. var str=”Hi”;
6. const string str=”Don”;
7.char[] str ={ ‘A’, ‘E’, ‘I’,’O’,’U’};
string Characters = new string(str);

Basic String Operations

using System.IO;
using System;

class Program
{
static void Main()
{
String str = “How do you do?”;
Console.WriteLine(str);

String substr = str.Substring(0, 3);

Console.WriteLine(“nSubstring:{0}”, substr);

Console.WriteLine(“nIndex of w:{0}”,str.IndexOf(‘w’));

Console.WriteLine(“nLength:{0}”, str.Length);

Console.WriteLine(“nLower letters string:nn{0} ” ,str.ToLower());

Console.ReadKey();

}
}

The StringBuilder

The StringBuilder provides amazing flexibility when we require more complex operations.System.Text

contains StringBuilder() object.

I am writing my project code to show the power of StringBuilder.Don’t go in details;just see its flexibility

StringBuilder sb = new StringBuilder();
sb.Append(“Update tbl_MoneyMovementInward Set “);

for (int c = 2; c < cOunt; c++) { TextBox txt = Panel1.FindControl("txt" + c.ToString()) as TextBox; if (txt != null) { sb.Append(dtCols.Rows[c][/c][0].ToString() + "=" + "'" + txt.Text + "'"); if (c < cOunt - 1) { sb.Append(","); } } } sb.Append(" Where ID=" + "'" + Request.QueryString["ID"].ToString() + "'"); sb.Append(" AND userID=" + "'" + Request.QueryString["userID"].ToString() + "'"); sb.Append(" AND uniqueID=" + "'" + Request.QueryString["uniqueID"].ToString() + "'");

Empty vs Null Strings and checking them

Regular Expressions

These are the expressions,which are used to play with the strings.They are quite easier to work with.Check these links

Searching With Regex

Splitting String

Joining the strings

Copyrighted.com Registered & Protected 
LCL9-EYWE-IWW6-4RZ3

Here, our topic regarding Operator Overloading , Strings and Regex get finished

Syed Adeel Ahmed
Syed Adeel Ahmed
Analyst, Programmer, Educationist and Blogger at Technofranchise
Computer Systems Engineer from Sir Syed University Of Engineering & Technology.I am passionate about all types of programming.

Published by

Syed Adeel Ahmed

Computer Systems Engineer from Sir Syed University Of Engineering & Technology.I am passionate about all types of programming.