Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Overloading Unary operator ~ (to reverse the string)
//Rextester.Program.Main is the entry point for your code. Don't change it. //Compiler version 4.0.30319.17929 for Microsoft (R) .NET Framework 4.5 using System; using System.Collections.Generic; using System.Linq; using System.Text.RegularExpressions; namespace Rextester { class Person { string name; public Person(string st) { name = st; } // string t = ~p1 will call this operator overloaded method public static string operator ~(Person tmp) // unary operator ~ overloaded here, returns string { string t = string.Empty; for(int i=tmp.name.Length-1; i>=0; i--) // reversing the string t=t+tmp.name[i]; return t; } } public class Program { public static void Main(string[] args) { Person p1=new Person("Steve Jobs"); // usually ~ (tilde) sign used for writing destructor // here we give special meaning to reverse the string stored in an object string strRev = ~p1; // hence it must be overloaded, the object 'p1' will be passed to 'tmp' object Console.WriteLine("Reversed : " + strRev); p1=new Person("Mark Zuckerberg"); strRev = ~p1; Console.WriteLine("Reversed : " + strRev); } } }
run
|
edit
|
history
|
help
0
Implementing two interfaces with the same method signature
MyUDA03Code0419-1176.cs
golestanali
generic stack implementation
test2
www
Jhjj
SortedDictionary : Add と foreach
Addition
Fórum SO ➡ Merging 2 lists by Name and adding their Values, using LINQ ♦