C# - Trying to learn it, hit the wall!

So i was going to try out diffrent ways to implement search algorithms... but instead to just do that, i got the idea!
To remind me self how generics works and learn more about the subject.
Did this:

using System;

namespace linearysearch
{
static class Search
{
static public void Linear(T[] store, T Key)
{
for (int i = 0; i < store.Length; i++)
{
if (store[i] == Key)
{
Console.WriteLine("\tQQQQ you where searching for was: {0} \n\tand was found in element: {1}.", Key, i);
}
}
}
}

class Program
{
    static void Main(string[] args)
    {
        int Key = 2;
        int[] store = new int[] { 9, 8, 1, 99, 33, 2 };
        Search.Linear<int>(store, Key);

        //string Key = "tomas";
        //string[] store = new string[] { "adam", "tomas", "erika", "greger" };
        //Search.Linear<string, string>(store, Key);
    }
}

}

And got stucked, search the internets! Found this thread:

That explain why it dosent work, i think(having some problems understanding it), but i cant come up with a solution for my problem. Thats why iam here, hope some one can help me out!

And one more thing, is it possible to make the "store" array name generic too. Thats was my next problem...

Best regards Tomas!

PS Sorry for my bad english, working on that too! :D

Can you restrict it to static public void Linear(T[] store, T key) where T : IEquatable<T> ?

Hello sir, now iam back at my computer...
Iam gonna try that out!: "static public void Linear(T[] store, T key) where T : IEquatable ?"

Havent seen that before actually: "where T : IEquatable " ,so that dose what?

oh min after i replyed to you, i found this:
http://www.dotnetperls.com/generic

Then i saw: "Constraints"

Thanks mate! Brb reading..

You can constrain based on classes, interfaces, (as above)
reference or value types where T : struct or where T : class
and a zero-argument constructor. where T : new()

this last one is somewhat unusual for languages, but a really nifty feature if you want to create template factories, especially noteworthy being a Singleton wrapper class.

Keep in mind though, the class you instantiate the generic with does have to conform to ALL the constraints. It's a logical AND, not OR. But knowing ahead about the allowable types is exactly what allows you to access the parameter class's members.

Hello again!

Ended up doing it like this, hope youre not disappointed hehe

static public void LinearSearch(T[] storage, string keyWord)/* where T : IComparable*/
{
string strElement;
for (int i = 0; i < storage.Length; i++)
{
strElement = Convert.ToString(storage[i]);
if (strElement == keyWord)
{
Console.WriteLine("Sökordet: {0}, hittades i element {1}", keyWord, i);
Console.ReadKey();
}
}

Thats good to know, what you wright in the last post(...Singleton...).
The thing is, as my program is right now, the class is static, think ive to rewrite it to be able to use constraints.

EDIT and having the arrays iam searching in, in the "static void Main(string[] args)" and not up in the class...

As fare as i understand, but i think ill will be happy with this and move on.

Best regards Tomas
}

PS about singleton:

You might run into problems with using == operator when comparing strings. Since the strings are actually not the same string objects but rather two identical objects, and == does a object reference test wether they are the same object.
Also == doesn't do a instaceof, or typeof() or what ever it's called in C#, anywho it doesn't test wether the objects are even of the same kind.
Try using str1.equals(str2); instead, the .equals does a value comparrison rather then object reference comparrison.
basically .equals() does a typeof() check and if not of the same object type, returns false, after that it does return a=b.
== usually is used for testing wether primitive types are compareable e.g. 1==1 or such.

1 Like

hmm okey thanks! .Equals its!
This is printed in my head now: "== usually is used for testing wether primitive types are compareable"

Best regards!

Operator overloading... It exists.

there is no real point in converting them to strings for a comparison, that is nothing more than wasting processor time.

If the objects have ANY sort of inherent equality trait other than what memory address they are stored in, they should have implemented IEquatable, where T is their own class.

Even if they do not, and you are relying on memory addresses (ie class System.Object), it still has two methods,

public virtual Boolean Object.Equals(Object other);
public static Boolean Object.Equals(Object a, Object b);

1 Like