Csharp/CSharp Tutorial/Class/Class Instance

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

Call GetType and ToString on object instance

using System;
public class Thing 
{
    public int i = 2;
    public int j = 3;
}
   
public class objectTypeApp
{
    public static void Main() 
    {
        object a;
        a = 1;
        Console.WriteLine(a);
        Console.WriteLine(a.ToString());
        Console.WriteLine(a.GetType());
        Console.WriteLine();
   
        Thing b = new Thing();
        Console.WriteLine(b);
        Console.WriteLine(b.ToString());
        Console.WriteLine(b.GetType());
    }
}

Creating an array of objects.

using System;
class MainClass {
    private int fValue;
    public MainClass() {
        fValue = 0;
    }
    public void setValue(int value) {
        fValue = value;
    }
    public int getValue() {
        return fValue;
    }
    public static void Main() {
        MainClass[] arr = new MainClass[10];
        for (int i = 0; i < 10; ++i)
            arr[i] = new MainClass();
        for (int i = 0; i < 10; ++i)
            arr[i].setValue(i);
        for (int i = 0; i < 10; ++i)
            Console.WriteLine("Object {0}. Value = {1}",
              i, arr[i].getValue());
    }
}

Instance Class Members

using System;
class MyClass
{
   public int memberVariable;
}
class MainClass
{
   static void Main()
   {
      MyClass d1 = new MyClass();
      MyClass d2 = new MyClass();
      d1.memberVariable = 10; 
      d2.memberVariable = 28;
      Console.WriteLine("d1 = {0}, d2 = {1}", d1.memberVariable, d2.memberVariable);
   }
}
d1 = 10, d2 = 28

Invoke a method in a class

using System;

class MyClass
{
   void PrintDateAndTime()               
   {
      DateTime dt = DateTime.Now;        
      Console.WriteLine("{0}", dt);      
   }
   static void Main()
   {
      MyClass mc = new MyClass();
      mc.PrintDateAndTime();             
   }
}
25/03/2007 2:14:04 PM

public member method

using System;
class MyClass
{
   public int High, Low;
   
   public int Sum() 
   {
      return High + Low;
   }
}
class MainClass {
   static void Main()
   {
      MyClass T1 = new MyClass();
      MyClass T2 = new MyClass();
      T1.High = 76; 
      T1.Low = 57;
      
      T2.High = 75; 
      T2.Low = 53;
      Console.WriteLine("T1: {0}, {1}, {2}", T1.High, T1.Low, T1.Sum ());
      Console.WriteLine("T2: {0}, {1}, {2}", T2.High, T2.Low, T2.Sum ());
   }
}
T1: 76, 57, 133
T2: 75, 53, 128

Try to change static value in different instance

using System;
class MyClass
{
   int nonStaticVariable;
   static int staticVariable;
   public void SetVars(int v1, int v2) 
   {
      nonStaticVariable = v1;
      staticVariable = v2;
   }
   public void Display()
   {
      Console.WriteLine("nonStaticVariable= {1}, staticVariable= {2}", nonStaticVariable, staticVariable);
   }
}
class MainClass
{
   static void Main()
   {
      MyClass d1 = new MyClass(), d2 = new MyClass();
      d1.SetVars(2, 4);    
      d1.Display();
      d2.SetVars(15, 17);  
      d2.Display();
   }                       
}