Wednesday, June 18, 2008

C# antics

Alright here are some things abt C#

? Nullable value:
int? value , implies that value is an integer and is nullable.

?? Null Coalescing Operator
Takes 2 operands and returns the left operand if it is not null else returns the right operand

? nullableObject = null;
temp = nullableObject ?? new SomeObjectType();
this is equivalent to
temp = (nullableObject.HasValue )? nullableObject.Value : new SomeObjectType();
string filename = GetFileName() ?? "Untitled File";

@ Operator
string path = "C:\\Program Files\\MyPage" and
string path = @"C:\Program Files\MyPage"
are the same. "@" tells the compiler that the string should be treated as verbatim and so no escaping of characters is required.
Converting From Streams and Back
Here is a sample where I convert from a stream to a byte array:

strm = mqMsg.BodyStream;
BinaryReader br = new BinaryReader(strm);
byteArray= br.ReadBytes(Convert.ToInt32(strm.Length));

Here is a sample where I convert from a byte array to a stream:

MemoryStream memStream = new MemoryStream();
BinaryWriter binWriter = new BinaryWriter(memStream);
binWriter.Write(byteArray);

No comments: