TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
C# Corner
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Generic Classes In C#
Soumalya Das
Aug 26
2016
Code
1.5
k
0
5
facebook
twitter
linkedIn
Reddit
WhatsApp
Email
Bookmark
expand
namespace
Consolepractice
{
class
Program {
public
static
void
Main(
string
[] args) {
// Use the generic type Demo with an int type parameter.
Demo <
int
> test1 =
new
Demo <
int
> (2);
// Call the Write method.
test1.print();
// Use the generic Demo with a string type parameter.
Demo <
string
> test2 =
new
Demo <
string
> (
"soumalya das"
);
test2.print();
// Use the generic Demo with a float type parameter.
Demo <
float
> test3 =
new
Demo <
float
> (4.5 f);
test3.print();
Console.ReadKey();
}
}
class
Demo < T > {
T _value;
public
Demo(T t) {
// The field has the same type as the parameter.
this
._value = t;
}
public
void
print() {
Console.WriteLine(
this
._value);
}
}
}
Generic Classes
C#