On Designing Good Libraries — Part III


style="FONT-WEIGHT: bold; FONT-SIZE: 10pt; FONT-FAMILY: Arial">Good discussion
on the first two…. Let’s see how this goes.


style="FONT-WEIGHT: bold; FONT-SIZE: 10pt; FONT-FAMILY: Arial"> 


style="FONT-WEIGHT: bold; FONT-SIZE: 10pt; FONT-FAMILY: Arial"> 


style="FONT-WEIGHT: bold; FONT-SIZE: 10pt; FONT-FAMILY: Arial"> 


style="FONT-WEIGHT: bold; FONT-SIZE: 10pt; FONT-FAMILY: Arial">Fields face=Arial size=2> style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"> "urn:schemas-microsoft-com:office:office" />


style="MARGIN: 0in 0in 0pt 0.5in; TEXT-INDENT: -0.25in; mso-list: l10 level1 lfo1; tab-stops: list .5in"> face=Tahoma size=2> style="FONT-SIZE: 10pt; FONT-FAMILY: Tahoma; mso-fareast-font-family: Tahoma"> style="mso-list: Ignore">• style="FONT: 7pt 'Times New Roman'">        
style="FONT-SIZE: 10pt; FONT-FAMILY: Arial">Never use publicly exposed
instance fields


style="MARGIN: 0in 0in 0pt 1in; TEXT-INDENT: -0.25in; mso-list: l10 level2 lfo1; tab-stops: list 1.0in"> face=Tahoma size=2> style="FONT-SIZE: 10pt; FONT-FAMILY: Tahoma; mso-fareast-font-family: Tahoma"> style="mso-list: Ignore">• style="FONT: 7pt 'Times New Roman'">        
style="FONT-SIZE: 10pt; FONT-FAMILY: Arial">Properties offer more flexibility at
minimal cost


style="MARGIN: 0in 0in 0pt 1.5in; TEXT-INDENT: -0.25in; mso-list: l10 level3 lfo1; tab-stops: list 1.5in"> face=Tahoma size=2> style="FONT-SIZE: 10pt; FONT-FAMILY: Tahoma; mso-fareast-font-family: Tahoma"> style="mso-list: Ignore">• style="FONT: 7pt 'Times New Roman'">        
"urn:schemas-microsoft-com:office:smarttags" /> face=Arial size=2> style="FONT-SIZE: 10pt; FONT-FAMILY: Arial">JIT face=Arial size=2> inlines
simple property access


style="MARGIN: 0in 0in 0pt 1.5in; TEXT-INDENT: -0.25in; mso-list: l10 level3 lfo1; tab-stops: list 1.5in"> face=Tahoma size=2> style="FONT-SIZE: 10pt; FONT-FAMILY: Tahoma; mso-fareast-font-family: Tahoma"> style="mso-list: Ignore">• style="FONT: 7pt 'Times New Roman'">        
style="FONT-SIZE: 10pt; FONT-FAMILY: Arial">Easy to add cache or delay creation
in the future


style="MARGIN: 0in 0in 0pt 0.5in; TEXT-INDENT: -0.25in; mso-list: l10 level1 lfo1; tab-stops: list .5in"> face=Tahoma size=2> style="FONT-SIZE: 10pt; FONT-FAMILY: Tahoma; mso-fareast-font-family: Tahoma"> style="mso-list: Ignore">• style="FONT: 7pt 'Times New Roman'">        
style="FONT-SIZE: 10pt; FONT-FAMILY: Arial">For static fields, do not include a
prefix on a public field name


style="MARGIN: 0in 0in 0pt 1in; TEXT-INDENT: -0.25in; mso-list: l10 level2 lfo1; tab-stops: list 1.0in"> face=Tahoma size=2> style="FONT-SIZE: 10pt; FONT-FAMILY: Tahoma; mso-fareast-font-family: Tahoma"> style="mso-list: Ignore">• style="FONT: 7pt 'Times New Roman'">        
style="FONT-SIZE: 10pt; FONT-FAMILY: Arial">Example: ‘g_’ or ‘s_’ to distinguish
static vs. non-static fields


style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"> 


size=2> style="FONT-SIZE: 10pt; FONT-FAMILY: Arial">const


style="MARGIN: 0in 0in 0pt 0.75in; TEXT-INDENT: -0.25in; mso-list: l14 level1 lfo2; tab-stops: list .75in"> face=Tahoma size=2> style="FONT-SIZE: 10pt; FONT-FAMILY: Tahoma; mso-fareast-font-family: Tahoma"> style="mso-list: Ignore">• style="FONT: 7pt 'Times New Roman'">        
style="FONT-SIZE: 10pt; FONT-FAMILY: Arial">Compile-time
evaluation


style="MARGIN: 0in 0in 0pt 0.75in; TEXT-INDENT: -0.25in; mso-list: l14 level1 lfo2; tab-stops: list .75in"> face=Tahoma size=2> style="FONT-SIZE: 10pt; FONT-FAMILY: Tahoma; mso-fareast-font-family: Tahoma"> style="mso-list: Ignore">• style="FONT: 7pt 'Times New Roman'">        
style="FONT-SIZE: 10pt; FONT-FAMILY: Arial">Stable across
versions


style="MARGIN: 0in 0in 0pt 0.75in; TEXT-INDENT: -0.25in; mso-list: l14 level1 lfo2; tab-stops: list .75in"> face=Tahoma size=2> style="FONT-SIZE: 10pt; FONT-FAMILY: Tahoma; mso-fareast-font-family: Tahoma"> style="mso-list: Ignore">• style="FONT: 7pt 'Times New Roman'">        
style="FONT-SIZE: 10pt; FONT-FAMILY: Arial">Always
static


size=2> style="FONT-SIZE: 10pt; FONT-FAMILY: Arial">readonly


style="MARGIN: 0in 0in 0pt 0.75in; TEXT-INDENT: -0.25in; mso-list: l4 level1 lfo3; tab-stops: list .75in"> face=Tahoma size=2> style="FONT-SIZE: 10pt; FONT-FAMILY: Tahoma; mso-fareast-font-family: Tahoma"> style="mso-list: Ignore">• style="FONT: 7pt 'Times New Roman'">        
style="FONT-SIZE: 10pt; FONT-FAMILY: Arial">Run-time
evaluation


style="MARGIN: 0in 0in 0pt 0.75in; TEXT-INDENT: -0.25in; mso-list: l4 level1 lfo3; tab-stops: list .75in"> face=Tahoma size=2> style="FONT-SIZE: 10pt; FONT-FAMILY: Tahoma; mso-fareast-font-family: Tahoma"> style="mso-list: Ignore">• style="FONT: 7pt 'Times New Roman'">        
style="FONT-SIZE: 10pt; FONT-FAMILY: Arial">Unstable across
versions


style="MARGIN: 0in 0in 0pt 0.75in; TEXT-INDENT: -0.25in; mso-list: l4 level1 lfo3; tab-stops: list .75in"> face=Tahoma size=2> style="FONT-SIZE: 10pt; FONT-FAMILY: Tahoma; mso-fareast-font-family: Tahoma"> style="mso-list: Ignore">• style="FONT: 7pt 'Times New Roman'">        
style="FONT-SIZE: 10pt; FONT-FAMILY: Arial">Static or
instance


style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"> 


style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"> 


size=2> style="FONT-SIZE: 10pt; mso-bidi-font-family: 'Courier New'">class Math
{


size=2> style="mso-spacerun: yes">  public const double Pi =
3.14;


size=2> style="FONT-SIZE: 10pt; mso-bidi-font-family: 'Courier New'">}


size=2> style="FONT-SIZE: 10pt; mso-bidi-font-family: 'Courier New'"> 


size=2> style="FONT-SIZE: 10pt; mso-bidi-font-family: 'Courier New'">class Color
{


size=2> style="mso-spacerun: yes">  public static readonly Color Red style="mso-spacerun: yes">   = new
Color(…);


size=2> style="mso-spacerun: yes">  public static readonly Color Blue style="mso-spacerun: yes">  = new
Color(…);


size=2> style="mso-spacerun: yes">  public static readonly Color Green = new
Color(…);


size=2> style="FONT-SIZE: 10pt; mso-bidi-font-family: 'Courier New'">}


style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"> 


style="FONT-WEIGHT: bold; FONT-SIZE: 10pt; FONT-FAMILY: Arial">Properties face=Arial size=2> style="FONT-SIZE: 10pt; FONT-FAMILY: Arial">


style="MARGIN: 0in 0in 0pt 0.5in; TEXT-INDENT: -0.25in; mso-list: l7 level1 lfo4; tab-stops: list .5in"> face=Tahoma size=2> style="FONT-SIZE: 10pt; FONT-FAMILY: Tahoma; mso-fareast-font-family: Tahoma"> style="mso-list: Ignore">• style="FONT: 7pt 'Times New Roman'">        
style="FONT-SIZE: 10pt; FONT-FAMILY: Arial">Smart
fields


style="MARGIN: 0in 0in 0pt 1in; TEXT-INDENT: -0.25in; mso-list: l7 level2 lfo4; tab-stops: list 1.0in"> face=Tahoma size=2> style="FONT-SIZE: 10pt; FONT-FAMILY: Tahoma; mso-fareast-font-family: Tahoma"> style="mso-list: Ignore">• style="FONT: 7pt 'Times New Roman'">        
style="FONT-SIZE: 10pt; FONT-FAMILY: Arial">Calling syntax like
fields


style="MARGIN: 0in 0in 0pt 1in; TEXT-INDENT: -0.25in; mso-list: l7 level2 lfo4; tab-stops: list 1.0in"> face=Tahoma size=2> style="FONT-SIZE: 10pt; FONT-FAMILY: Tahoma; mso-fareast-font-family: Tahoma"> style="mso-list: Ignore">• style="FONT: 7pt 'Times New Roman'">        
style="FONT-SIZE: 10pt; FONT-FAMILY: Arial">Flexibility of
methods


style="MARGIN: 0in 0in 0pt 0.5in; TEXT-INDENT: -0.25in; mso-list: l7 level1 lfo4; tab-stops: list .5in"> face=Tahoma size=2> style="FONT-SIZE: 10pt; FONT-FAMILY: Tahoma; mso-fareast-font-family: Tahoma"> style="mso-list: Ignore">• style="FONT: 7pt 'Times New Roman'">        
style="FONT-SIZE: 10pt; FONT-FAMILY: Arial">Use read only properties where
appropriate


style="MARGIN: 0in 0in 0pt 0.5in; TEXT-INDENT: -0.25in; mso-list: l7 level1 lfo4; tab-stops: list .5in"> face=Tahoma size=2> style="FONT-SIZE: 10pt; FONT-FAMILY: Tahoma; mso-fareast-font-family: Tahoma"> style="mso-list: Ignore">• style="FONT: 7pt 'Times New Roman'">        
style="FONT-SIZE: 10pt; FONT-FAMILY: Arial">Do not use write-only
properties


style="MARGIN: 0in 0in 0pt 0.5in; TEXT-INDENT: -0.25in; mso-list: l7 level1 lfo4; tab-stops: list .5in"> face=Tahoma size=2> style="FONT-SIZE: 10pt; FONT-FAMILY: Tahoma; mso-fareast-font-family: Tahoma"> style="mso-list: Ignore">• style="FONT: 7pt 'Times New Roman'">        
style="FONT-SIZE: 10pt; FONT-FAMILY: Arial">Consider raising PropertyChanged
events


style="MARGIN: 0in 0in 0pt 0.5in; TEXT-INDENT: -0.25in; mso-list: l7 level1 lfo4; tab-stops: list .5in"> face=Tahoma size=2> style="FONT-SIZE: 10pt; FONT-FAMILY: Tahoma; mso-fareast-font-family: Tahoma"> style="mso-list: Ignore">• style="FONT: 7pt 'Times New Roman'">        
style="FONT-SIZE: 10pt; FONT-FAMILY: Arial">Property getters should be simple
and therefore unlikely to throw exceptions


style="MARGIN: 0in 0in 0pt 0.5in; TEXT-INDENT: -0.25in; mso-list: l7 level1 lfo4; tab-stops: list .5in"> face=Tahoma size=2> style="FONT-SIZE: 10pt; FONT-FAMILY: Tahoma; mso-fareast-font-family: Tahoma"> style="mso-list: Ignore">• style="FONT: 7pt 'Times New Roman'">        
style="FONT-SIZE: 10pt; FONT-FAMILY: Arial">Properties should not have
dependencies on each other


style="MARGIN: 0in 0in 0pt 1in; TEXT-INDENT: -0.25in; mso-list: l7 level2 lfo4; tab-stops: list 1.0in"> face=Tahoma size=2> style="FONT-SIZE: 10pt; FONT-FAMILY: Tahoma; mso-fareast-font-family: Tahoma"> style="mso-list: Ignore">• style="FONT: 7pt 'Times New Roman'">        
style="FONT-SIZE: 10pt; FONT-FAMILY: Arial">Setting one property should not
affect other properties


style="MARGIN: 0in 0in 0pt 0.5in; TEXT-INDENT: -0.25in; mso-list: l7 level1 lfo4; tab-stops: list .5in"> face=Tahoma size=2> style="FONT-SIZE: 10pt; FONT-FAMILY: Tahoma; mso-fareast-font-family: Tahoma"> style="mso-list: Ignore">• style="FONT: 7pt 'Times New Roman'">        
style="FONT-SIZE: 10pt; FONT-FAMILY: Arial">Properties should be settable in any
order


style="MARGIN: 0in 0in 0pt 0.5in; TEXT-INDENT: -0.25in; mso-list: l7 level1 lfo4; tab-stops: list .5in"> face=Tahoma size=2> style="FONT-SIZE: 10pt; FONT-FAMILY: Tahoma; mso-fareast-font-family: Tahoma"> style="mso-list: Ignore">• style="FONT: 7pt 'Times New Roman'">        
style="FONT-SIZE: 10pt; FONT-FAMILY: Arial">Common to have read-only public
access and protected write access


size=2> style="FONT-SIZE: 10pt; mso-bidi-font-family: 'Courier New'">public class
MyClass {    

   private string
name;
   public string Name
{
     get
{
       
return name;
    
}
   }
style="mso-spacerun: yes">   protected void SetName (string
name)
   {
style="mso-spacerun: yes">       this.name
= name;
  
}
}


style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"> 


style="FONT-WEIGHT: bold; FONT-SIZE: 10pt; FONT-FAMILY: Arial">Properties versus
Methods
style="FONT-SIZE: 10pt; FONT-FAMILY: Arial">


style="MARGIN: 0in 0in 0pt 0.5in; TEXT-INDENT: -0.25in; mso-list: l12 level1 lfo5; tab-stops: list .5in"> face=Tahoma size=2> style="FONT-SIZE: 10pt; FONT-FAMILY: Tahoma; mso-fareast-font-family: Tahoma"> style="mso-list: Ignore">• style="FONT: 7pt 'Times New Roman'">        
style="FONT-SIZE: 10pt; FONT-FAMILY: Arial">Do use a
property:


style="MARGIN: 0in 0in 0pt 1in; TEXT-INDENT: -0.25in; mso-list: l12 level2 lfo5; tab-stops: list 1.0in"> face=Tahoma size=2> style="FONT-SIZE: 10pt; FONT-FAMILY: Tahoma; mso-fareast-font-family: Tahoma"> style="mso-list: Ignore">• style="FONT: 7pt 'Times New Roman'">        
style="FONT-SIZE: 10pt; FONT-FAMILY: Arial">If the member has a logical data
member


size=2> style="FONT-SIZE: 10pt; mso-bidi-font-family: 'Courier New'">string Name
{get;} //Good


size=2> style="FONT-SIZE: 10pt; mso-bidi-font-family: 'Courier New'">string
GetName() //Bad


face="Courier New"> "urn:schemas:contacts" /> style="FONT-SIZE: 10pt; mso-bidi-font-family: 'Courier New'">Guid size=2> GetNext size=2>(){} //Good


style="FONT-SIZE: 10pt; mso-bidi-font-family: 'Courier New'">Guid size=2> Next {get;} //Bad


style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"> 


style="MARGIN: 0in 0in 0pt 0.5in; TEXT-INDENT: -0.25in; mso-list: l18 level1 lfo6; tab-stops: list .5in"> face=Tahoma size=2> style="FONT-SIZE: 10pt; FONT-FAMILY: Tahoma; mso-fareast-font-family: Tahoma"> style="mso-list: Ignore">• style="FONT: 7pt 'Times New Roman'">        
style="FONT-SIZE: 10pt; FONT-FAMILY: Arial">Do use a method:


style="MARGIN: 0in 0in 0pt 1in; TEXT-INDENT: -0.25in; mso-list: l18 level2 lfo6; tab-stops: list 1.0in"> face=Tahoma size=2> style="FONT-SIZE: 10pt; FONT-FAMILY: Tahoma; mso-fareast-font-family: Tahoma"> style="mso-list: Ignore">• style="FONT: 7pt 'Times New Roman'">        
style="FONT-SIZE: 10pt; FONT-FAMILY: Arial">If the operation is a conversion,
such as ToString()


style="MARGIN: 0in 0in 0pt 1in; TEXT-INDENT: -0.25in; mso-list: l18 level2 lfo6; tab-stops: list 1.0in"> face=Tahoma size=2> style="FONT-SIZE: 10pt; FONT-FAMILY: Tahoma; mso-fareast-font-family: Tahoma"> style="mso-list: Ignore">• style="FONT: 7pt 'Times New Roman'">        
style="FONT-SIZE: 10pt; FONT-FAMILY: Arial">If the getter has an observable side
effect


style="MARGIN: 0in 0in 0pt 1in; TEXT-INDENT: -0.25in; mso-list: l18 level2 lfo6; tab-stops: list 1.0in"> face=Tahoma size=2> style="FONT-SIZE: 10pt; FONT-FAMILY: Tahoma; mso-fareast-font-family: Tahoma"> style="mso-list: Ignore">• style="FONT: 7pt 'Times New Roman'">        
style="FONT-SIZE: 10pt; FONT-FAMILY: Arial">If order of execution is
important


style="MARGIN: 0in 0in 0pt 1in; TEXT-INDENT: -0.25in; mso-list: l18 level2 lfo6; tab-stops: list 1.0in"> face=Tahoma size=2> style="FONT-SIZE: 10pt; FONT-FAMILY: Tahoma; mso-fareast-font-family: Tahoma"> style="mso-list: Ignore">• style="FONT: 7pt 'Times New Roman'">        
style="FONT-SIZE: 10pt; FONT-FAMILY: Arial">If the method might not return
immediately


style="MARGIN: 0in 0in 0pt 1in; TEXT-INDENT: -0.25in; mso-list: l9 level2 lfo7; tab-stops: list 1.0in"> face=Tahoma size=2> style="FONT-SIZE: 10pt; FONT-FAMILY: Tahoma; mso-fareast-font-family: Tahoma"> style="mso-list: Ignore">• style="FONT: 7pt 'Times New Roman'">        
style="FONT-SIZE: 10pt; FONT-FAMILY: Arial">If the member returns an
array


style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"> 


size=2> style="FONT-SIZE: 10pt; mso-bidi-font-family: 'Courier New'">EmployeeList
l = FillList();


size=2> style="FONT-SIZE: 10pt; mso-bidi-font-family: 'Courier New'">for (int i =
0; i < l.Length; i++) {


size=2> style="mso-spacerun: yes">   if (l.All[i] ==
x){…}


size=2> style="FONT-SIZE: 10pt; mso-bidi-font-family: 'Courier New'">}


size=2> style="FONT-SIZE: 10pt; mso-bidi-font-family: 'Courier New'">//calling
code:


size=2> style="FONT-SIZE: 10pt; mso-bidi-font-family: 'Courier New'">if
(l.GetAll()[i]== x) {…}


style="MARGIN: 0in 0in 0pt 0.5in; TEXT-INDENT: -0.25in; mso-list: l20 level1 lfo8; tab-stops: list .5in"> face=Tahoma size=2> style="FONT-SIZE: 10pt; FONT-FAMILY: Tahoma; mso-fareast-font-family: Tahoma"> style="mso-list: Ignore">• style="FONT: 7pt 'Times New Roman'">        
style="FONT-SIZE: 10pt; FONT-FAMILY: Arial">Creates a new snap shot of the array
each time through the loop


style="MARGIN: 0in 0in 0pt 0.5in; TEXT-INDENT: -0.25in; mso-list: l20 level1 lfo8; tab-stops: list .5in"> face=Tahoma size=2> style="FONT-SIZE: 10pt; FONT-FAMILY: Tahoma; mso-fareast-font-family: Tahoma"> style="mso-list: Ignore">• style="FONT: 7pt 'Times New Roman'">        
style="FONT-SIZE: 10pt; FONT-FAMILY: Arial">The GetAll() form makes this much
clearer


style="FONT-WEIGHT: bold; FONT-SIZE: 10pt; FONT-FAMILY: Arial"> 


style="FONT-WEIGHT: bold; FONT-SIZE: 10pt; FONT-FAMILY: Arial">Properties:
Indexers
style="FONT-SIZE: 10pt; FONT-FAMILY: Arial">


style="MARGIN: 0in 0in 0pt 0.5in; TEXT-INDENT: -0.25in; mso-list: l0 level1 lfo9; tab-stops: list .5in"> face=Tahoma size=2> style="FONT-SIZE: 10pt; FONT-FAMILY: Tahoma; mso-fareast-font-family: Tahoma"> style="mso-list: Ignore">• style="FONT: 7pt 'Times New Roman'">        
style="FONT-SIZE: 10pt; FONT-FAMILY: Arial">Use if the logical backing store is
an array


style="MARGIN: 0in 0in 0pt 0.5in; TEXT-INDENT: -0.25in; mso-list: l0 level1 lfo9; tab-stops: list .5in"> face=Tahoma size=2> style="FONT-SIZE: 10pt; FONT-FAMILY: Tahoma; mso-fareast-font-family: Tahoma"> style="mso-list: Ignore">• style="FONT: 7pt 'Times New Roman'">        
style="FONT-SIZE: 10pt; FONT-FAMILY: Arial">Almost always int or string
indexed


style="MARGIN: 0in 0in 0pt 0.5in; TEXT-INDENT: -0.25in; mso-list: l0 level1 lfo9; tab-stops: list .5in"> face=Tahoma size=2> style="FONT-SIZE: 10pt; FONT-FAMILY: Tahoma; mso-fareast-font-family: Tahoma"> style="mso-list: Ignore">• style="FONT: 7pt 'Times New Roman'">        
style="FONT-SIZE: 10pt; FONT-FAMILY: Arial">Rare to have multiple
indices


size=2> style="FONT-SIZE: 10pt; mso-bidi-font-family: 'Courier New'">public char
this[int index] {get;}


size=2>String
s = “foo”;


size=2> style="FONT-SIZE: 10pt; mso-bidi-font-family: 'Courier New'">Console.WriteLine
(s[i]); // calls indexer


style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"> 


style="FONT-WEIGHT: bold; FONT-SIZE: 10pt; FONT-FAMILY: Arial">Events face=Arial size=2> style="FONT-SIZE: 10pt; FONT-FAMILY: Arial">


style="MARGIN: 0in 0in 0pt 0.5in; TEXT-INDENT: -0.25in; mso-list: l17 level1 lfo10; tab-stops: list .5in"> face=Tahoma size=2> style="FONT-SIZE: 10pt; FONT-FAMILY: Tahoma; mso-fareast-font-family: Tahoma"> style="mso-list: Ignore">• style="FONT: 7pt 'Times New Roman'">        
style="FONT-SIZE: 10pt; FONT-FAMILY: Arial">Defining an
event


size=2> style="FONT-SIZE: 10pt; mso-bidi-font-family: 'Courier New'">public
delegate void EventHandler(object sender,


size=2> style="mso-spacerun: yes">    EventArgs
e);


size=2> style="FONT-SIZE: 10pt; mso-bidi-font-family: 'Courier New'"> 


size=2> style="FONT-SIZE: 10pt; mso-bidi-font-family: 'Courier New'">public class
Button: Control {
   public
event EventHandler Click;


size=2> style="FONT-SIZE: 10pt; mso-bidi-font-family: 'Courier New'"> 


size=2> style="mso-spacerun: yes">   protected void OnClick(EventArgs
e) {
      if
(Click != null)


size=2> style="mso-spacerun: yes">        
Click(this, e);
  
}


size=2> style="FONT-SIZE: 10pt; mso-bidi-font-family: 'Courier New'">}


style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"> 


style="MARGIN: 0in 0in 0pt 0.5in; TEXT-INDENT: -0.25in; mso-list: l3 level1 lfo11; tab-stops: list .5in"> face=Tahoma size=2> style="FONT-SIZE: 10pt; FONT-FAMILY: Tahoma; mso-fareast-font-family: Tahoma"> style="mso-list: Ignore">• style="FONT: 7pt 'Times New Roman'">        
style="FONT-SIZE: 10pt; FONT-FAMILY: Arial">Using an
Event


size=2> style="FONT-SIZE: 10pt; mso-bidi-font-family: 'Courier New'">void
Initialize() {


size=2> style="mso-spacerun: yes">   Button b = new
Button(…);


size=2> style="mso-spacerun: yes">   b.Click += new
EventHandler(ButtonClick);


size=2> style="FONT-SIZE: 10pt; mso-bidi-font-family: 'Courier New'">}


size=2> style="FONT-SIZE: 10pt; mso-bidi-font-family: 'Courier New'"> 


size=2> style="FONT-SIZE: 10pt; mso-bidi-font-family: 'Courier New'">void
ButtonClick(object sender, EventArgs e)


size=2> style="FONT-SIZE: 10pt; mso-bidi-font-family: 'Courier New'">{


size=2> style="mso-spacerun: yes">   MessageBox.Show(“You pressed the
button”);


size=2> style="FONT-SIZE: 10pt; mso-bidi-font-family: 'Courier New'">}


style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"> 


style="MARGIN: 0in 0in 0pt 0.5in; TEXT-INDENT: -0.25in; mso-list: l2 level1 lfo12; tab-stops: list .5in"> face=Tahoma size=2> style="FONT-SIZE: 10pt; FONT-FAMILY: Tahoma; mso-fareast-font-family: Tahoma"> style="mso-list: Ignore">• style="FONT: 7pt 'Times New Roman'">        
style="FONT-SIZE: 10pt; FONT-FAMILY: Arial">Events are style="FONT-WEIGHT: bold">raised, not triggered or
fired


style="MARGIN: 0in 0in 0pt 0.5in; TEXT-INDENT: -0.25in; mso-list: l2 level1 lfo12; tab-stops: list .5in"> face=Tahoma size=2> style="FONT-SIZE: 10pt; FONT-FAMILY: Tahoma; mso-fareast-font-family: Tahoma"> style="mso-list: Ignore">• style="FONT: 7pt 'Times New Roman'">        
style="FONT-SIZE: 10pt; FONT-FAMILY: Arial">Name events with a
verb


style="MARGIN: 0in 0in 0pt 1in; TEXT-INDENT: -0.25in; mso-list: l2 level2 lfo12; tab-stops: list 1.0in"> face=Tahoma size=2> style="FONT-SIZE: 10pt; FONT-FAMILY: Tahoma; mso-fareast-font-family: Tahoma"> style="mso-list: Ignore">• style="FONT: 7pt 'Times New Roman'">        
style="FONT-SIZE: 10pt; FONT-STYLE: italic; FONT-FAMILY: Arial">E.g. face=Arial size=2>: Click,
Paint, DrawItem, DropDown,


style="MARGIN: 0in 0in 0pt 0.5in; TEXT-INDENT: -0.25in; mso-list: l2 level1 lfo12; tab-stops: list .5in"> face=Tahoma size=2> style="FONT-SIZE: 10pt; FONT-FAMILY: Tahoma; mso-fareast-font-family: Tahoma"> style="mso-list: Ignore">• style="FONT: 7pt 'Times New Roman'">        
style="FONT-SIZE: 10pt; FONT-FAMILY: Arial">Event handlers have void return
type
style="mso-special-character: line-break">


size=2> style="FONT-SIZE: 10pt; mso-bidi-font-family: 'Courier New'">public
delegate void MouseEventHandler (
style="mso-spacerun: yes">                           
object sender,
style="mso-spacerun: yes">                           
MouseEventArgs e);


style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"> 


style="MARGIN: 0in 0in 0pt 0.5in; TEXT-INDENT: -0.25in; mso-list: l5 level1 lfo13; tab-stops: list .5in"> face=Tahoma size=2> style="FONT-SIZE: 10pt; FONT-FAMILY: Tahoma; mso-fareast-font-family: Tahoma"> style="mso-list: Ignore">• style="FONT: 7pt 'Times New Roman'">        
style="FONT-SIZE: 10pt; FONT-FAMILY: Arial">Event handler delegates use a
signature that follows the event design pattern


style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"> 


style="MARGIN: 0in 0in 0pt 0.5in; TEXT-INDENT: -0.25in; mso-list: l11 level1 lfo14; tab-stops: list .5in"> face=Tahoma size=2> style="FONT-SIZE: 10pt; FONT-FAMILY: Tahoma; mso-fareast-font-family: Tahoma"> style="mso-list: Ignore">• style="FONT: 7pt 'Times New Roman'">        
style="FONT-SIZE: 10pt; FONT-FAMILY: Arial">Use strongly typed event data where
appropriate


size=2> style="FONT-SIZE: 10pt; mso-bidi-font-family: 'Courier New'">public class
MouseEventArgs :
style="mso-spacerun: yes">                    
EventArgs { }


style="MARGIN: 0in 0in 0pt 0.5in; TEXT-INDENT: -0.25in; mso-list: l15 level1 lfo15; tab-stops: list .5in"> face=Tahoma size=2> style="FONT-SIZE: 10pt; FONT-FAMILY: Tahoma; mso-fareast-font-family: Tahoma"> style="mso-list: Ignore">• style="FONT: 7pt 'Times New Roman'">        
style="FONT-SIZE: 10pt; FONT-FAMILY: Arial">Able to add new members without a
breaking change


style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"> 


style="MARGIN: 0in 0in 0pt 0.5in; TEXT-INDENT: -0.25in; mso-list: l16 level1 lfo16; tab-stops: list .5in"> face=Tahoma size=2> style="FONT-SIZE: 10pt; FONT-FAMILY: Tahoma; mso-fareast-font-family: Tahoma"> style="mso-list: Ignore">• style="FONT: 7pt 'Times New Roman'">        
style="FONT-SIZE: 10pt; FONT-FAMILY: Arial">Provide a protected method to raise
the event


style="MARGIN: 0in 0in 0pt 1in; TEXT-INDENT: -0.25in; mso-list: l16 level2 lfo16; tab-stops: list 1.0in"> face=Tahoma size=2> style="FONT-SIZE: 10pt; FONT-FAMILY: Tahoma; mso-fareast-font-family: Tahoma"> style="mso-list: Ignore">• style="FONT: 7pt 'Times New Roman'">        
style="FONT-SIZE: 10pt; FONT-FAMILY: Arial">Named On style="FONT-STYLE: italic">EventName


style="MARGIN: 0in 0in 0pt 0.5in; TEXT-INDENT: -0.25in; mso-list: l16 level1 lfo16; tab-stops: list .5in"> face=Tahoma size=2> style="FONT-SIZE: 10pt; FONT-FAMILY: Tahoma; mso-fareast-font-family: Tahoma"> style="mso-list: Ignore">• style="FONT: 7pt 'Times New Roman'">        
style="FONT-SIZE: 10pt; FONT-FAMILY: Arial">Make it virtual if extensibility is
needed


size=2> style="FONT-SIZE: 10pt; mso-bidi-font-family: 'Courier New'">public class
Button {
   private
ButtonClickHandler onClickHandler; style="mso-spacerun: yes">      
style="mso-spacerun: yes">   protected void OnClick
(ClickEventArgs e) {
style="mso-spacerun: yes">      if
(onClickHandler != null) {
style="mso-tab-count: 1">      style="mso-spacerun: yes">    // call the delegate if
non-null
style="mso-spacerun: yes">        
onClickHandler(this, e);
style="mso-spacerun: yes">      }
style="mso-spacerun: yes">  
}
}


style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"> 


style="MARGIN: 0in 0in 0pt 0.5in; TEXT-INDENT: -0.25in; mso-list: l13 level1 lfo17; tab-stops: list .5in"> face=Tahoma size=2> style="FONT-SIZE: 10pt; FONT-FAMILY: Tahoma; mso-fareast-font-family: Tahoma"> style="mso-list: Ignore">• style="FONT: 7pt 'Times New Roman'">        
style="FONT-SIZE: 10pt; FONT-FAMILY: Arial">Events are callbacks into arbitrary
user code


style="MARGIN: 0in 0in 0pt 1in; TEXT-INDENT: -0.25in; mso-list: l13 level2 lfo17; tab-stops: list 1.0in"> face=Tahoma size=2> style="FONT-SIZE: 10pt; FONT-FAMILY: Tahoma; mso-fareast-font-family: Tahoma"> style="mso-list: Ignore">• style="FONT: 7pt 'Times New Roman'">        
style="FONT-SIZE: 10pt; FONT-FAMILY: Arial">Do not assume anything about the
state of your object


style="MARGIN: 0in 0in 0pt 1in; TEXT-INDENT: -0.25in; mso-list: l13 level2 lfo17; tab-stops: list 1.0in"> face=Tahoma size=2> style="FONT-SIZE: 10pt; FONT-FAMILY: Tahoma; mso-fareast-font-family: Tahoma"> style="mso-list: Ignore">• style="FONT: 7pt 'Times New Roman'">        
style="FONT-SIZE: 10pt; FONT-FAMILY: Arial">Code
defensively


size=2> style="FONT-SIZE: 10pt; mso-bidi-font-family: 'Courier New'">protected
void DoClick() {
  
PaintDown(); // paint button in depressed state
style="mso-spacerun: yes">   try {
style="mso-spacerun: yes">      OnClick(); //
call event handler
   }

   finally {
style="mso-spacerun: yes">      // window may be
deleted in event handler
style="mso-spacerun: yes">      if (windowHandle
!= null) {
style="mso-spacerun: yes">        
PaintUp(); // paint button in normal state
style="mso-spacerun: yes">      }
style="mso-spacerun: yes">   }
}


style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"> 


style="FONT-WEIGHT: bold; FONT-SIZE: 10pt; FONT-FAMILY: Arial">Static
Members
style="FONT-SIZE: 10pt; FONT-FAMILY: Arial">


style="MARGIN: 0in 0in 0pt 0.5in; TEXT-INDENT: -0.25in; mso-list: l8 level1 lfo18; tab-stops: list .5in"> face=Tahoma size=2> style="FONT-SIZE: 10pt; FONT-FAMILY: Tahoma; mso-fareast-font-family: Tahoma"> style="mso-list: Ignore">• style="FONT: 7pt 'Times New Roman'">        
style="FONT-SIZE: 10pt; FONT-FAMILY: Arial">Any kind of member can be
static


style="MARGIN: 0in 0in 0pt 0.5in; TEXT-INDENT: -0.25in; mso-list: l8 level1 lfo18; tab-stops: list .5in"> face=Tahoma size=2> style="FONT-SIZE: 10pt; FONT-FAMILY: Tahoma; mso-fareast-font-family: Tahoma"> style="mso-list: Ignore">• style="FONT: 7pt 'Times New Roman'">        
style="FONT-SIZE: 10pt; FONT-FAMILY: Arial">Static members


style="MARGIN: 0in 0in 0pt 1in; TEXT-INDENT: -0.25in; mso-list: l8 level2 lfo18; tab-stops: list 1.0in"> face=Tahoma size=2> style="FONT-SIZE: 10pt; FONT-FAMILY: Tahoma; mso-fareast-font-family: Tahoma"> style="mso-list: Ignore">• style="FONT: 7pt 'Times New Roman'">        
style="FONT-SIZE: 10pt; FONT-FAMILY: Arial">Cannot access instance
state


style="MARGIN: 0in 0in 0pt 1in; TEXT-INDENT: -0.25in; mso-list: l8 level2 lfo18; tab-stops: list 1.0in"> face=Tahoma size=2> style="FONT-SIZE: 10pt; FONT-FAMILY: Tahoma; mso-fareast-font-family: Tahoma"> style="mso-list: Ignore">• style="FONT: 7pt 'Times New Roman'">        
style="FONT-SIZE: 10pt; FONT-FAMILY: Arial">Cannot override or
specialize


style="MARGIN: 0in 0in 0pt 1in; TEXT-INDENT: -0.25in; mso-list: l8 level2 lfo18; tab-stops: list 1.0in"> face=Tahoma size=2> style="FONT-SIZE: 10pt; FONT-FAMILY: Tahoma; mso-fareast-font-family: Tahoma"> style="mso-list: Ignore">• style="FONT: 7pt 'Times New Roman'">        
style="FONT-SIZE: 10pt; FONT-FAMILY: Arial">Should be
thread-safe


style="MARGIN: 0in 0in 0pt 0.5in; TEXT-INDENT: -0.25in; mso-list: l8 level1 lfo18; tab-stops: list .5in"> face=Tahoma size=2> style="FONT-SIZE: 10pt; FONT-FAMILY: Tahoma; mso-fareast-font-family: Tahoma"> style="mso-list: Ignore">• style="FONT: 7pt 'Times New Roman'">        
style="FONT-SIZE: 10pt; FONT-FAMILY: Arial">Commonly used
for


style="MARGIN: 0in 0in 0pt 1in; TEXT-INDENT: -0.25in; mso-list: l8 level2 lfo18; tab-stops: list 1.0in"> face=Tahoma size=2> style="FONT-SIZE: 10pt; FONT-FAMILY: Tahoma; mso-fareast-font-family: Tahoma"> style="mso-list: Ignore">• style="FONT: 7pt 'Times New Roman'">        
style="FONT-SIZE: 10pt; FONT-FAMILY: Arial">Singleton
pattern


style="MARGIN: 0in 0in 0pt 1in; TEXT-INDENT: -0.25in; mso-list: l8 level2 lfo18; tab-stops: list 1.0in"> face=Tahoma size=2> style="FONT-SIZE: 10pt; FONT-FAMILY: Tahoma; mso-fareast-font-family: Tahoma"> style="mso-list: Ignore">• style="FONT: 7pt 'Times New Roman'">        
style="FONT-SIZE: 10pt; FONT-FAMILY: Arial">Utility
methods


style="MARGIN: 0in 0in 0pt 0.5in; TEXT-INDENT: -0.25in; mso-list: l8 level1 lfo18; tab-stops: list .5in"> face=Tahoma size=2> style="FONT-SIZE: 10pt; FONT-FAMILY: Tahoma; mso-fareast-font-family: Tahoma"> style="mso-list: Ignore">• style="FONT: 7pt 'Times New Roman'">        
style="FONT-SIZE: 10pt; FONT-FAMILY: Arial">Statics are the . w:st="on">NET equivalent of global variables or global
functions


style="MARGIN: 0in 0in 0pt 1in; TEXT-INDENT: -0.25in; mso-list: l8 level2 lfo18; tab-stops: list 1.0in"> face=Tahoma size=2> style="FONT-SIZE: 10pt; FONT-FAMILY: Tahoma; mso-fareast-font-family: Tahoma"> style="mso-list: Ignore">• style="FONT: 7pt 'Times New Roman'">        
style="FONT-SIZE: 10pt; FONT-FAMILY: Arial">Not object
oriented


style="MARGIN: 0in 0in 0pt 1in; TEXT-INDENT: -0.25in; mso-list: l8 level2 lfo18; tab-stops: list 1.0in"> face=Tahoma size=2> style="FONT-SIZE: 10pt; FONT-FAMILY: Tahoma; mso-fareast-font-family: Tahoma"> style="mso-list: Ignore">• style="FONT: 7pt 'Times New Roman'">        
style="FONT-SIZE: 10pt; FONT-FAMILY: Arial">Same evils as
global


style="MARGIN: 0in 0in 0pt 1in; TEXT-INDENT: -0.25in; mso-list: l8 level2 lfo18; tab-stops: list 1.0in"> face=Tahoma size=2> style="FONT-SIZE: 10pt; FONT-FAMILY: Tahoma; mso-fareast-font-family: Tahoma"> style="mso-list: Ignore">• style="FONT: 7pt 'Times New Roman'">        
style="FONT-SIZE: 10pt; FONT-FAMILY: Arial">But can be very useful, e.g.,
System.Math


style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"> 


style="FONT-WEIGHT: bold; FONT-SIZE: 10pt; FONT-FAMILY: Arial">Singleton
Pattern


style="MARGIN: 0in 0in 0pt 0.5in; TEXT-INDENT: -0.25in; mso-list: l19 level1 lfo19; tab-stops: list .5in"> face=Tahoma size=2> style="FONT-SIZE: 10pt; FONT-FAMILY: Tahoma; mso-fareast-font-family: Tahoma"> style="mso-list: Ignore">• style="FONT: 7pt 'Times New Roman'">        
style="FONT-SIZE: 10pt; FONT-FAMILY: Arial">Ensures that a class has only one
instance and provide a global point of access to it


style="MARGIN: 0in 0in 0pt 0.5in; TEXT-INDENT: -0.25in; mso-list: l19 level1 lfo19; tab-stops: list .5in"> face=Tahoma size=2> style="FONT-SIZE: 10pt; FONT-FAMILY: Tahoma; mso-fareast-font-family: Tahoma"> style="mso-list: Ignore">• style="FONT: 7pt 'Times New Roman'">        
style="FONT-SIZE: 10pt; FONT-FAMILY: Arial">This is not exactly the GoF pattern
(see threading section)


size=2> style="FONT-SIZE: 10pt; mso-bidi-font-family: 'Courier New'">public
sealed class DBNull {
  
private DBNull() {}
  
public static readonly DBNull Value = new DBNull();
style="mso-spacerun: yes">   // instance
Methods…


size=2> style="FONT-SIZE: 10pt; mso-bidi-font-family: 'Courier New'">}


size=2> style="FONT-SIZE: 10pt; mso-bidi-font-family: 'Courier New'">//Calling
code


size=2> style="FONT-SIZE: 10pt; mso-bidi-font-family: 'Courier New'">if (x ==
DBNull.Value) {..}


style="MARGIN: 0in 0in 0pt 0.5in; TEXT-INDENT: -0.25in; mso-list: l8 level1 lfo18; tab-stops: list .5in"> face=Tahoma size=2> style="FONT-SIZE: 10pt; FONT-FAMILY: Tahoma; mso-fareast-font-family: Tahoma"> style="mso-list: Ignore">• style="FONT: 7pt 'Times New Roman'">        
style="FONT-SIZE: 10pt; FONT-FAMILY: Arial">Notice this
class:


style="MARGIN: 0in 0in 0pt 1in; TEXT-INDENT: -0.25in; mso-list: l8 level2 lfo18; tab-stops: list 1.0in"> face=Tahoma size=2> style="FONT-SIZE: 10pt; FONT-FAMILY: Tahoma; mso-fareast-font-family: Tahoma"> style="mso-list: Ignore">• style="FONT: 7pt 'Times New Roman'">        
style="FONT-SIZE: 10pt; FONT-FAMILY: Arial">Is sealed to prevent sub-classing to
add instances


style="MARGIN: 0in 0in 0pt 1in; TEXT-INDENT: -0.25in; mso-list: l8 level2 lfo18; tab-stops: list 1.0in"> face=Tahoma size=2> style="FONT-SIZE: 10pt; FONT-FAMILY: Tahoma; mso-fareast-font-family: Tahoma"> style="mso-list: Ignore">• style="FONT: 7pt 'Times New Roman'">        
style="FONT-SIZE: 10pt; FONT-FAMILY: Arial">The style="FONT-WEIGHT: bold">Value is static for easy
access


style="MARGIN: 0in 0in 0pt 1in; TEXT-INDENT: -0.25in; mso-list: l8 level2 lfo18; tab-stops: list 1.0in"> face=Tahoma size=2> style="FONT-SIZE: 10pt; FONT-FAMILY: Tahoma; mso-fareast-font-family: Tahoma"> style="mso-list: Ignore">• style="FONT: 7pt 'Times New Roman'">        
style="FONT-SIZE: 10pt; FONT-FAMILY: Arial">The style="FONT-WEIGHT: bold">Value is readonly so it cannot be
modified


style="MARGIN: 0in 0in 0pt 1in; TEXT-INDENT: -0.25in; mso-list: l8 level2 lfo18; tab-stops: list 1.0in"> face=Tahoma size=2> style="FONT-SIZE: 10pt; FONT-FAMILY: Tahoma; mso-fareast-font-family: Tahoma"> style="mso-list: Ignore">• style="FONT: 7pt 'Times New Roman'">        
style="FONT-SIZE: 10pt; FONT-FAMILY: Arial">Has a private
constructor


style="MARGIN: 0in 0in 0pt 1in; TEXT-INDENT: -0.25in; mso-list: l8 level2 lfo18; tab-stops: list 1.0in"> face=Tahoma size=2> style="FONT-SIZE: 10pt; FONT-FAMILY: Tahoma; mso-fareast-font-family: Tahoma"> style="mso-list: Ignore">• style="FONT: 7pt 'Times New Roman'">        
style="FONT-SIZE: 10pt; FONT-FAMILY: Arial">The instance is immutable


style="MARGIN: 0in 0in 0pt 1.5in; TEXT-INDENT: -0.25in; mso-list: l8 level3 lfo18; tab-stops: list 1.5in"> face=Tahoma size=2> style="FONT-SIZE: 10pt; FONT-FAMILY: Tahoma; mso-fareast-font-family: Tahoma"> style="mso-list: Ignore">• style="FONT: 7pt 'Times New Roman'">        
style="FONT-SIZE: 10pt; FONT-FAMILY: Arial">No methods that can mutate its
state


style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"> 


style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"> 


style="FONT-WEIGHT: bold; FONT-SIZE: 10pt; FONT-FAMILY: Arial">Parameter
Passing
style="FONT-SIZE: 10pt; FONT-FAMILY: Arial">


style="MARGIN: 0in 0in 0pt 0.5in; TEXT-INDENT: -0.25in; mso-list: l1 level1 lfo20; tab-stops: list .5in"> face=Tahoma size=2> style="FONT-SIZE: 10pt; FONT-FAMILY: Tahoma; mso-fareast-font-family: Tahoma"> style="mso-list: Ignore">• style="FONT: 7pt 'Times New Roman'">        
style="FONT-SIZE: 10pt; FONT-FAMILY: Arial">Value types and Reference types can
both be passed by value or by reference


style="MARGIN: 0in 0in 0pt 0.5in; TEXT-INDENT: -0.25in; mso-list: l1 level1 lfo20; tab-stops: list .5in"> face=Tahoma size=2> style="FONT-SIZE: 10pt; FONT-FAMILY: Tahoma; mso-fareast-font-family: Tahoma"> style="mso-list: Ignore">• style="FONT: 7pt 'Times New Roman'">        
style="FONT-SIZE: 10pt; FONT-FAMILY: Arial">A value type by value copies the
value


style="MARGIN: 0in 0in 0pt 1in; TEXT-INDENT: -0.25in; mso-list: l1 level2 lfo20; tab-stops: list 1.0in"> face=Tahoma size=2> style="FONT-SIZE: 10pt; FONT-FAMILY: Tahoma; mso-fareast-font-family: Tahoma"> style="mso-list: Ignore">• style="FONT: 7pt 'Times New Roman'">        
style="FONT-SIZE: 10pt; FONT-FAMILY: Arial">No side
effects


style="MARGIN: 0in 0in 0pt 1in; TEXT-INDENT: -0.25in; mso-list: l1 level2 lfo20; tab-stops: list 1.0in"> face=Tahoma size=2> style="FONT-SIZE: 10pt; FONT-FAMILY: Tahoma; mso-fareast-font-family: Tahoma"> style="mso-list: Ignore">• style="FONT: 7pt 'Times New Roman'">        
style="FONT-SIZE: 10pt; FONT-FAMILY: Arial">Commonly used
style="mso-spacerun: yes"> public int Add (int x, int y)
{..}


style="MARGIN: 0in 0in 0pt 0.5in; TEXT-INDENT: -0.25in; mso-list: l1 level1 lfo20; tab-stops: list .5in"> face=Tahoma size=2> style="FONT-SIZE: 10pt; FONT-FAMILY: Tahoma; mso-fareast-font-family: Tahoma"> style="mso-list: Ignore">• style="FONT: 7pt 'Times New Roman'">        
style="FONT-SIZE: 10pt; FONT-FAMILY: Arial">A value type by reference uses a
pointer to the value


style="MARGIN: 0in 0in 0pt 1in; TEXT-INDENT: -0.25in; mso-list: l1 level2 lfo20; tab-stops: list 1.0in"> face=Tahoma size=2> style="FONT-SIZE: 10pt; FONT-FAMILY: Tahoma; mso-fareast-font-family: Tahoma"> style="mso-list: Ignore">• style="FONT: 7pt 'Times New Roman'">        
style="FONT-SIZE: 10pt; FONT-FAMILY: Arial">Side effects
possible


style="MARGIN: 0in 0in 0pt 1in; TEXT-INDENT: -0.25in; mso-list: l1 level2 lfo20; tab-stops: list 1.0in"> face=Tahoma size=2> style="FONT-SIZE: 10pt; FONT-FAMILY: Tahoma; mso-fareast-font-family: Tahoma"> style="mso-list: Ignore">• style="FONT: 7pt 'Times New Roman'">        
style="FONT-SIZE: 10pt; FONT-FAMILY: Arial">Rarely used
public static int
Exchange (ref int location, int value) {..}


style="MARGIN: 0in 0in 0pt 0.5in; TEXT-INDENT: -0.25in; mso-list: l21 level1 lfo21; tab-stops: list .5in"> face=Tahoma size=2> style="FONT-SIZE: 10pt; FONT-FAMILY: Tahoma; mso-fareast-font-family: Tahoma"> style="mso-list: Ignore">• style="FONT: 7pt 'Times New Roman'">        
style="FONT-SIZE: 10pt; FONT-FAMILY: Arial">A reference type by value copied the
reference


style="MARGIN: 0in 0in 0pt 1in; TEXT-INDENT: -0.25in; mso-list: l21 level2 lfo21; tab-stops: list 1.0in"> face=Tahoma size=2> style="FONT-SIZE: 10pt; FONT-FAMILY: Tahoma; mso-fareast-font-family: Tahoma"> style="mso-list: Ignore">• style="FONT: 7pt 'Times New Roman'">        
style="FONT-SIZE: 10pt; FONT-FAMILY: Arial">Side effects are possible on mutable
types


style="MARGIN: 0in 0in 0pt 1in; TEXT-INDENT: -0.25in; mso-list: l21 level2 lfo21; tab-stops: list 1.0in"> face=Tahoma size=2> style="FONT-SIZE: 10pt; FONT-FAMILY: Tahoma; mso-fareast-font-family: Tahoma"> style="mso-list: Ignore">• style="FONT: 7pt 'Times New Roman'">        
style="FONT-SIZE: 10pt; FONT-FAMILY: Arial">Commonly used
style="mso-spacerun: yes"> public void Insert (object value)
{..}


style="MARGIN: 0in 0in 0pt 0.5in; TEXT-INDENT: -0.25in; mso-list: l21 level1 lfo21; tab-stops: list .5in"> face=Tahoma size=2> style="FONT-SIZE: 10pt; FONT-FAMILY: Tahoma; mso-fareast-font-family: Tahoma"> style="mso-list: Ignore">• style="FONT: 7pt 'Times New Roman'">        
style="FONT-SIZE: 10pt; FONT-FAMILY: Arial">A reference type by reference uses a
pointer to the reference variable


style="MARGIN: 0in 0in 0pt 1in; TEXT-INDENT: -0.25in; mso-list: l21 level2 lfo21; tab-stops: list 1.0in"> face=Tahoma size=2> style="FONT-SIZE: 10pt; FONT-FAMILY: Tahoma; mso-fareast-font-family: Tahoma"> style="mso-list: Ignore">• style="FONT: 7pt 'Times New Roman'">        
style="FONT-SIZE: 10pt; FONT-FAMILY: Arial">Side effects
possible


style="MARGIN: 0in 0in 0pt 1in; TEXT-INDENT: -0.25in; mso-list: l21 level2 lfo21; tab-stops: list 1.0in"> face=Tahoma size=2> style="FONT-SIZE: 10pt; FONT-FAMILY: Tahoma; mso-fareast-font-family: Tahoma"> style="mso-list: Ignore">• style="FONT: 7pt 'Times New Roman'">        
style="FONT-SIZE: 10pt; FONT-FAMILY: Arial">Almost never used
public static
int Method (ref object moreData) {..}


style="MARGIN: 0in 0in 0pt 0.5in; TEXT-INDENT: -0.25in; mso-list: l21 level1 lfo21; tab-stops: list .5in"> face=Tahoma size=2> style="FONT-SIZE: 10pt; FONT-FAMILY: Tahoma; mso-fareast-font-family: Tahoma"> style="mso-list: Ignore">• style="FONT: 7pt 'Times New Roman'">        
style="FONT-SIZE: 10pt; FONT-FAMILY: Arial">Using Ref and Out
parameters


style="MARGIN: 0in 0in 0pt 1in; TEXT-INDENT: -0.25in; mso-list: l21 level2 lfo21; tab-stops: list 1.0in"> face=Tahoma size=2> style="FONT-SIZE: 10pt; FONT-FAMILY: Tahoma; mso-fareast-font-family: Tahoma"> style="mso-list: Ignore">• style="FONT: 7pt 'Times New Roman'">        
style="FONT-SIZE: 10pt; FONT-FAMILY: Arial">Primarily used for
interop


style="MARGIN: 0in 0in 0pt 1.5in; TEXT-INDENT: -0.25in; mso-list: l21 level3 lfo21; tab-stops: list 1.5in"> face=Tahoma size=2> style="FONT-SIZE: 10pt; FONT-FAMILY: Tahoma; mso-fareast-font-family: Tahoma"> style="mso-list: Ignore">• style="FONT: 7pt 'Times New Roman'">        
style="FONT-SIZE: 10pt; FONT-FAMILY: Arial">Avoid directly exposing
publicly


style="MARGIN: 0in 0in 0pt 1in; TEXT-INDENT: -0.25in; mso-list: l21 level2 lfo21; tab-stops: list 1.0in"> face=Tahoma size=2> style="FONT-SIZE: 10pt; FONT-FAMILY: Tahoma; mso-fareast-font-family: Tahoma"> style="mso-list: Ignore">• style="FONT: 7pt 'Times New Roman'">        
style="FONT-SIZE: 10pt; FONT-FAMILY: Arial">May be used for extremely
performance-sensitive areas


style="MARGIN: 0in 0in 0pt 1in; TEXT-INDENT: -0.25in; mso-list: l21 level2 lfo21; tab-stops: list 1.0in"> face=Tahoma size=2> style="FONT-SIZE: 10pt; FONT-FAMILY: Tahoma; mso-fareast-font-family: Tahoma"> style="mso-list: Ignore">• style="FONT: 7pt 'Times New Roman'">        
style="FONT-SIZE: 10pt; FONT-FAMILY: Arial">Almost exclusively used with value
types


style="MARGIN: 0in 0in 0pt 0.5in; TEXT-INDENT: -0.25in; mso-list: l21 level1 lfo21; tab-stops: list .5in"> face=Tahoma size=2> style="FONT-SIZE: 10pt; FONT-FAMILY: Tahoma; mso-fareast-font-family: Tahoma"> style="mso-list: Ignore">• style="FONT: 7pt 'Times New Roman'">        
style="FONT-SIZE: 10pt; FONT-FAMILY: Arial">Ref is a w:st="on">CLR feature


style="MARGIN: 0in 0in 0pt 1in; TEXT-INDENT: -0.25in; mso-list: l21 level2 lfo21; tab-stops: list 1.0in"> face=Tahoma size=2> style="FONT-SIZE: 10pt; FONT-FAMILY: Tahoma; mso-fareast-font-family: Tahoma"> style="mso-list: Ignore">• style="FONT: 7pt 'Times New Roman'">        
style="FONT-SIZE: 10pt; FONT-FAMILY: Arial">Out is a C#
feature


style="MARGIN: 0in 0in 0pt 1in; TEXT-INDENT: -0.25in; mso-list: l21 level2 lfo21; tab-stops: list 1.0in"> face=Tahoma size=2> style="FONT-SIZE: 10pt; FONT-FAMILY: Tahoma; mso-fareast-font-family: Tahoma"> style="mso-list: Ignore">• style="FONT: 7pt 'Times New Roman'">        
style="FONT-SIZE: 10pt; FONT-FAMILY: Arial">Out parameter semantics downgrade to
Ref semantics in other languages


style="MARGIN: 0in 0in 0pt 0.5in; TEXT-INDENT: -0.25in; mso-list: l21 level1 lfo21; tab-stops: list .5in"> face=Tahoma size=2> style="FONT-SIZE: 10pt; FONT-FAMILY: Tahoma; mso-fareast-font-family: Tahoma"> style="mso-list: Ignore">• style="FONT: 7pt 'Times New Roman'">        
style="FONT-SIZE: 10pt; FONT-FAMILY: Arial">Ref and out designs are less
usable


size=2> style="FONT-SIZE: 10pt; mso-bidi-font-family: 'Courier New'">public void
GetLocation (
style="mso-tab-count: 2">           
ref int x, out int y) {..}


size=2> style="FONT-SIZE: 10pt; mso-bidi-font-family: 'Courier New'">//calling
code


size=2> style="FONT-SIZE: 10pt; mso-bidi-font-family: 'Courier New'">int x =
42;
int y;
b.GetLocation (ref x, out y);


size=2> style="FONT-SIZE: 10pt; mso-bidi-font-family: 'Courier New'"> 


size=2> style="FONT-SIZE: 10pt; mso-bidi-font-family: 'Courier New'"> 


size=2> style="FONT-SIZE: 10pt; mso-bidi-font-family: 'Courier New'">public
struct Point {
  public int X
{get;}
  public int Y
{get;}
}


size=2> style="FONT-SIZE: 10pt; mso-bidi-font-family: 'Courier New'">//calling
code


size=2>Point
p = b.Location;


style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"> 


style="FONT-WEIGHT: bold; FONT-SIZE: 10pt; FONT-FAMILY: Arial">Argument
Validation
style="FONT-SIZE: 10pt; FONT-FAMILY: Arial">


style="MARGIN: 0in 0in 0pt 0.5in; TEXT-INDENT: -0.25in; mso-list: l6 level1 lfo22; tab-stops: list .5in"> face=Tahoma size=2> style="FONT-SIZE: 10pt; FONT-FAMILY: Tahoma; mso-fareast-font-family: Tahoma"> style="mso-list: Ignore">• style="FONT: 7pt 'Times New Roman'">        
style="FONT-SIZE: 10pt; FONT-FAMILY: Arial">Do argument checking on every
publicly exposed member


style="MARGIN: 0in 0in 0pt 1in; TEXT-INDENT: -0.25in; mso-list: l6 level2 lfo22; tab-stops: list 1.0in"> face=Tahoma size=2> style="FONT-SIZE: 10pt; FONT-FAMILY: Tahoma; mso-fareast-font-family: Tahoma"> style="mso-list: Ignore">• style="FONT: 7pt 'Times New Roman'">        
style="FONT-SIZE: 10pt; FONT-FAMILY: Arial">Catches errors early
(fail-fast)


style="MARGIN: 0in 0in 0pt 1in; TEXT-INDENT: -0.25in; mso-list: l6 level2 lfo22; tab-stops: list 1.0in"> face=Tahoma size=2> style="FONT-SIZE: 10pt; FONT-FAMILY: Tahoma; mso-fareast-font-family: Tahoma"> style="mso-list: Ignore">• style="FONT: 7pt 'Times New Roman'">        
style="FONT-SIZE: 10pt; FONT-FAMILY: Arial">Much easier to
debug


style="MARGIN: 0in 0in 0pt 1in; TEXT-INDENT: -0.25in; mso-list: l6 level2 lfo22; tab-stops: list 1.0in"> face=Tahoma size=2> style="FONT-SIZE: 10pt; FONT-FAMILY: Tahoma; mso-fareast-font-family: Tahoma"> style="mso-list: Ignore">• style="FONT: 7pt 'Times New Roman'">        
style="FONT-SIZE: 10pt; FONT-FAMILY: Arial">Powerful security
precaution


style="MARGIN: 0in 0in 0pt 0.5in; TEXT-INDENT: -0.25in; mso-list: l6 level1 lfo22; tab-stops: list .5in"> face=Tahoma size=2> style="FONT-SIZE: 10pt; FONT-FAMILY: Tahoma; mso-fareast-font-family: Tahoma"> style="mso-list: Ignore">• style="FONT: 7pt 'Times New Roman'">        
style="FONT-SIZE: 10pt; FONT-FAMILY: Arial">Throw meaningful
exceptions


style="MARGIN: 0in 0in 0pt 1in; TEXT-INDENT: -0.25in; mso-list: l6 level2 lfo22; tab-stops: list 1.0in"> face=Tahoma size=2> style="FONT-SIZE: 10pt; FONT-FAMILY: Tahoma; mso-fareast-font-family: Tahoma"> style="mso-list: Ignore">• style="FONT: 7pt 'Times New Roman'">        
style="FONT-SIZE: 10pt; FONT-FAMILY: Arial">Subclasses of
ArgumentException


size=2> style="FONT-SIZE: 10pt; mso-bidi-font-family: 'Courier New'">public int
Count {
   get {return
count;}
   set {
style="mso-spacerun: yes">      if (value < 0
|| value >= MaxValue)
style="mso-spacerun: yes">        
throw new ArgumentException(..);
style="mso-spacerun: yes">   }
}
public void Select (int
start, int end) {
   if
(start < 0)
     
 throw new
ArgumentException(..);
   if
(end < start)
style="mso-spacerun: yes">      throw new
ArgumentException(..);
}


style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"> 


 

Comments (18)

  1. Frans Bouma says:

    A reference type by value copied the reference
    * Commonly used: public void Insert (object value) {..}

    I might be wrong, but ‘value’ in the example above is passed by reference since it’s an object, am I right?

    Also, an important (IMHO ;)) thing about events is missing from the list (and from MS’ documentation), which is: if object A exposes an event, and object B subscribes on that event by defining a new event handler (A.Event += new EventHandler(name); ), and B goes out of scope it will not be garbage collected until A is also garbage collected, because A holds a reference to a method in B.

  2. >I might be wrong, but ‘value’ in the example above is passed by reference since it’s an object, am I right?

    Yes and no. In Java is a well known subtle issue. It’s a matter of definitions. In the example above you pass the object reference by value. Hence what Brad said it’s right.

  3. Steve says:

    Thanks for the excellent tips…keep them coming!

  4. Frans Bouma says:

    what’s ‘passing a reference by value’ ? You pass OR a reference (i.e. pointer) OR a value (i.e. a copy of the original object). In compiler builder land, ‘call by value’ is used to specify the calling scheme where the passed in variable’s contents is copied into the stackframe. Call by reference is used to specify the calling scheme where the passed in variable’s address is copied into the stackframe. The latter is used for objects( or structures pointed by by pointers 🙂 ) or references to value typed variables.

    The reason I asked was that when you do not specify ‘ref’ you can still modify a passed in object in a called method and use the modification results in the caller. So passing by ‘value’ is not possible when using with object typed variables/parameters (you have to do that yourself: copy the object and pass that in).

    This is also a point of discussion where some people argued for the keyword ‘const’ in the method header, which effectually makes a parameter called by value, even though it is passed in by reference. 🙂 (at the moment this is not possible).

  5. I think that you didn’t understand what I mean. When you pass around value types you pass the value (the contained data) by default or if you specify ref you pass a reference (the address I guess) of the variable. This is what I call "passing a value type by value or by reference". When you pass around an object you don’t pass its value but the address of the object location. This is what I call "passing the reference by value". As Brad stated there is another passing method that is passing the object by reference. In this situation you pass a reference of the reference (a pointer) and this is what I call "passing the reference by reference".

    It’s a matter of terminology.

  6. Michael Bouck says:

    On Field usage (I might be starting a religious war here!):

    I personally believe that non-public fields should be somehow prefix-notated as such (_foo, m_foo, etc.) This accomplishes several things:

    1) It is immediately obvious that you are referencing a non-public entity.
    2) It removes the necessity of using the "this" operator to resolve abiguity.
    3) It fits into well-known paradigms for programmers with a C++/Java background.

    One caveat of using "_"-previxed fields, though, is that it’s not CLS-compliant and will cause the C# compiler to throw a error if CLS compliance is turned-on. In any event, I feel the pros outweigh the cons enough that this guidance should be formalized in the guidelines…

  7. Rick Byers says:

    > Property getters should be simple

    In my oppinion, this cannot be overstated. When using a property it is natural to assume that calling the getter has roughly the same implications as a field access (eg. performance, blocking, side effects, locking, etc.). Infact, the VStudio debugger makes this assumption by having property evaulation enabled in the watch windows by default, which results in MANY getter calls (my tests with VS.Net 2002 showed ~18 calls per property per debug step even after the object containing the property was collapsed or otherwise made not visible!).

    Due to non-trivial property getters (mostly in the apps I’m debugging, but I believe there are a few in the .NET framework) I’ve been forced to disable automatic property evaluation during debuging. OtherwiseI I occasionally run into horrible performance problems (the VStudio IDE moves very slowly) and sometimes runtime deadlocks (VS is fine, but debugger won’t make any progress).

    My rule of thumb is if my getter is going to be much more complex than "return someField", I use a ‘Get’ method instead.

    Thanks for the great information Brad!

  8. Kevin Dente says:

    Here’s a couple of questions/comments:

    You say "For static fields, do not include a prefix on a public field name". In general, it looks like MS make an effort to expunge hungarian notation from the coding conventions, which I think is goodness. However, there is one place where a form of it remains – the "I" prefix in interfaces. Why the hold over? Personally, I think it junks up the APIs and thus subtly discourages the use of interface-based programming.

    You also comment that it’s "common to have read-only public access and protected write access". One of the few things that I don’t like about C# is the requirement that property getters and setter have the same accessibility, which requires ugliness like separate Set methods. Any hope that this will be addressed in the future?

    Thanks for all the great info. Good stuff.

  9. >However, there is one place where a form of it remains – the "I" prefix in interfaces. Why the hold over? Personally, I think it >junks up the APIs and thus subtly discourages the use of interface-based programming.

    Why do you think it messes up? I like "I" in front of interface. It’s a widely used convention (COM, Java) and it allows you to quickly identify interfaces in the object trees.

  10. Frans Bouma says:

    "As Brad stated there is another passing method that is passing the object by reference. In this situation you pass a reference of the reference (a pointer) and this is what I call "passing the reference by reference". "
    Hmm, I understand, but a reference of a reference would require a pointerpointer to access the actual data, thus a **pFoo. However, it doesn’t matter if you specify ‘ref’ for an object or not, your code would work the same. This can mean 2 things: OR the CLR does the double pointer jump under the hood, OR ‘ref’ is not necessary for passing the object to a method :). If I understand you right, Lawrence, the CLR does the doublepointer logic under the hood, I thought ‘ref’ was not necessary for objects, that’s the miscommunication we’re having I think 🙂

  11. Thomas Freudenberg says:

    You’re giving following example
    public class Button {
    private ButtonClickHandler onClickHandler;
    protected void OnClick (ClickEventArgs e) {
    if (onClickHandler != null) {
    // call the delegate if non-null
    onClickHandler(this, e);
    }
    }
    }

    According to the naming convention, shouldn’t the ButtonClickHandler field be named ‘Click’, since some tips above you say "Name events with a verb, e.g. Click, Paint, DrawItem, DropDown"?

  12. Kevin Westhead says:

    Regarding byval and byref for reference types, here is how I understand it:

    void DoSomething(object someValue)
    A copy of the handle to someValue on the managed heap is passed to DoSomething. This means that you can still change the state of the object via its properties and methods, but you cannot change the original handle. In otherwords, setting someValue to null in the callee will not null the original object that was passed by the caller.

    void DoSomething(ref object someValue)
    The original handle to someValue on the managed heap is passed to DoSomething. Setting someValue to null in callee will null the original object that was passed by the caller.

  13. This is exactly what I meant.

  14. Diego Gonzalez says:

    Just my 2 cents…. "Any kind of member can be static", except class indexers, which can’t be static.