Thursday, August 31, 2006

Transforming a List or an ArrayList collection to a C# regular array

1. From an ArrayList to, say, a string[]:
ArrayList oldList = new ArrayList();
[...]
string[] newList = (string[]) oldList.ToArray(typeof(string));


2. From a List to an array T[]. This is very simple:
List<
MyClass > oldList = new List < MyClass >();
[...]
MyClass[] newList = oldList.ToArray();

Saturday, August 26, 2006

C# Classes Versus Structs

I will try to group here the differences and commonalities bewtween classes and structs.

Differences:

Classes are reference types, structs are value types.
Classes are allocated in the Heap, structs are allocated on the stack.
Class instances are passed to methods as references, a copy of the struct is passed to a method.
Classes can declare an explicit parameterless constructor, structs cannot declare an explicit parameterless constructor.
Class members can have initializers (private string s = "asd";) while struct members cannot have initializers (private string s;). They must be initialized in the constructor.
Classes cannot be instantiated without the use of the new operator, structs can be instantiated without the use of the new operator.
A struct cannot inherit from another struct or class (it inherits from object by default).
A struct cannot be the base of another class.


Commonalitites:

Both can implement interfaces.
You CAN call a parameterless constructor for both classes and structs (if the class has one defined; a struct always has it and it is implicit).

Struct usage guidelines (taken from http://www.awprofessional.com/articles/article.asp?p=423349&seqNum=2&rl=1):

DO NOT define a struct unless the type has all of the folloing characteristics:

  • It logically represents a single value, similar to primitive types (int, double, etc.).
  • It has an instance size under 16 bytes.
  • It is immutable.
  • It will not have to be boxed frequently.

Working with the registry

Opening a subkey:

string subKey = @"Software\Play\WindowPos";
RegistryKey key = Registry.CurrentUser.OpenSubKey(subKey, true);

Creating a subkey:

key = Registry.CurrentUser.CreateSubKey(subKey);

The second parameter specifies whether the key is writable or not.

Getting a value:
  • either by using the static method of the Registry class (in this case, the full registry path must be specified):

    Registry.GetValue(Registry.CurrentUser.Name + @"\" + subKey, "X", 300)
  • by using the curent key (relative path):

    object theValue = key.GetValue("X");
Storing a value:

key.SetValue("X", 123);

Storing/Getting an array of strings:

string[] array = new string[2] { "asd", "dfg" };
k.SetValue("myArray", array, RegistryValueKind.MultiString);
object readArray = k.GetValue("myArray");

Comments:
  1. Use Flush() or Close() to commit changes of the registry key.
  2. Other functions of the RegistryKey class: DeleteSubkeyTree(), DeleteValue(), string[] GetSubKeyNames(), GetSubKeyValues().

The Reason Why This Blog Exists

I have been thinking for a long time to finally nail down all those .NET Framework classes that I would read about in various articles, leaving me with a feeling of uncertainty. Now, I have finally decided to have my own space where I could save all the new things I learn about C#. In parallel, I will also try to take each of the .NET assemblies and write a small example and description about each type I encounter. This way, I will have a place to come back to whenever I forget something.