- using System;
namespace primer__klassa_1
{
/// <summary>
/// Summary description for Class1.
/// </summary>
class MyClass
{
public MyClass()
{
Console.WriteLine(» constructor_1″);
}
public MyClass(int value)
{
MyField = value;
Console.WriteLine(» constructor_2″);
}
~MyClass()
{
Console.WriteLine(«Destructor»);
}
public const int MyConst = 12;
public int MyField = 34;
public void MyMethod()
{
Console.WriteLine(«MyClass.MyMethod»);
}
public int MyProperty
{
get
{
return MyField;
}
set
{
MyField = value;
}
}
public int this[int index]
{
get
{
return 0;
}
set
{
Console.WriteLine(«this[{0}] = {1}», index, value);
}
}
public event EventHandler MyEvent;
public static MyClass operator+(MyClass a, MyClass b)
{
return new MyClass(a.MyField + b.MyField);
}
internal class MyNestedClass
{}
}
class Class1
{
[STAThread]
static void Main(string[] args)
{
// Instance constructor usage
MyClass a = new MyClass();
MyClass b = new MyClass(123);
// Constant usage
Console.WriteLine(«MyConst = {0}», MyClass.MyConst);
// Field usage
a.MyField++;
Console.WriteLine(«a.MyField = {0}», a.MyField);
// Method usage
a.MyMethod();
// Property usage
a.MyProperty++;
Console.WriteLine(«a.MyProperty = {0}», a.MyProperty);
// Indexer usage
a[3] = a[1] = a[2];
Console.WriteLine(«a[3] = {0}», a[3]);
// Event usage
a.MyEvent += new EventHandler(MyHandler);
// Overloaded operator usage
MyClass c = a + b;
Console.WriteLine(«c.MyField = {0}», c.MyField);
Console.ReadLine();
}
static void MyHandler(object sender, EventArgs e)
{
Console.WriteLine(«Test.MyHandler»);
}
internal class MyNestedClass
{}
}
}
- using System;
namespace klass_2
{
/// <summary>
/// Summary description for Class1.
/// </summary>
public class Digit
{
public byte value;
public Digit(byte value)
{
if (value < 0 || value > 9) throw new ArgumentException();
this.value = value;
}
public Digit(int value): this((byte) value) {}
public static implicit operator byte(Digit d)
{
return d.value;
}
public static explicit operator Digit(byte b)
{
return new Digit(b);
}
public static Digit operator+(Digit a, Digit b)
{
return new Digit(a.value + b.value);
}
public static Digit operator-(Digit a, Digit b)
{
return new Digit(a.value — b.value);
}
public static bool operator==(Digit a, Digit b)
{
return a.value == b.value;
}
public static bool operator!=(Digit a, Digit b)
{
return a.value != b.value;
}
public override bool Equals(object value)
{
return this == (Digit) value;
}
public override int GetHashCode()
{
return value.GetHashCode();
}
public override string ToString()
{
return value.ToString();
}
}
class Class1
{
static void Main(string[] args)
{
Digit a = (Digit)5;
Digit b = (Digit)3;
Digit plus = a + b;
Digit minus = a-b;
bool equals = (a == b);
Console.WriteLine(«{0} + {1} = {2}», a, b, plus);
Console.WriteLine(«{0} — {1} = {2}», a, b, minus);
Console.WriteLine(«{0} == {1} = {2}», a, b, equals);
Console.ReadLine();
}
}
}
- using System;
class Point
{
public double x, y;
public Point() {
this.x = 0;
this.y = 0;
}
public Point(double x, double y) {
this.x = x;
this.y = y;
}
public static double Distance(Point a, Point b) {
double xdiff = a.x – b.x;
double ydiff = a.y – b.y;
return Math.Sqrt(xdiff * xdiff + ydiff * ydiff);
}
public override string ToString() {
return string.Format(«({0}, {1})», x, y);
}
}
class Test
{
static void Main() {
Point a = new Point();
Point b = new Point(3, 4);
double d = Point.Distance(a, b);
Console.WriteLine(«Distance from {0} to {1} is {2}», a, b, d);
}
}