Home > Articles

  • Print
  • + Share This

Joe's Tricks

  1. Response files, command line option @<respfilename>.rsp, can be used to hold assembly references, freeing you from typing them on the command line. The C# compiler automatically calls the response file at

  2. %windir%\Microsoft.NET\Framework\v<version#>\csc.rsp. 

    This file contains all of the .NET assemblies, leaving only third-party and project assemblies to be added to a local response file.

  3. Using the as operator is more efficient than the is operator when obtaining a reference to an object and checking it’s type at the same time. More specifically, the following operation using the is operator

  4. 
    		if (mc1 is MyClass)
    
    		{
    
    			mc2 = mc1;
    
    		}
    
    		else
    
    		{
    
    			mc2 = null;
    
    		}
    
    
    
               }

    takes two operations: one to check if mc1 is of type MyClass and another to assign mc1 to mc2. The next line shows how the as operator performs this in one statement, leaving a null result if mc3 is not of type MyClass:

     MyClass mc3 = mc1 as MyClass;
  5. Implement the using statement with the Dispose pattern for deterministic resource destruction. This allows you to free system resources when you’re done with them rather than waiting for the garbage collector, which may never finalize the object holding the resources.

  6. When implementing a method that accepts a variable number of parameters, use the params parameter type. It accepts a comma-separated list of arguments, a single dimension array, or a jagged array.

  7. For late bound delegates, instantiate the delegate in the get accessor of a static property. This ensures that the delegate is not created until it’s used, saving system resources. This technique is shown below:

  8. 
    	public delegate void MyDelegate();
    
    	public static   void MyMethod() {}
    
    
    
    	public static MyDelegate MyCallBack 
    
    	{ 
    
    		get 
    
    		{ 
    
    			return new MyDelegate(MyMethod);
    
    		}
    
    	}
  9. Explicitly declare base class methods as virtual if you intend for subclasses to override them. Otherwise, non-virtual base class methods will be hidden by derived methods of the same name. The virtual modifier guarantees the ability to implement polymorphic behavior.

  10. C# doesn’t have automatic fall through on switch statements without break statements between cases. To simulate fall through in a switch statement, similar to the behavior of C and C++ switch statements, use goto case n:, where n represents the next case statement.

  11. When the value of a variable isn’t known until runtime and you still want to implement constant behavior, use the readonly modifier. It can be initialized in a constructor, but can’t change thereafter.

  12. A property with only a get accessor

  13. 
    		public string MyReadProp 
    
    		{ 
    
    			get 
    
    			{ 
    
    				return "readonly"; 
    
    			} 
    
    		}

    is read-only, and a property with only a set accessor

    
    		public string MyWriteProp 
    
    		{ 
    
    			set 
    
    			{ 
    
    				myField = value;
    
    			} 
    
    		}
    
    

    is write-only.

  14. Conditions for if and loop statements must evaluate to a Boolean true or false value. To simulate a Boolean false with an integral expression use

  15. if ( ( int_expression ) == 0 ) { }

    and to simulate a Booleantrue condition use

    if ( ( int_expression ) == 0 ) { }
  • + Share This
  • 🔖 Save To Your Account

Related Resources

There are currently no related titles. Please check back later.