Quick Reference: C# Keywords

A few C# keywords including access modifiers, etc.

I compiled this list recently and it seems like the sort of thing that should be enshrined in blog. I’ll add to this as others come up. Feel free to leave a comment if there’s any glaring omissions.

Access Modifiers

Public – The property/method is visible within the containing class and to all classes in the same project, as well as any other projects that reference the containing project.

Internal – The property/method is visible within the containing class and to all classes in the same project. It is not visible to any other projects.

Protected – The property is only visible within the containing class AND to classes that inherit from the containing class (regardless of whether they are in the same project or a different referencing project).

Private – Visible within the containing class only.

It is also possible to mark a property/method protected internal. No prizes for guessing what that does…

Other important keywords

Overrides – Use this in a derived class to override a method/property on the base class (the one you’re inheriting from)

Abstract – An abstract class cannot be used by itself. You have to inherit/derive from it and use the derived class. Within your abstract class, you can also define abstract properties/methods, which have no implementation. This forces the derived class to implement them.

Virtual – A virtual method/property has some default implementation, but a derived class can optionally override its behaviour to some alternative implementation.

Sealed – Marking a class sealed prevents it being derived from. You may NOT inherit from a sealed class.

Static – In a conventional class, you would instantiate a new instance of it, use it and then dispose of it. Where a class is marked static you cannot instantiate it. Instead there is one instance shared by the entire application. It therefore can be useful for utility functions (such as null checking) or for global variables (for example, the inbuilt ConfigurationManager class is static providing access to the configuration file to the entire application). The one shared instance is instantiated when it is first called, and you can create a private constructor on a static class which will be called at that point. It is okay to have static members on a non-static class, but you may not have non-static members in a static class… and if you think about it, it wouldn’t make sense that way round.

Caution: There is a temptation sometimes to use a static class as a big bucket of variables that you’re not really sure what to do with. This is almost always a bad idea. It is impossible to tell what set those variables (and when) which can cause unpredictable behaviour, particularly in a multi-threaded scenario. They are are also very hard to unit test. For more info on static classes, check out the singleton design pattern.

For a class with none of those keywords, the default behaviour allows you to use the class as it is (ie: you don’t need to derive from it to use it), but you can derive from it if you wish.

extern – This is used when communicating with members in unmanaged dlls, typically combined with the DllImport attribute.

Attributes

Attributes are markers that can be applied to classes and/or class members depending on the attribute. One such example might be the serializable attribute, which allows us to pick specifically which classess and properties will be serialized (.Net has built in serializers for binary and xml).:

[Serializable]
public string Name { get; set; }

There’s a number of attributes built into the framework for various purposes and you can also define your own attributes. You can identify properties with attributes using reflection.

Builtin attributes:

ThreadStatic – Limits the scope of a static member to the one instance per thread (as opposed to one global instance)

DllImport – Indicates that the function that follows exists a different dll. Takes the name of a dll registered with the OS as an argument. Used together with the extern keyword.

Continue Reading