Live Webinar: Prompt Engineering: Skill Everyone Must Learn Today
x
C# Corner
Tech
News
Videos
Forums
Jobs
Books
Events
More
Interviews
Live
Learn
Training
Career
Members
Blogs
Challenges
Certification
Contribute
Article
Blog
Video
Ebook
Interview Question
Collapse
Feed
Dashboard
Wallet
Learn
Achievements
Network
Refer
Rewards
SharpGPT
Premium
Contribute
Article
Blog
Video
Ebook
Interview Question
Register
Login
Generic Classes In C#
WhatsApp
Soumalya Das
Aug 26
2016
1.5
k
0
5
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#
Up Next
Generic Classes In C#