implementing a Set class in Main Method

implementing a Set<T> class in Main Method



Currently, I'm learning about Hashing and its purposes and was given this piece of code so that I can see how the buckets and how the hashing happens. The only problem is, the book put the coding in a Set<T> class without telling me how to implement it in the Main()!


Set<T>


Main()



This is the given code


class Set<T>
{
private List<T> buckets = new List<T>[100];

public void Insert(T item)

int bucket = GetBucket(item.GetHashCode());
if (Contains(item, bucket))

return;


if (buckets[bucket] == null)

buckets[bucket] = new List<T>();


buckets[bucket].Add(item);



public bool Contains(T item)

return Contains(item, GetBucket(item.GetHashCode()));


private int GetBucket(int hashcode)

unchecked

return (int)((uint)hashcode%(uint)buckets.Length);



private bool Contains(T item, int bucket)

if (buckets[bucket] != null)

foreach (T member in buckets[bucket])

if (member.Equals(item))

return true;



return false;



I know how to create the normal instance of a class:


MyClass objectname = new MyClass();



but when I try to make an instance of Set<T> class (I know that its generic, by the way), the compiler says that T doesn't exist.


Set<T>


T



How on earth do I access Set<T> from my Main()?


Set<T>


Main()





Set<MyClass> mc = new Set<MyClass>();. Not really worth an answer.
– Patrick Hofman
Aug 28 at 9:18



Set<MyClass> mc = new Set<MyClass>();





I don't get the downvotes ... Just because someone is stuck on a simple problem, it does not mean the question is bad. Problem is clear, question too ... +1. Welcome on SO!
– nilsK
Aug 28 at 9:24





Then create a MyClass class.
– Patrick Hofman
Aug 28 at 9:44


MyClass





So what type should the elements in your set have?
– derpirscher
Aug 28 at 9:46





@diedomkop, please learn about Generics so you understand what you need to do: docs.microsoft.com/en-us/dotnet/csharp/programming-guide/…
– Aldert
Aug 28 at 10:32




1 Answer
1



Your problem actually comes from the understanding of Generics in C#.



A generic class is used to manipulate data that we don't know nothing about, but that we can manipulate to structure it.



Take a list for example. You can manipulate a list of string, integers, doubles, floats... That's why the List<T> class has been implemented in the .Net Framework. The point is to replace the T with the type you are planning to use at runtime.


List<T>



So if I know I want to use a list of integer, I will use List<int>.


List<int>



This is the same for your Set<T> class. T stands for "any type you want".


Set<T>



So if you want to use it, you have to do :


Set<int> MySetOfInteger = new Set<int>();
Set<String> MySetOfStrings = new Set<String>();



This actually generates two different classes, but based on the same template.



Hopes it clarifies a bit.






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Edmonton

Crossroads (UK TV series)