C# Template method does not work same as non-template method, why?

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
Hi !namespace Sandbox
{
class A
{
public A(string name)
{
mName = name;
}

public A(A _a)
{
mName = _a.Name;
}

public string Name
{
get
{
return mName;
}
set
{
mName = value;
}
}
private string mName;
}

class B : ISearchable< B > {
private string mId;
private List<B> mChildren;

public B( string _id )
{
mId = _id;
mChildren = new List<B>();
}

public void add( B _child )
{
mChildren.Add( _child );
}

public string Id { get { return mId; } }

public List< B > Children { get { return mChildren; } }
}

class Program
{
//public static B search(List<B> _items, string _searchPath)
//{
// string[] strSeprator = { "#" };
// string[] pathTokens = _searchPath.Split(strSeprator, StringSplitOptions.RemoveEmptyEntries);
// int pathLen = pathTokens.Length;
// int pathTokenCtr = 0;
// // Traverse current level
// List<B> items = _items;
// for (int i = 0; i < items.Count; i++)
// {
// Console.WriteLine("1. {0} == {1} = {2}", pathTokens[pathTokenCtr], _items.Id, pathTokens[pathTokenCtr] == _items.Id);
// Console.WriteLine("2. {0} == {1} = {2}", pathTokens[pathTokenCtr], items.Id, pathTokens[pathTokenCtr] == items.Id);
// // Is current item id matching to current path token.
// if (pathTokens[pathTokenCtr] == items.Id)
// {
// // Are we at last token in the path?
// if (pathTokenCtr == pathLen - 1)
// {
// return items;
// }
// // Step into next level!
// items = items.Children;
// // take next token
// pathTokenCtr++;
// // reset items counter to 0
// i = -1;
// continue;
// }
// }
// // Oops ! Did not find it!
// return null;
//}


public static T search<T>(ref List<T> _items, string _searchPath) where T : ISearchable<T>
{
string[] strSeprator = { "#" };
string[] pathTokens = _searchPath.Split(strSeprator, StringSplitOptions.RemoveEmptyEntries);
int pathLen = pathTokens.Length;
int pathTokenCtr = 0;
// Traverse current level
List<T> items = _items;
for (int i = 0; i < items.Count; i++)
{
Console.WriteLine("1. {0} == {1} = {2}", pathTokens[pathTokenCtr], _items.Id, pathTokens[pathTokenCtr] == _items.Id);
Console.WriteLine("2. {0} == {1} = {2}", pathTokens[pathTokenCtr], items.Id, pathTokens[pathTokenCtr] == items.Id);
// Is current item id matching to current path token.
if (pathTokens[pathTokenCtr] == items.Id)
{
// Are we at last token in the path?
if (pathTokenCtr == pathLen - 1)
{
return items;
}
// Step into next level!
items = items.Children;
// take next token
pathTokenCtr++;
// reset items counter to 0
i = -1;
continue;
}
}
// Oops ! Did not find it!
return null;
}

static void Main(string[] args)
{
B root = new B( "root" );
root.add( new B("Item1" ));
root.add( new B("Item2" ));
root.add( new B("Item3" ));
root.add( new B("Item4" ));

root.Children[2].add( new B("Item3_1"));
root.Children[2].add( new B("Item3_2"));
root.Children[2].add( new B("Item3_3"));

root.Children[2].Children[1].add( new B("Item3_2_1"));
root.Children[2].Children[1].add( new B("Item3_2_2"));
root.Children[2].Children[1].add( new B("Item3_2_3"));

string searchPath = "Item3#Item3_2#Item3_2_2"; //root\
List<B> tempRoot = root.Children;
B temp = search<B>( ref tempRoot, searchPath);
//B temp = search(root.Children, searchPath);
if (temp == null)
{
Console.WriteLine("Oops ! NOT Found ");
Console.ReadKey();
}
else
{
Console.WriteLine("Found = {0}", temp.Id);
Console.ReadKey();
}


//string[] strSeprator = {"\"};
//string[] strArrayFullPath = searchPath.Split(strSeprator,StringSplitOptions.RemoveEmptyEntries);
//int iselect = 0;
//if ( strArrayFullPath[iselect] == root.Id ) {
// if ( iselect == strArrayFullPath.Length - 1 ) {
// Console.WriteLine( "Found = {0}", root.Id );
// Console.ReadKey();
// return;
// }
//}
//++iselect;
//List<B> listTransComp = root.Children;
//for ( int i = 0; i < listTransComp.Count; i++ )
//{
// if (strArrayFullPath[iselect] == listTransComp.Id)
// {
// if (iselect == strArrayFullPath.Length - 1) {
// Console.WriteLine("Found = {0}", listTransComp.Id);
// Console.ReadKey();
// return;
// }
// listTransComp = listTransComp.Children;
// iselect++;
// i = -1;
// continue;
// }
//}
//Console.WriteLine( "Oops ! NOT Found " );
//Console.ReadKey();
return;
}
}
}

In above code you will find a template function, a non template function both doing same thing. but output is not the same. Why?
Please explain!
Thank you.
Rupesh

View the full article
 
Back
Top