Create and use types

Create Types

It’s important to know the different types that are available in C#. A value type should be used for small, immutable objects that are used a lot. In all other situations, use a reference type.

Extension methods

Remember that the difference between a regular static method and an extension method is the special this keyword for the first argument.

Consume types

Explicit conversions

Make sure that you know the difference between an implicit and explicit conversion. An explicit conversion is called casting and always needs some special syntax.

Enforce encapsulation

Using properties

Always favor properties over fields for public members. An automatically implemented property looks like a field to the outside world, but you can always add extra behavior when necessary.

Create and implement a class hierarchy

Abstract classes

Make sure that you know the difference between an interface and an abstract class. An interface has no implementation code. An abstract class can choose to implement methods or leave it to the derived class.

Manage the object life cycle

IDisposable

It’s important to know the difference between implementing IDisposable and a finalizer. Both clean up your object, but a finalizer is called by the garbage collector, and the Dispose method can be called from code.

Manipulate strings

Manipulating strings

Because of the immutability of the string type, all string operations return a new string. Make sure that you use this value instead of the original string.