# Wisenheimer Brainstorm Wiki

### Site Tools

notes:csharp:miscellaneous

# Miscellaneous topics in .NET

### Command line arguments

Example: Enumerate command line arguments:

// the arguments are placed in a string[] array
foreach(string arg in Environment.GetCommandLineArgs())
{
// ...
}

### Conditional operators

The null conditional operator (C# 6) indicates that the code should not throw a NullReferenceException exception if the handler variable is null:

Delegate handler = null;
handler?.Invoke();

The above is equivalent to the following code in previous versions of the C#:

Delegate handler = null;
if (handler != null)
{
handler.Invoke();
}

Equivalent expressions with and without using a conditional operator:

int SomeMethod(int a, int b)
{
return (a<b ? 1 : a>b ? 2 : 3);
}
int SomeMethod(int a, int b)
{
if (a<b)
return 1;
else if (a>b)
return 2;
else
return 3;
}

The conditional operator and nullable types:

int a = 0;
int b = 1;

int? x = (a == 0 ? new int?() : b); // x == null

// The following statement does not compile.
// Error: Type of conditional expression cannot be determined because there is no implicit
// conversion between '<null>' and 'int'
int? x = (a == 0 ? null : b);

The null coalescing operator ?? returns the left operand if it's not null; otherwise, the right operand.

• The first operand of the ?? operator must be a nullable type or reference type
• The second operand of the ?? operator must be of the same type as the first or of a type that is implicitly convertible to the type of the first operant
• If the first operant is not null, the expression has the value of the first operand
• If the first oparand is null, the expression has the value of the second operand
• The null coalescing operator is right associative
int? n = null;

// If n is null, assign -1 to i.
int i = n ?? -1;

// Null-coalescing operator is right associative.
int? x = null;
int? z = null;
int y = x ?? z ?? -1; // chain null-coalescing operators

// If the type of the second operand is the underlying type of the first operand
// then the result is that of the underlying type.
int? a = null;
int b = 5;
int c = a ?? b; // c == 5 of type int

### Custom formatting

Example: Custom formatting using a ToString method that accepts a format string:

class Book
{
public string ToString(string format)
{
// "G" represents a common format. It is the same as calling ToString().
// A null value for the format string should also display the common format.
if (String.IsNullOrWhiteSpace(format) || format == "G")
format = "F";

switch (format)
{
case "F": // full
return Title + "; " + Authors + "; " + ISBN + "; " + String.Format("{0:C}", Price);
case "T": // title only
return Title;
case "TA": // title and authors
return Title + ", " + Authors;
case "I": // isbn only
return ISBN;
default:
throw new FormatException(String.Format("The '{0}' format is not supported.", format));
}
}

public string Title { get; set; }
public string Authors { get; set; }
public decimal Price { get; set; }
public float Rating { get; set; }
public string ISBN { get; set; }
}

// Testing...
Book book = new Book();
book.Title = "Programming Windows";
book.Authors = "Charles Petzold";
book.Price = 44.30M;
book.Rating = 4.6f;
book.ISBN = "978-0735671768";

### XML Documentation

Tags recognized by the C# compiler:

<c>          - inline code, e.g. <c>string str = "test";</c>
<code>       - code block
<example>    - code example
<exception>  - an exception class
<include>    - merges an external XML file that contains documentation
- in the <include> element the path attribute denotes an XPath query to a specific element
in an external XML file that contains documentation
<list>       - a list
<para>       - a text paragraph
<param>      - a method parameter, e.g. <param name="title">description of title param</param>
<paramref>   - the name of the parameter to refer to, e.g. <paramref name="title"/>
- indicates an IPermission type required by the type or member
<remarks>    - a remarks section
<returns>    - the return value of a method
<see>        - a cross-reference to another parameter, e.g. <see cref="member"/>...</see>
<summary>    - a summary section
<typeparam>, <typeparamref> - used with generics
<value>      - a description for the property 'value' parameter

You can define your own tags by simply using them.

When the C# compiler processes the XML comments, it generates <member> elements. Each member has a name with one of the following prefixes:

• N: - namespace
• T: - type (class, struct, enum, interface, delegate)
• F: - field
• M: - method
• P: - property (including indexers)
• E: - event
• !: - error

The <list> element emits a bulleted, numbered, or table-style list:

<list type=[ bullet | number | table ]>
</list>