Csharp/C Sharp/Class Interface/Object Instance

Материал из .Net Framework эксперт
Версия от 14:39, 26 мая 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

Calling a Class Method on a Class

<source lang="csharp">

using System; public class MainClass {

   public static void Main() {
       string firstName = "J";
       string lastName = "R";
       string name = string.Format("{0} {1}", firstName, lastName);
       Console.WriteLine(name);
   }

}

      </source>


Calling an Instance Method on an Object

<source lang="csharp">


using System; public class MainClass {

   public static void Main() {
       string name = "Joe";
       Console.WriteLine(name.ToUpper());
   }

}

      </source>


Catch NullReferenceException Exception

<source lang="csharp"> using System; class MyClass {

   public int Value;

} class MainClass {

   public static void Main() {
       try {
           MyClass MyObject = new MyClass();
           MyObject = null;
           MyObject.Value = 123;
       } catch (NullReferenceException) {
           Console.WriteLine("Cannot reference a null object.");
       }
   }

}

      </source>


Object assignment

<source lang="csharp">

using System; public class Foo {

   public int i;

}

class RefTest1App {

   static void Main(string[] args)
   {
       Foo test1 = new Foo();
       test1.i = 1;
  
       Foo test2 = new Foo();
       test2.i = 2;
  
       Console.WriteLine("BEFORE OBJECT ASSIGNMENT");
       Console.WriteLine("test1.i={0}", test1.i);
       Console.WriteLine("test2.i={0}", test2.i);
       Console.WriteLine();
  
       test1 = test2;
  
       Console.WriteLine("AFTER OBJECT ASSIGNMENT");
       Console.WriteLine("test1.i={0}", test1.i);
       Console.WriteLine("test2.i={0}", test2.i);
       Console.WriteLine();
  
       test1.i = 42;
  
       Console.WriteLine("AFTER CHANGE TO ONLY TEST1 MEMBER");
       Console.WriteLine("test1.i={0}", test1.i);
       Console.WriteLine("test2.i={0}", test2.i);
   }

}

</source>


Object.ReferenceEquals Method

<source lang="csharp"> using System;

public class Starter {

   public static void Main() {
       Employee obj1 = new Employee(5678);
       Employee obj2 = (Employee)obj1.Clone();
       if (Employee.ReferenceEquals(obj1, obj2)) {
           Console.WriteLine("objects identical");
       } else {
           Console.WriteLine("objects not identical");
       }
   }

} class Employee : ICloneable {

   public Employee(int id) {
       if ((id < 1000) || (id > 9999)) {
           throw new Exception(
               "Invalid Employee ID");
       }
   }
   public object Clone() {
       return MemberwiseClone();
   }

}

</source>


Pass by ref for object parameters

<source lang="csharp">

using System; using System.Collections.Generic; using System.Text; class Pass {

   public static void Value(ref int param) {
       param = 42;
   }
   public static void Reference(WrappedInt param) {
       param.Number = 42;
   }

} class WrappedInt {

   public int Number;

} class Program {

   static void Entrance() {
       int i = 0;
       Console.WriteLine(i);
       Pass.Value(ref i);
       Console.WriteLine(i);
       WrappedInt wi = new WrappedInt();
       Console.WriteLine(wi.Number);
       Pass.Reference(wi);
       Console.WriteLine(wi.Number);
   }
   static void Main(string[] args) {
       try {
           Entrance();
       } catch (Exception ex) {
           Console.WriteLine(ex.Message);
       }
   }

}

</source>


Point object

<source lang="csharp"> using System; using System.Collections.Generic; using System.Text; class Point {

   public Point() {
       this.x = -1;
       this.y = -1;
       objectCount++;
   }
   public Point(int x, int y) {
       this.x = x;
       this.y = y;
       objectCount++;
   }
   public double DistanceTo(Point other) {
       int xDiff = this.x - other.x;
       int yDiff = this.y - other.y;
       return Math.Sqrt((xDiff * xDiff) + (yDiff * yDiff));
   }
   public static int ObjectCount() {
       return objectCount;
   }
   private int x, y;
   private static int objectCount = 0;

} class Program {

   static void Main(string[] args) {
       try {
           Point origin = new Point();
           Point bottomRight = new Point(1024, 1280);
           double distance = origin.DistanceTo(bottomRight);
           Console.WriteLine("Distance is {0}", distance);
           Console.WriteLine("No of Point objects: {0}", Point.ObjectCount());
       } catch (Exception ex) {
           Console.WriteLine(ex.Message);
       }
   }

}

</source>