using System;
using System.Text;
class StringBuilderAppendFormat {
static void Main(string[] args)
{
StringBuilder buffer = new StringBuilder();
string string1, string2;
string1 = "This {0} costs: {1:C}.
";
object[] objectArray = new object[ 2 ];
objectArray[ 0 ] = "Software";
objectArray[ 1 ] = 1234.56;
buffer.AppendFormat( string1, objectArray );
string2 = "Number:{0:d3}.
" +
"Number right aligned with spaces:{0, 4}.
" +
"Number left aligned with spaces:{0, -4}.";
buffer.AppendFormat( string2, 5 );
Console.WriteLine(buffer.ToString());
}
}
Month: February 2011
StringBuilder EnsureCapacity method
using System;
using System.Text;
class StringBuilderFeatures
{
static void Main(string[] args)
{
StringBuilder buffer =new StringBuilder( “Hello, how are you?” );
string output = “buffer = ” + buffer.ToString() +
”
Length = ” + buffer.Length +
”
Capacity = ” + buffer.Capacity;
buffer.EnsureCapacity( 75 );
output += ”
New capacity = ” +buffer.Capacity;
// truncate StringBuilder by setting Length property
buffer.Length = 10;
output += ”
New length = ” +
buffer.Length + ”
buffer = “;
// use StringBuilder indexer
for ( int i = 0; i < buffer.Length; i++ )
output += buffer[ i ];
Console.WriteLine( output);
}
}
[/csharp]
Use stackalloc to allocate memory for integer array
using System;
class TestStackallocApp
{
unsafe public static void Foo(int* pa)
{
for (int* ip = pa; ip < (pa+5); ip++)
{
Console.WriteLine("value {0} at address: {1}", *ip, (int)ip);
}
}
static void Main(string[] args)
{
unsafe
{
int* pa = stackalloc int[5];
pa[0] = 12;
pa[1] = 34;
pa[2] = 56;
pa[3] = 78;
pa[4] = 90;
Foo(pa);
}
}
}
[/csharp]
allocates 26 characters on the stack the for loop assigns alphabetic characters to each element
using System;
public unsafe class Starter {
public static void Main() {
char* pChar = stackalloc char[26];
char* _pChar = pChar;
for (int count = 0; count < 26; ++count) {
(*_pChar) = (char)(((int)('A')) + count);
++_pChar;
}
for (int count = 0; count < 26; ++count) {
Console.Write(pChar[count]);
}
}
}
[/csharp]
Invalid Cast Exceptions with Implicit Operators
public class TestClass
{
private MainClass MyMainClassObject;
public TestClass()
{
MyMainClassObject = new MainClass();
}
public static implicit operator MainClass(TestClass Source)
{
return Source.MyMainClassObject;
}
}
public class MainClass
{
public static void Main()
{
object MyObject;
MainClass MyMainClassObject;
MyObject = new TestClass();
MyMainClassObject = (MainClass)MyObject;
}
}
Allocating Memory from the Stack
using System;
public class MyClass
{
public unsafe static void Main()
{
int * buf = stackalloc int [5];
for(int i = 0; i < 5; i++) buf[i] = i; for(int i = 0; i < 5; i++) Console.WriteLine(buf[i]); } } [/csharp]
stackalloc Demo
using System;
class MainEntryPoint {
static unsafe void Main() {
Console.Write(“How big an array do you want?
> “);
string userInput = Console.ReadLine();
uint size = uint.Parse(userInput);
long* pArray = stackalloc long[(int)size];
for (int i = 0; i < size; i++)
pArray[i] = i * i;
for (int i = 0; i < size; i++)
Console.WriteLine("Element {0} = {1}", i, *(pArray + i));
}
}
[/csharp]

