using System;
using System.Text;
class Test {
public static void DisplayProperties(string name,StringBuilder myStringBuilder) {
// display the properties for the StringBuilder object
Console.WriteLine(name + ".Length = " + myStringBuilder.Length);
Console.WriteLine(name + ".Capacity = " + myStringBuilder.Capacity);
Console.WriteLine(name + ".MaxCapacity = " + myStringBuilder.MaxCapacity);
}
public static void Main() {
StringBuilder myStringBuilder = new StringBuilder();
int capacity = 50;
StringBuilder myStringBuilder2 = new StringBuilder(capacity);
int maxCapacity = 100;
StringBuilder myStringBuilder3 = new StringBuilder(capacity, maxCapacity);
string myString = "www.kutayzorlu.com/java2s/com";
StringBuilder myStringBuilder4 = new StringBuilder(myString);
int startIndex = 0;
int stringLength = myString.Length;
StringBuilder myStringBuilder5 = new StringBuilder(myString, startIndex, stringLength, capacity);
// display the StringBuilder objects' properties
DisplayProperties("myStringBuilder", myStringBuilder);
DisplayProperties("myStringBuilder2", myStringBuilder2);
DisplayProperties("myStringBuilder3", myStringBuilder3);
DisplayProperties("myStringBuilder4", myStringBuilder4);
DisplayProperties("myStringBuilder5", myStringBuilder5);
}
}
Language Basics
Demonstrating StringBuilder AppendFormat
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());
}
}
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]