C# Class Demonstration (.Net)

If you have mastered Pascal classes and objects, you should find it easy to use them in C#. A simple example follows. Note that instead of a Create constructor, you have a method with the same identifier as the class.

using System;

namespace classes
{
  class Player
  {
    private String name;
    private Single points;

    public Player(String forename, Single pts)  
    {
      this.name = forename;
      this.points = pts;
    }

    public  string GetName()
    {
      return this.name;
    }

    public void AddPoints (Single newPoints)
    {
      this.points += newPoints;
    }

    public void ShowPoints()
    {
      Console.WriteLine("Points for " + this.GetName() + ": {0:0.0}", this.GetPoints());
    }

    public Single GetPoints()
    {
      return this.points;
    }
  }

  static class Program
  {
    public static void Main(string[] args)
    {
      Player p1 = new Player("Jo", 0.0f);
      Random rnd = new Random();
      Single score  = rnd.Next(1, 6);
      score = Convert.ToSingle(score) / 2;
      p1.AddPoints(score);
      p1.ShowPoints();
      Console.ReadLine();
    }
  }
}

Programming - a skill for life!

C# Demonstrations of I/O, arrays, methods, classes, loops, enums, pointers, file handling, Firebird databases, LINQ and drawing on a form