Run Code
|
Code Wall
|
Users
|
Misc
|
Feedback
|
About
|
Login
|
Theme
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
Please
log in
to post a comment.
5555555
Random number + IF var change2
My
Prism area
Fórum ➡ Get all combinations for ▶different◀ Id's ( GroupBy version ) ♦
Compare 2 int
linq subquery z grupowaniem po dwóch polach
1
Strings in C# with some properties examples
Server last restarted
Please log in to post a comment.