?
Solved

Understandign some code

Posted on 2011-05-01
14
Medium Priority
?
348 Views
Last Modified: 2012-05-11

 I was reading one of the thread on this site only and I saw some unusual class definition which I was not able to understand that well. Can somebody please explain me what this definition mean?

 
public class Node<T> where T : class 
 {   
   public Node<T> Next { get; set; }   
  public T Value { get; set; }   
   public Node(T value)   
   {    Next = null;   Value = value;   
   }
  }

Open in new window



specially when it says  
public class Node<T> where T : class  

Open in new window

0
Comment
Question by:TheCommunicator
  • 4
  • 3
  • 2
  • +4
14 Comments
 
LVL 30

Accepted Solution

by:
anarki_jimbel earned 668 total points
ID: 35501643
See the link for more details:
http://msdn.microsoft.com/en-us/library/d5x73970(v=vs.80).aspx

The line basically tells that the node may contain ANY reference type: any class, interface, delegate, or array type. Value types are not included.

The article above also contains the answer why you may need restrictions on parameter types.

If you are not familiar with generics please read:

http://msdn.microsoft.com/en-us/library/ms379564(v=vs.80).aspx

0
 
LVL 11

Expert Comment

by:saragani
ID: 35501673
This is how you define a Generic class.
0
 
LVL 7

Assisted Solution

by:OklahomaDave
OklahomaDave earned 668 total points
ID: 35501679
TheCommunicator:

This structure introduces the concept of Generics, which were originally introduced in C++if I'm not mistaken. They extend the notion that certain sets of classes that only work on specific data types don't lend themselves very well to extension to other types. As as result, developers write what ends up being a bit of "boilerplate" code to implement the same basic functionality in different classes only because they had to write code to handle each possible type. You could use generic object types in some cases, but you then had to write code to ensure type safety among calling objects. Generics overcome this problem.

Here's an example. You could have a generic List that stores objects. Suppose, however, you needed a LIst that suported your own, more complex custom type. Without Generics, you would write a subclass of the List class and expose type-safe methods that only accepted your new custom type. That's eminently doable, but the concept of a Generic allows you simply to declare a type of List<YourCustomType>, with all the type-safety and functionality ready-to-go. That's a huge time saver!

I had a real-world situation where I wished I had generics during one of my first C-sharp projects. We were repeatedly writing collection objects with substantially identical functionality but for their types. We knew we were repeating code - and then we read about Generics. The first chance we had, we started writing generics-based code and it streamlined our process tremendously.

Good luck!
-David



0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:TheCommunicator
ID: 35501735
Ok I understand
where T : class 

Open in new window


Part but what does

Node<T>

Open in new window

mean?

I have seen List<Int> which means colllection of int types but node<T>? How can that be explained?
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 35501997
The "Next" reference member will point to another instance of the Node class having the same specified type.
0
 
LVL 30

Assisted Solution

by:anarki_jimbel
anarki_jimbel earned 668 total points
ID: 35502075
Obviously, node may contain objects of a reference type 'T' only, same as List may contain items of type T only.

I guess that the Node class represents some node in a data structure like a tree or graph etc.

In other words, when you create a Node you have to create it for a, say, some class Book (e.g.). Or class Employee. Or class Mummal. Etc.

The main idea of Generics is that you don't need to create specialized classes like EmployeeNode, DepartmentNode, GroupNode etc. To describe , e.g., a structure of your organization you may build different trees or graph using Generic Node class creating an instance for a specialized class.
0
 
LVL 8

Assisted Solution

by:ssnkumar
ssnkumar earned 332 total points
ID: 35502792
> Part but what does
>
> Node<T>
> mean?

Node is the name of the class and T is its type.
Actually T stands for Template.
So, while writing the class definition of Node, we want to make it work for all the types (or we don't know what type will come later).
When the object is instantiated, at that time a type will be passed.
0
 
LVL 15

Assisted Solution

by:Navneet
Navneet earned 332 total points
ID: 35503178
0
 

Author Comment

by:TheCommunicator
ID: 35506849
Thank you for the explanation guys, That was really helpful. One last question:

So List<int> is a collection which can only contain int types.
forget about T. If we strictly talk about node<String> then that means that class Node can be instantiated with only string obkects?
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 35506963
That's correct...anywhere you saw "T" before would now be replaced with "string" as if it had been hard-coded that way.
0
 
LVL 7

Assisted Solution

by:OklahomaDave
OklahomaDave earned 668 total points
ID: 35507021
You could, in a way, think of the term in the < > markers as a declarative parameter to the Generic, and the parameter is the type you want for that particular object.
0
 

Author Comment

by:TheCommunicator
ID: 35507099
So to conclude I have declared a generic lass then I can not instantiate class like other regular instantiation like:

Node SampleNode = new Node();

Open in new window


I HAVE TO:
Node<String> samplenode = new Node<String>():

Open in new window


would that be a correct statement?
0
 
LVL 7

Expert Comment

by:OklahomaDave
ID: 35507288
You've got it!
0
 

Author Comment

by:TheCommunicator
ID: 35507813
Perfect!! thanks guys.........
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
Simple Linear Regression

578 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question