.


:




:

































 

 

 

 


Gen , where. .




class GenCT, V> where T: class

where V: struct {

, where . .

,

, . , , , . null, 0 false, bool, struct , . : : null, 0 ? , Test:

class Test<T> {

obj;

...

obj ,

obj = null; //

obj =0; // // ,

? default, .

default ()

default , .

,

Default.

// default.

Using System;

class MyClass {

//...

}

// , , class Test<T> { public obj;

public Test() {

// .

// obj = null; //

// .

// obj = 0; //

II ,

// , obj = default(); // !

}

II...

}

class DefaultDemo { static void Main() {

// Test, .

Test<MyClass> = new Test<MyClass> (); 11

if(x.obj == null)

Console.WriteLine(" x.obj <null>.");

// Test, .

Test<int> = new Test<int>();

if(.obj == 0)

Console.WriteLine(" .obj 0.");

}

}

.

x.obj <null>.

.obj 0.

C# . , . , XY X, Y.

// , using System;

// , struct XY<T> {

;

;

public XY( , ) { = ;

= ;

}

public X {

get { return ; } set { = value; }

}

public T Y {

get { return y; } set { = value; }

}

}

class StructTest { static void Main() {

XY<int> xy = new XY<int>(10, 20);

XY<double> xy2 = new XY<double>(88.0, 99.0);

Console.WriteLine(xy.X + ", " + xy.Y);

Console.WriteLine(xy2.X + ", " + xy2.Y);

}

}

.

10, 20 88, 99

, . , XY .

struct XY<T> where : struct {

//...

, , , , , . , .

. ArrayUtils, Copy Insert (). , . CopylnsertO .

// , using System;

// . , class ArrayUtils {

// , .

// .

public static bool CopyInsert<T> ( e, uint idx,

T[] src, T[] target) {

// , , if(target.Length < src.Length+1) return false;

// src ,

// - idx. for(int i=0, j=0; i < src.Length; i++, j++) { if(i == idx) { target[j] = e; j++;

}

target[j] = src[i];

}

Return true;

}

}

class GenMethDemo { static void Main() {

int[] nums = { 1, 2, 3 }; int[] nums2 = new int [4];

// nums.

Console.Write(" nums: "); foreach(int x in nums)

Console.Write( + " ");

Console.WriteLine();

// int.





:


: 2016-12-03; !; : 498 |


:

:

: , , , , .
==> ...

1285 - | 1186 -


© 2015-2024 lektsii.org - -

: 0.014 .