<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="ru">
		<id>http://www.nfex.ru/index.php?action=history&amp;feed=atom&amp;title=Csharp%2FC_Sharp%2FGenerics%2FGeneric_Class</id>
		<title>Csharp/C Sharp/Generics/Generic Class - История изменений</title>
		<link rel="self" type="application/atom+xml" href="http://www.nfex.ru/index.php?action=history&amp;feed=atom&amp;title=Csharp%2FC_Sharp%2FGenerics%2FGeneric_Class"/>
		<link rel="alternate" type="text/html" href="http://www.nfex.ru/index.php?title=Csharp/C_Sharp/Generics/Generic_Class&amp;action=history"/>
		<updated>2026-04-16T12:31:14Z</updated>
		<subtitle>История изменений этой страницы в вики</subtitle>
		<generator>MediaWiki 1.30.0</generator>

	<entry>
		<id>http://www.nfex.ru/index.php?title=Csharp/C_Sharp/Generics/Generic_Class&amp;diff=1376&amp;oldid=prev</id>
		<title> в 15:31, 26 мая 2010</title>
		<link rel="alternate" type="text/html" href="http://www.nfex.ru/index.php?title=Csharp/C_Sharp/Generics/Generic_Class&amp;diff=1376&amp;oldid=prev"/>
				<updated>2010-05-26T15:31:19Z</updated>
		
		<summary type="html">&lt;p&gt;&lt;/p&gt;
&lt;table class=&quot;diff diff-contentalign-left&quot; data-mw=&quot;interface&quot;&gt;
				&lt;tr style=&quot;vertical-align: top;&quot; lang=&quot;ru&quot;&gt;
				&lt;td colspan=&quot;1&quot; style=&quot;background-color: white; color:black; text-align: center;&quot;&gt;← Предыдущая&lt;/td&gt;
				&lt;td colspan=&quot;1&quot; style=&quot;background-color: white; color:black; text-align: center;&quot;&gt;Версия 15:31, 26 мая 2010&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; style=&quot;text-align: center;&quot; lang=&quot;ru&quot;&gt;&lt;div class=&quot;mw-diff-empty&quot;&gt;(нет различий)&lt;/div&gt;
&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;</summary>
			</entry>

	<entry>
		<id>http://www.nfex.ru/index.php?title=Csharp/C_Sharp/Generics/Generic_Class&amp;diff=1377&amp;oldid=prev</id>
		<title>Admin: 1 версия</title>
		<link rel="alternate" type="text/html" href="http://www.nfex.ru/index.php?title=Csharp/C_Sharp/Generics/Generic_Class&amp;diff=1377&amp;oldid=prev"/>
				<updated>2010-05-26T11:45:50Z</updated>
		
		<summary type="html">&lt;p&gt;1 версия&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Новая страница&lt;/b&gt;&lt;/p&gt;&lt;div&gt;==A Generic Class with Two Type Parameters==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
class MyGenericClass&amp;lt;T, V&amp;gt; {&lt;br /&gt;
  T ob1;&lt;br /&gt;
  V ob2;&lt;br /&gt;
  public MyGenericClass(T o1, V o2) {&lt;br /&gt;
    ob1 = o1;&lt;br /&gt;
    ob2 = o2;&lt;br /&gt;
  }&lt;br /&gt;
  public void showTypes() {&lt;br /&gt;
    Console.WriteLine(&amp;quot;Type of T is &amp;quot; + typeof(T));&lt;br /&gt;
    Console.WriteLine(&amp;quot;Type of V is &amp;quot; + typeof(V));&lt;br /&gt;
  }&lt;br /&gt;
  public T getob1() {&lt;br /&gt;
    return ob1;&lt;br /&gt;
  }&lt;br /&gt;
  public V getob2() {&lt;br /&gt;
    return ob2;&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
public class Test {&lt;br /&gt;
  public static void Main() {&lt;br /&gt;
    MyGenericClass&amp;lt;int, string&amp;gt; obj = new MyGenericClass&amp;lt;int, string&amp;gt;(1, &amp;quot;string&amp;quot;);&lt;br /&gt;
    obj.showTypes();&lt;br /&gt;
    int v = obj.getob1();&lt;br /&gt;
    Console.WriteLine(&amp;quot;value: &amp;quot; + v);&lt;br /&gt;
    string str = obj.getob2();&lt;br /&gt;
    Console.WriteLine(&amp;quot;value: &amp;quot; + str);&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
           &lt;br /&gt;
       &amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==A simple generic class==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
class MyGenericClass&amp;lt;T&amp;gt; {&lt;br /&gt;
  T ob;&lt;br /&gt;
  public MyGenericClass(T o) {&lt;br /&gt;
    ob = o;&lt;br /&gt;
  }&lt;br /&gt;
  public T getob() {&lt;br /&gt;
    return ob;&lt;br /&gt;
  }&lt;br /&gt;
  public void showType() {&lt;br /&gt;
    Console.WriteLine(&amp;quot;Type of T is &amp;quot; + typeof(T));&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
public class Test {&lt;br /&gt;
  public static void Main() {&lt;br /&gt;
    MyGenericClass&amp;lt;int&amp;gt; iOb;&lt;br /&gt;
    iOb = new MyGenericClass&amp;lt;int&amp;gt;(102);&lt;br /&gt;
    iOb.showType();&lt;br /&gt;
    int v = iOb.getob();&lt;br /&gt;
    Console.WriteLine(&amp;quot;value: &amp;quot; + v);&lt;br /&gt;
    MyGenericClass&amp;lt;string&amp;gt; strOb = new MyGenericClass&amp;lt;string&amp;gt;(&amp;quot;Generics add power.&amp;quot;);&lt;br /&gt;
    strOb.showType();&lt;br /&gt;
    &lt;br /&gt;
    string str = strOb.getob();&lt;br /&gt;
    Console.WriteLine(&amp;quot;value: &amp;quot; + str);&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
           &lt;br /&gt;
       &amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Comparing Instances of a Type Parameter==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
class MyClass : IComparable {&lt;br /&gt;
  public int val;&lt;br /&gt;
  public MyClass(int x) { &lt;br /&gt;
    val = x; &lt;br /&gt;
  }&lt;br /&gt;
  public int CompareTo(object obj) {&lt;br /&gt;
    return val - ((MyClass) obj).val;&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
class CompareDemo {&lt;br /&gt;
  public static bool contains&amp;lt;T&amp;gt;(T what, T[] obs) where T : IComparable {&lt;br /&gt;
    foreach(T v in obs)&lt;br /&gt;
      if(v.rupareTo(what) == 0)&lt;br /&gt;
        return true;&lt;br /&gt;
    return false;&lt;br /&gt;
  }&lt;br /&gt;
  public static void Main() {&lt;br /&gt;
    int[] nums = { 1, 2, 3, 4, 5 };&lt;br /&gt;
    if(contains(2, nums))&lt;br /&gt;
      Console.WriteLine(&amp;quot;2 is found.&amp;quot;);&lt;br /&gt;
    string[] strs = { &amp;quot;one&amp;quot;, &amp;quot;two&amp;quot;, &amp;quot;three&amp;quot;};&lt;br /&gt;
    if(contains(&amp;quot;two&amp;quot;, strs))&lt;br /&gt;
      Console.WriteLine(&amp;quot;two is found.&amp;quot;);&lt;br /&gt;
    MyClass[] mcs = { new MyClass(1), new MyClass(2),&lt;br /&gt;
                      new MyClass(3), new MyClass(4) };&lt;br /&gt;
    if(contains(new MyClass(3), mcs))&lt;br /&gt;
      Console.WriteLine(&amp;quot;MyClass(3) is found.&amp;quot;);&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
           &lt;br /&gt;
       &amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Create relationship between two type parameters==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
class A {&lt;br /&gt;
}&lt;br /&gt;
class B : A {&lt;br /&gt;
}&lt;br /&gt;
// Here, V must inherit T.&lt;br /&gt;
class Gen&amp;lt;T, V&amp;gt; where V : T {&lt;br /&gt;
}&lt;br /&gt;
class Test {&lt;br /&gt;
  public static void Main() {&lt;br /&gt;
    // This declaration is OK because B inherits A.&lt;br /&gt;
    Gen&amp;lt;A, B&amp;gt; x = new Gen&amp;lt;A, B&amp;gt;();&lt;br /&gt;
    // This declaration is in error because&lt;br /&gt;
    // A does not inherit B.&lt;br /&gt;
//    Gen&amp;lt;B, A&amp;gt; y = new Gen&amp;lt;B, A&amp;gt;();&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
           &lt;br /&gt;
       &amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Demonstrate the default keyword==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
class MyClass {&lt;br /&gt;
}&lt;br /&gt;
class Test&amp;lt;T&amp;gt; {&lt;br /&gt;
  public T obj;&lt;br /&gt;
  public Test() {&lt;br /&gt;
    obj = default(T);&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
class DefaultDemo {&lt;br /&gt;
  public static void Main() {&lt;br /&gt;
    Test&amp;lt;MyClass&amp;gt; x = new Test&amp;lt;MyClass&amp;gt;();&lt;br /&gt;
    if(x.obj == null)&lt;br /&gt;
      Console.WriteLine(&amp;quot;x.obj is null.&amp;quot;);&lt;br /&gt;
    Test&amp;lt;int&amp;gt; y = new Test&amp;lt;int&amp;gt;();&lt;br /&gt;
    if(y.obj == 0)&lt;br /&gt;
      Console.WriteLine(&amp;quot;y.obj is 0.&amp;quot;);&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
           &lt;br /&gt;
       &amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Generic class with interface==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
using System.Collections.Generic;&lt;br /&gt;
public interface IShape&lt;br /&gt;
{&lt;br /&gt;
    double Area {&lt;br /&gt;
        get;&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
public class Circle : IShape&lt;br /&gt;
{&lt;br /&gt;
    public Circle( double radius ) {&lt;br /&gt;
        this.radius = radius;&lt;br /&gt;
    }&lt;br /&gt;
    public double Area {&lt;br /&gt;
        get {&lt;br /&gt;
            return 3.14 * radius * radius;&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
    private double radius;&lt;br /&gt;
}&lt;br /&gt;
public class Rect : IShape&lt;br /&gt;
{&lt;br /&gt;
    public Rect( double width, double height ) {&lt;br /&gt;
        this.width = width;&lt;br /&gt;
        this.height = height;&lt;br /&gt;
    }&lt;br /&gt;
    public double Area {&lt;br /&gt;
        get {&lt;br /&gt;
            return width*height;&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
    private double width;&lt;br /&gt;
    private double height;&lt;br /&gt;
}&lt;br /&gt;
public class Shapes&amp;lt;T&amp;gt;&lt;br /&gt;
    where T: IShape&lt;br /&gt;
{&lt;br /&gt;
    public double TotalArea {&lt;br /&gt;
        get {&lt;br /&gt;
            double acc = 0;&lt;br /&gt;
            foreach( T shape in shapes ) {&lt;br /&gt;
                acc += shape.Area;&lt;br /&gt;
            }&lt;br /&gt;
            return acc;&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
    public void Add( T shape ) {&lt;br /&gt;
        shapes.Add( shape );&lt;br /&gt;
    }&lt;br /&gt;
    private List&amp;lt;T&amp;gt; shapes = new List&amp;lt;T&amp;gt;();&lt;br /&gt;
}&lt;br /&gt;
public class Test&lt;br /&gt;
{&lt;br /&gt;
    static void Main() {&lt;br /&gt;
        Shapes&amp;lt;IShape&amp;gt; shapes = new Shapes&amp;lt;IShape&amp;gt;();&lt;br /&gt;
        shapes.Add( new Circle(3) );&lt;br /&gt;
        shapes.Add( new Rect(7, 5) );&lt;br /&gt;
        Console.WriteLine( &amp;quot;Total Area: {0}&amp;quot;, shapes.TotalArea );&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
           &lt;br /&gt;
       &amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Generic Fields==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;csharp&amp;quot;&amp;gt; &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
using System;&lt;br /&gt;
using System.Collections.Generic;&lt;br /&gt;
using System.Text;&lt;br /&gt;
class MyType&amp;lt;T, U&amp;gt; {&lt;br /&gt;
    private T _myFirstDataMember;&lt;br /&gt;
    private U _mySecondDataMember;&lt;br /&gt;
    public MyType(T val1, U val2) {&lt;br /&gt;
        this._myFirstDataMember = val1;&lt;br /&gt;
        this._mySecondDataMember = val2;&lt;br /&gt;
    }&lt;br /&gt;
    public T GetFirstDataMember() {&lt;br /&gt;
        return this._myFirstDataMember;&lt;br /&gt;
    }&lt;br /&gt;
    public U GetSecondDataMember() {&lt;br /&gt;
        return this._mySecondDataMember;&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
class MyApp {&lt;br /&gt;
    static void main(String[] args) {&lt;br /&gt;
        MyType&amp;lt;string, string&amp;gt; testType = new MyType&amp;lt;string, string&amp;gt;(&amp;quot;Val1&amp;quot;, &amp;quot;Val2&amp;quot;);&lt;br /&gt;
        Console.WriteLine(testType.GetFirstDataMember());&lt;br /&gt;
        Console.WriteLine(testType.GetSecondDataMember());&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
  &lt;br /&gt;
       &amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==&amp;quot;This&amp;quot; Reference for Generic Types==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
    using System;&lt;br /&gt;
    public class Starter{&lt;br /&gt;
        public static void Main(){&lt;br /&gt;
            MyGenericClass&amp;lt;int&amp;gt; obj=new MyGenericClass&amp;lt;int&amp;gt;();&lt;br /&gt;
            obj.MethodA();&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    class MyGenericClass&amp;lt;T&amp;gt; {&lt;br /&gt;
        public T MethodA() {&lt;br /&gt;
           T var=default(T);&lt;br /&gt;
           Console.WriteLine(this.GetType().ToString());&lt;br /&gt;
           return var;&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
           &lt;br /&gt;
       &amp;lt;/source&amp;gt;&lt;/div&gt;</summary>
		<author><name>Admin</name></author>	</entry>

	</feed>