Field Attributes

   
 

using System;
using System.Reflection;

public enum RegHives {
    HKEY_CLASSES_ROOT,
    HKEY_CURRENT_USER,
    HKEY_LOCAL_MACHINE,
    HKEY_USERS,
    HKEY_CURRENT_CONFIG
}

public class RegKeyAttribute : Attribute {
    public RegKeyAttribute(RegHives Hive, String ValueName) {
        this.Hive = Hive;
        this.ValueName = ValueName;
    }

    protected RegHives hive;
    public RegHives Hive {
        get { return hive; }
        set { hive = value; }
    }

    protected String valueName;
    public String ValueName {
        get { return valueName; }
        set { valueName = value; }
    }
}

class SomeClass {
    [RegKey(RegHives.HKEY_CURRENT_USER, "Foo")]
    public int Foo;

    public int Bar;
}

class Test {
    [STAThread]
    static void Main(string[] args) {
        Type type = Type.GetType("FieldAttribs.SomeClass");
        foreach (FieldInfo field in type.GetFields()) {
            foreach (Attribute attr in
                field.GetCustomAttributes(true)) {
                RegKeyAttribute rka =
                    attr as RegKeyAttribute;
                if (null != rka) {
                    Console.WriteLine("{0} will be saved in {1}{2}", field.Name, rka.Hive, rka.ValueName);
                }
            }
        }
    }
}

    


The super-string class.

using System;

public class MyString {
private string fString;

public MyString() {
fString = “”;
}
public MyString(string inStr) {
fString = inStr;
}
public string ToStr() {
return fString;
}
public string Right(int nChars) {
if (nChars > fString.Length)
return fString;

string s = “”;
for (int i = fString.Length – nChars; i < fString.Length; ++i) s += fString[i]; return s; } public string Left(int nChars) { if (nChars > fString.Length)
return fString;
string s = “”;
for (int i = 0; i < nChars; ++i) s += fString[i]; return s; } public string Mid(int nStart, int nEnd) { if (nStart < 0 || nEnd > fString.Length)
return fString;
if (nStart > nEnd)
return “”;

string s = “”;
for (int i = nStart; i < nEnd; ++i) s += fString[i]; return s; } } class Class1 { static void Main(string[] args) { MyString s = new MyString("Hello world"); System.Console.WriteLine("s = {0}", s.ToStr()); System.Console.WriteLine("Right 3 = [{0}]", s.Right(3)); System.Console.WriteLine("Left 6 = [{0}]", s.Left(6)); System.Console.WriteLine("Mid 2,4 = [{0}]", s.Mid(2, 4)); } } [/csharp]

change field value in a method

   
 

using System;

public class Foo
{
    public int i;
}
   
class RefTest2App
{
    public static void ChangeValue(Foo f)
    {
        f.i = 42;
    }
   
    static void Main(string[] args)
    {
        Foo test = new Foo();
        test.i = 6;
   
        Console.WriteLine("BEFORE METHOD CALL");
        Console.WriteLine("test.i={0}", test.i);
        Console.WriteLine();
   
        ChangeValue(test);
   
        Console.WriteLine("AFTER METHOD CALL");
        Console.WriteLine("test.i={0}", test.i);
    }
}

    


C# Classes Member Functions


   


using System;

public class MemberFunctions
{
    public static void Main()
    {
        Point myPoint = new Point(10, 15);
        Console.WriteLine("myPoint.X {0}", myPoint.GetX());
        Console.WriteLine("myPoint.Y {0}", myPoint.GetY());
    }
}
class Point
{
    public Point(int x, int y)
    {
        this.x = x;
        this.y = y;
    }
    
    // accessor functions
public int GetX() {return(x);}
public int GetY() {return(y);}
    
    // variables now private
    int x;
    int y;
}



           
          


Overloading Classes


   

/*
 * C# Programmers Pocket Consultant
 * Author: Gregory S. MacBeth
 * Email: gmacbeth@comporium.net
 * Create Date: June 27, 2003
 * Last Modified Date:
 */
using System;

namespace Client.Chapter_5___Building_Your_Own_Classes
{
  public class OverloadingClasses
  {
    static void Main(string[] args)
    {
      A MyA = new A();

      MyA.Display();
      MyA.Display(10);
    }
  }
  class A
  {
    public void Display()
    {
      Console.WriteLine("No Params Display Method");
    }
    public void Display(int A)
    {
      Console.WriteLine("Overloaded Display {0}", A);
    }
  }
}

           
          


A simple example of recursion

/*
C#: The Complete Reference
by Herbert Schildt

Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/

// A simple example of recursion.

using System;

class Factorial {
// This is a recursive function.
public int factR(int n) {
int result;

if(n==1) return 1;
result = factR(n-1) * n;
return result;
}

// This is an iterative equivalent.
public int factI(int n) {
int t, result;

result = 1;
for(t=1; t <= n; t++) result *= t; return result; } } public class Recursion { public static void Main() { Factorial f = new Factorial(); Console.WriteLine("Factorials using recursive method."); Console.WriteLine("Factorial of 3 is " + f.factR(3)); Console.WriteLine("Factorial of 4 is " + f.factR(4)); Console.WriteLine("Factorial of 5 is " + f.factR(5)); Console.WriteLine(); Console.WriteLine("Factorials using iterative method."); Console.WriteLine("Factorial of 3 is " + f.factI(3)); Console.WriteLine("Factorial of 4 is " + f.factI(4)); Console.WriteLine("Factorial of 5 is " + f.factI(5)); } } [/csharp]

Automatic type conversions can affect overloaded method resolution


   

/*
C#: The Complete Reference 
by Herbert Schildt 

Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/


/* Automatic type conversions can affect 
   overloaded method resolution. */ 
 
using System; 
 
class Overload2 { 
  public void f(int x) { 
    Console.WriteLine("Inside f(int): " + x); 
  } 
 
  public void f(double x) { 
    Console.WriteLine("Inside f(double): " + x); 
  } 
} 
 
public class TypeConv { 
  public static void Main() { 
    Overload2 ob = new Overload2(); 
 
    int i = 10; 
    double d = 10.1; 
 
    byte b = 99; 
    short s = 10; 
    float f = 11.5F; 
 
 
    ob.f(i); // calls ob.f(int) 
    ob.f(d); // calls ob.f(double) 
 
    ob.f(b); // calls ob.f(int) -- type conversion 
    ob.f(s); // calls ob.f(int) -- type conversion 
    ob.f(f); // calls ob.f(double) -- type conversion 
  } 
}