Posts Tagged ‘modifier’

C# modifiers for classes (C), methods(M), properties(P), indexers(I), events(E), field(F) and operator(O)

March 22nd, 2009 Comments off

There are quite a few the C# modifiers, depends on the modifiers, they can be used on classes (C), methods(M), properties(P), indexers(I), events(E), field(F) and operator(O) here are the summary (which may be imcompleted). abstract Usage: C M P I E const Usage: P Note: Only can be initialize in declearation field. All const are static! readonly Usage: F NOTE: Assignments to the fields introduced by the declaration can only occur as part of the declaration or in a constructor in the same class. You can only pass the readonly var into a method with ref or out modifier. event Usage: M (delegate) Note: An event must modify a deligate, it acts as a subscription point for other methods with the same signiture of the deligate it modifies. You need to declear a delegate, then associate the event with it. // event_keyword.cs using System; public delegate void MyDelegate(); // delegate declaration extern Usage: M Note: Use the extern modifier in a method declaration to indicate that the method is implemented externally. A common use of the extern modifier is with the DllImport attribute. (Similar to C/C++ external link)

public interface I {  
event MyDelegate MyEvent;  
void FireAway(); 
public class MyClass: I {
  public event MyDelegate MyEvent;
  public void FireAway(){
   if (MyEvent != null)

public class MainClass {
  static private void f()  {
   Console.WriteLine{"This is called when the event fires.");  
  static public void Main ()  {
   I i = new MyClass();
   i.MyEvent += new MyDelegate(f);

override Usage: M P I E Note: The method overridden by an override declaration is known as the overridden base method. The overridden base method must have the same signature as the override method. You cannot override a non-virtual or static method. Both the override method and the virtual method must have the same access level modifier. new Usage: M P F Note: use it to hide the parent class’s member. sealed Usage: C M P Note:A sealed class cannot be inherited. It is an error to use a sealed class as a base class. Use the sealed modifier in a class declaration to prevent inheritance of the class. Structs are implicitly sealed; therefore, they cannot be inherited. static Usage: F M P O E constructor Note: A constant or type declaration is implicitly a static member. F/M/P/O/E: class level static, must access from class level Class.method or Class.field Constructor: Static constructor is a special method which gets called when the class assembly get loaded, and NO extra modifier for static constructor unsafe Usage: M Note: The unsafe keyword denotes an unsafe context, which is required for any operation involving pointers. virtual Usgae: M P Note:By default, methods are non-virtual. Virtual enables dynamic binding. volatile Usage: F Note: It indicates modified Object can be chaanged unexpectedly. The type of a field marked as volatile is restricted to the following types: Any reference type. Any pointer type (in an unsafe context). The types sbyte, byte, short, ushort, int, uint, char, float, bool. An enum type with an enum base type of byte, sbyte, short, ushort, int, or uint. unchecked Usage: Block Note: The unchecked keyword is used to suppress overflow-checking for integral-type arithmetic operations and conversions.

unchecked {
  int val = 2147483647 * 2; 
public int UncheckedAdd(int a, int b) {
  return unchecked(a + b); 
Categories: C#, Tech Tags: , , ,