using System;
using System.Collections.Generic;
using System.Linq;
class MainClass {
static void Main() {
Console.WriteLine("Hello World!");
GenericClass<string> gc1 = new GenericClass<string>();
gc1.AddData("Hello");
Console.WriteLine(gc1.GetData());
var pagedData = new Page<User>(new List<User>());
GenericMethodExample example = new GenericMethodExample();
example.Display<int>(100); // Outputs: 100
example.Display<string>("C#"); // Outputs: C#
// Usage
IGenericRepository<string> repo = new GenericRepository<string>();
repo.Add("Item1");
Console.WriteLine(repo.GetById(0)); // Outputs: Item1
}
}
public class User {}
public class Page<T>{
public IEnumerable<T> Data {get; set;}
public Page(IEnumerable<T> data){
Data = data;
}
}
public class GenericClass<T>{
private T _data;
public T GetData(){
return _data;
}
public void AddData(T data){
_data = data;
}
}
public class GenericMethodExample
{
public void Display<T>(T data)
{
Console.WriteLine(data);
}
}
using System.Collections.Generic;
public interface IGenericRepository<T>
{
void Add(T item);
T GetById(int id);
}
public class GenericRepository<T> : IGenericRepository<T>
{
private List<T> _items = new List<T>();
public void Add(T item)
{
_items.Add(item);
}
public T GetById(int id)
{
return _items[id];
}
}