C# , . . , . , , . , . ( ), .
.
namespace Virtual1
{
abstract class Shape
{ // ,
// abstract !
public int a,h;
public Shape (int x,int y)
{
a=x;
h=y;
}
public abstract void Show_area();
// ,
//
}
class Tri:Shape
{
// . ,
// public override void Show_area() !
}
class Square:Shape
{
// .
// public override void Show_area() !
}
class Class1
{
static void Main(string[] args)
{
Shape q; //
// q=new Shape(4,6); ,
// !
q = new Tri(10,20);
q.Show_area();
q = new Square(10,20);
q.Show_area();
Console.ReadLine();
} } }
.
, , . . , . C# , , . , . :
interface _
{
__ _1();
__ _2();
.........
}
. C# : . , , . , .
.
namespace Interface1
{
public interface Int1
{ //
double area(double x);
double per();
string type();
}
struct Tri:Int1 //
{ //
|
|
double a,b,c;
//
public double area(double x)
{double p;
p=(a+b+c)/2;
return x*Math.Sqrt(p*(p-a)*(p-b)*(p-c));
}
public double per()
{
return a+b+c;
}
public string type()
{
return "Treug";
}
public Tri(double a1,double b1,double c1)
// ()
{
a=a1;
b=b1;
c=c1;
}
}
class Neli:Int1
{ //
double a,h;
public Neli(){
a=10;h=5;
}
//
public double area(double x)
{
return x*a*h;;
}
public double per()
{
return 2*(a+h);
}
public string type()
{
return "Square";
}
}
class Class1
{
static void Main(string[] args)
{
Tri t1=new Tri(4.2,5.6,7.2);
Console.Write("Area={0:####.##}",t1.area(0.8));
Console.WriteLine(" Perim="+t1.per());
Neli r1=new Neli();
Console.WriteLine("Area 2 = "+r1.area(1.0)+
" Perim 2 ="+r1.per());
Console.ReadLine();
}
} }
: . , . :
static void Main(string[] args)
{
Int1 xint; //
xint = new Tri(3, 4, 5); //
Console.Write("Area={0:####.##}", xint.area(0.8));
Console.WriteLine(" Perim=" + xint.per());
Neli r1 = new Neli();
xint = r1; //
Console.WriteLine("Area 2 = " + xint.area(1.0)
+ " Perim 2 =" + xint.per());
Console.ReadLine();
}
, . , , . , , , , . C# ++.
:
delegate __ _ (__);
_ __;
.
__ = new _ (_);
( ).
.
1. 3.12 :
namespace Virtual1
{
delegate void del1(); // del1
// 3.12
}
del1() .
:
static void Main(string[] args)
{
Shape q, r;
q = new Tri(10,20);
del1 f1 = new del1(q.Show_area);
// f1
// Show_area q Tri
//
r = new Square(10,20);
|
|
del1 f2 = new del1(r.Show_area);
// f2
// Show_area r Square
//
f1(); // q.Show_area();
f2(); // r.Show_area();
Console.ReadLine();
}
. r , q.
, , .
2. .
namespace Deleg
{
delegate double fun1(double x); //
class Test
{
protected double []x;
protected double y=0;
protected int n;
public Test()
{
n=5;
x=new double[n];
for(int i=0;i<x.Length;i++)
{
Console.Write("X["+i+"]=");
x[i]=Convert.ToDouble(Console.ReadLine());
}
}
public void gg(fun1 ff)
//
{
for(int i=0;i<x.Length;i++)
y+=(double)ff(x[i]);
//
Console.WriteLine("Summa={0:##.###}",y);
}
public static double w1(double p)
{return Math.Sin(p);}
public double w2(double p)
{return Math.Log(p);}
}
class Class1
{
static void Main(string[] args)
{
Test tt=new Test();
// Test
fun1 f1=new fun1(Test.w1);
// f1 fun1, w1
// , Test
tt.gg(f1);
//
fun1 f2;
f2=new fun1(tt.w2);
// f1 fun1, w2
// tt
tt.gg(f1);
//
Console.ReadLine();
} } }
fun1 , double double.
3. . . , .
namespace Deleg_2
{
delegate int Deleg(ref string st); //
class Class1
{
public static int met1(ref string x)
{
Console.WriteLine("I am Metod 1");
x+=" 11111";
return 5;
}
public static int met2(ref string y)
{
Console.WriteLine("I am Metod 2");
y+=" AAAAA";
return 55;
}
static void Main(string[] args)
{
Deleg d1;
int k;
string r="******";
Deleg d2=new Deleg(met1);
//
Deleg d3=new Deleg(met2);
d1=d2; //
d1+=d3; //
k=d1(ref r);
Console.WriteLine(r);
Console.WriteLine("k=" + k);
Console.ReadLine();
} } }
:
I am Metod 1
I am Metod 2
****** 11111 AAAAA
K=55
, , ( int), , ( met2).
d1-=d3; ( , d3).
: :
- , / .
- , .
, .
#: (event). . . , , . , . . , event. :
|
|
event _ ;
_ , , .
4:
namespace Event1
{
delegate void MyEvent(); //
class My
{ //
public event MyEvent activate; // activate
public void fire()
{
if(activate!=null)activate();
}
}
class Demo
{
static void handler()
{ //
Console.WriteLine("- ... ");
}
static void Main(string[] args)
{
My evt=new My();
evt.activate+=new MyEvent(handler);
// handler
evt.fire();
Console.ReadLine();
}
}
}
. , . , . My. , , MyEvent. , My fire(), , ( , , ). ,
if(activate!=null)activate();
, , activate null-. , , , , fire () . null-, , null-. Demo handler (). , , . Main() My,
handler () . , "+=". , "+=" " - = ".
. .
5.
namespace Events2
{
delegate void MyEvent();
// ,
class My
{
public event MyEvent activate; //
public void fire()
{
if (activate!=null) activate();
}
}
class X
{ //
public void Xhandler()
{
Console.WriteLine("I am X");
}
}
class Y
{
public void Yhandler()
{ //
Console.WriteLine("I am Y");
}
}
class Class1
|
|
{
static void handler()
{ // ,
Console.WriteLine("I am base");
}
static void Main(string[] args)
{
My evt =new My();
X x1= new X();
Y y1=new Y();
evt.activate+=new MyEvent(handler);
// , -
evt.activate+=new MyEvent(x1.Xhandler);
evt.activate+=new MyEvent(y1.Yhandler);
// ,
evt.fire();
Console.ReadLine();
} } }
. , - . : , . , . . . C# try catch finally.
try {
// , .
}
catch {__l exOb1) {
// __l.
}
catch (__2 exOb2) {
// __2.
}
catch {
//
}
finally {
//
}
try , . , catch finally. try catch. finally. catch . , . catch (). finally. catch finally .
. . . , : .
namespace Exception1
{
class Program
{
static void Main(string[] args)
{
int[] x ={4,64,128,256,516,1024,2048 };
int[] y ={2,0,4,0,6 };
for (int i=0;i<x.Length;i++)
try
{
Console.WriteLine(x[i] + " / " + y[i] + " = " + x[i] / y[i]);
}
catch (DivideByZeroException)
{
Console.WriteLine(" ");
}
catch (IndexOutOfRangeException)
{
Console.WriteLine(" ");
}
catch
// , exception
{
Console.WriteLine("- ");
}
finally //
{
Console.WriteLine(" ");
}
Console.WriteLine(" ");
Console.ReadLine();
} } }
:
: , try?
.
ArrayTypeMismatchException | |
DivideByZeroException | |
IndexOutOfRangeException | |
InvalidCastException | |
OutOfMemoryException | new - |
OverflowException | |
NullReferenceException | , .. , |
StackoverflowException |
|
|
NullReferenceException , , , . . null-, null.
1. .
2. - .
3. C#? ?
4. ? ?
5. ?
6. .
7. ? ?
8. ?
9. ?