using System;
using System.Text;
class MainClass {
public static string ReverseString(string str)
{
if (str == null || str.Length <= 1)
{
return str;
}
StringBuilder revStr = new StringBuilder(str.Length);
for (int count = str.Length - 1; count > -1; count--)
{
revStr.Append(str[count]);
}
return revStr.ToString();
}
public static void Main() {
Console.WriteLine(ReverseString("The quick brown fox jumped over the lazy dog."));
}
}
Month: February 2011
Length and Indexer
using System;
using System.Text;
class MainClass
{
public static string ReverseString(string str)
{
if (str == null || str.Length <= 1)
{
return str;
}
StringBuilder revStr = new StringBuilder(str.Length);
for (int count = str.Length - 1; count > -1; count--)
{
revStr.Append(str[count]);
}
return revStr.ToString();
}
public static void Main(){
Console.WriteLine(ReverseString("Madam Im Adam"));
Console.WriteLine(ReverseString("The quick brown fox jumped over the lazy dog."));
}
}
Create a StringBuilder which hold 100 characters.
using System;
using System.Text;
class StringApp {
static void Main(string[] args) {
StringBuilder myBuffer = new StringBuilder("My string data");
Console.WriteLine(myBuffer.Capacity);
myBuffer.Append(" contains some numerical data: ");
myBuffer.AppendFormat("{0}, {1}.", 44, 99);
Console.WriteLine(myBuffer.Capacity);
Console.WriteLine(myBuffer);
}
}
Get Next Text Element
using System;
using System.Globalization;
using System.Threading;
class Class1 {
static void Main(string[] args) {
String MyStr, OutBuf;
MyStr = “The Quick programmer ran rings around the lazy manager”;
for (int k=0; k
Get Text Element Enumerator
using System;
using System.Globalization;
using System.Threading;
class Class1 {
static void Main(string[] args) {
TextElementEnumerator Iter;
String MyStr, OutBuf;
MyStr = "The Quick programmer ran rings around the lazy manager";
//Lets do the iterator thing
Iter = StringInfo.GetTextElementEnumerator(MyStr);
while (Iter.MoveNext())
{
OutBuf = "Character at position " +
Iter.ElementIndex.ToString() +
" = " + Iter.Current;
Console.WriteLine(OutBuf);
}
}
}
Use Stopwatch
using System;
using System.Reflection;
using System.Diagnostics;
class OnlyLoad {
static void Main() {
Stopwatch duration = new Stopwatch();
duration.Reset();
duration.Start();
Assembly a = Assembly.Load("library");
duration.Stop();
Console.WriteLine(duration.ElapsedTicks.ToString());
duration.Reset();
duration.Start();
a = Assembly.ReflectionOnlyLoad("library");
duration.Stop();
Console.WriteLine(duration.ElapsedTicks.ToString());
}
}
Starting a new process: open wordpad
// Starting a new process.
using System;
using System.Diagnostics;
public class StartProcess {
public static void Main() {
Process newProc = Process.Start("wordpad.exe");
Console.WriteLine("New process started.");
newProc.WaitForExit();
newProc.Close(); // free resources
Console.WriteLine("New process ended.");
}
}

