Hi guys - I need your advice)
In which cases It'll be correct to use <T> after class name when I want to have generic type fileds in this class?
big thanks in advance)

Are you talking about generic method?
If the operations performed by several overloaded methods are identical for each argument type, the overloaded methods can be more compactly and conveniently coded using a generic method. You can write a single generic method declaration that can be called at different times with arguments of different types. Based on the types of the arguments passed to the generic method, the compiler handles each method call appropriately.

here is an example code:

static void Main( string[] args )
     {
         // create arrays of int, double and char
         int[] intArray = { 1, 2, 3, 4, 5, 6 };
         double[] doubleArray = { 1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7 };
         char[] charArray = { 'H', 'E', 'L', 'L', 'O' };
   
         Console.WriteLine( "Array intArray contains:" );
         PrintArray( intArray ); // pass an int array argument
         Console.WriteLine( "Array doubleArray contains:" );
         PrintArray( doubleArray ); // pass a double array argument
         Console.WriteLine( "Array charArray contains:" );
         PrintArray( charArray ); // pass a char array argument
      } // end Main
   
      // output array of all types
      static void PrintArray< E >( E[] inputArray )
      {
         foreach ( E element in inputArray )
            Console.Write( element + " " );
   
         Console.WriteLine( "\n" );
      } // end method PrintArray
  } // end class Generic method

Edited 5 Years Ago by Mitja Bonca: n/a

Comments
++++++

Mitja , if I have a generic fileds in class - for exml -

public class asd // 
        {
         public   T value;
         
        }

should I always use <T> in class declaration?

Edited 5 Years Ago by vedro-compota: n/a

Depends, if your return that type of not. If you do not return, then your method does not need to be a type of <T>, otherwise is has to be.
Example:

static List<T> GetInitializedList<T>(T value, int count)
    {
	// This generic method returns a List with ten elements initialized.
	// ... It uses a type parameter.
	// ... It uses the "open type" T.
	List<T> list = new List<T>();
	for (int i = 0; i < count; i++)
	{
	    list.Add(value);
	}
	return list;
    }

    static void Main()
    {
	// Use the generic method.
	// ... Specifying the type parameter is optional here.
	// ... Then print the results.
	List<bool> list1 = GetInitializedList(true, 5);
	List<string> list2 = GetInitializedList<string>("Perls", 3);
	foreach (bool value in list1)
	{
	    Console.WriteLine(value);
	}
	foreach (string value in list2)
	{
	    Console.WriteLine(value);
	}
    }

Edited 5 Years Ago by Mitja Bonca: n/a

Comments
Good explanation.

//...then your method does not need to be a type of <T>

not method ! )) for example - what is the difference between this -
1)

public class asd  
        {
         public   T value; 
        }

and this one -

public class asd[B]<T>[/B]  
        {
         public   T value; 
        }

Edited 5 Years Ago by vedro-compota: n/a

That you can return T. Becuase the method is a return type of <T>. If there is no specific return type in the method name, you are unable to return it.

In your 2nd example you can return T vlaue, when in your 1st can not.

why method...?...the valie - is a filed! may be -
if we not use <T> we'll can't so -

static void Main()
    {
asd.value = 123; // error here - Error	5	Cannot implicitly convert type 'int' to 'T'

    }

does it mean that we can initialize generics fields only by constructor using?

Momerath , hello)
but what should I do if in generic class code I need initialize an other generic class , which's type i don't know ?

for exml - will it be right -

public class LinkedListElement<T>
        {
            public LinkedListElement(T value)
            {
                this.Value = value;
            } 
            public T Value;
        }
public class OneClass<T>
{
  OneClass(T value) {
LinkedListElement[B]<T>[/B] u2 = new LinkedListElement[B]<T>[/B] (value)
}
}

Edited 5 Years Ago by vedro-compota: n/a

Yes, that's how you do it. You can see an example in my Permutation snippet, on line 51 where I create a List<T> without knowing what T is :)

Edited 5 Years Ago by Momerath: n/a

This question has already been answered. Start a new discussion instead.