Convert Binary to Base64 String

6 Comments »

Modern applications increasingly use plain text to store and share data, especially in XML and SOAP formats. However, binary data cannot be represented directly in plain text, so one popular method is to convert binary to Base64 format.

Read the rest of this entry »

String.IsNullOrEmpty Shootout

5 Comments »

Given a string ‘s’, which of the following expressions is faster?

1. String.IsNullOrEmpty( s )

2. s == null || s.Length == 0

Read the rest of this entry »

KeyedCollection: Dictionary for Values with Embedded Keys

2 Comments »

If you need a collection of objects accessed by a key, and the key is one of the object’s properties, then you should use the KeyedCollection class instead of Dictionary. For example, you would use a KeyedCollection to store Employee objects accessed by the employee’s ID property.

Read the rest of this entry »

Executing Code in Partial Trust Environments

9 Comments »

When building your first .NET web service, you may be in for a rude awakening when you discover the concept of “partial trust.” Your previously bullet-proof code will suddenly fail in a flurry of exceptions thrown by seemingly innocuous commands such as reading files or accessing the Registry. This article provides a brief overview of Code Access Security and describes how to modify and test your code to work in a partial trust environment.
Read the rest of this entry »

Construct C# Objects at Runtime

No Comments »

Creating C# objects at run-time is easy. Just call the Activator.CreateInstance method.

For example, to create the same type of object as the current object using its default constructor:

Activator.CreateInstance( this.GetType() );

Read the rest of this entry »

C# Universal Type Converter

5 Comments »

Sometimes you need to be able to convert between multiple data types. One solution is to create a “universal type converter,” which is an object that implicitly executes all of the desired type conversions.

Read the rest of this entry »

C# Get Calling Method

6 Comments »

Reflection is a handy mechanism in .NET that enables you to obtain class information, get and set properties, and invoke methods entirely at run-time. Reflection can also provide information about the object and method that called a particular method. This can be useful for debug and trace purposes.

Read the rest of this entry »

Read a Web Page in C#

9 Comments »

Reading the contents of a web page is easy in C# with the System.Net.WebClient class:

Read the rest of this entry »

C# Object Initialization

4 Comments »

When constructing a C# Object, it’s important to understand the order in which the object’s fields and constructors are initialized:

Read the rest of this entry »

Convert Between Synchronous and Asynchronous

6 Comments »

When a program calls a synchronous function, the program halts and waits for the function to finish executing. When a program calls an asynchronous function, the program does not wait and continues to execute while the asynchronous function executes in the background.

By default, C# methods are synchronous. External functions that can take a long time to execute–such as interprocess communications (IPC) and database queries–are typically asynchronous. However, there may be instances where you need to make a synchronous function asynchronous and vice-versa.

Read the rest of this entry »

« go backkeep looking »