Looking for my RSS feed? Here it is!

C# 2.0 Self-Test

Thursday, December 1, 2005

In the .NET course I'm teaching (".NET Complete") I gave a little project to the class, of which all of them are new to OO design, C#, .NET, and all the related items. The project requirement was for them to study the example and see what it does. The point of the project was to allow them to test their knowledge of C# syntax, collections, generics, and nullable types

It's almost as useless as "Hello World". Or,rather, hello world is almost as useless as this.

You simply type in string and it tokenized it into characters...but they didn't know that.  Anyhow here you go...

using System;
using System.Collections.ObjectModel;
using System.Collections.Generic;

namespace CodeExamples
{
    class Program
    {
        class Storage<T> where T : ICollection<Char>, new( )
        {
            private Dictionary<String, T> list = new Dictionary<String, T>( );

            public void ShowLines( ){
                Int32? i;
                foreach(KeyValuePair<String, T> collection in list) {
                    i = null;
                    if(collection.Key.Length > 2) {
                        i = collection.Key.Length;
                    }

                    Console.Write((i ?? -1) + " : " + collection.Key + " : ");
                    foreach(Char thing in collection.Value) {
                        Console.Write(thing);
                    }


                    Console.WriteLine();
                }
            }

            public void Save(String text) {
                T parts = new T( );
                foreach(Char c in text.ToCharArray( )) {
                    parts.Add(c);
                }

                if(!list.ContainsKey(text)) {
                    list.Add(text, parts);
                }
            }
        }

        static void Main(String[] args) {
            Storage<Collection<Char>> storage = new Storage<Collection<Char>>( );

            Boolean done = false;

            while(!done) {
                String text = Console.ReadLine( );

                if(text == "end") {
                    done = true;
                    continue;
                }

                storage.Save(text);
            }

            storage.ShowLines( );
            Console.ReadLine( );
        }
    }
}

Comments (2)

Chakravarthy

"class Storage (T) where T : ICollection (char), new( )"


Newbie to C++ and C# here, can you please tell me what's happening in the above statement ? I can understand that Storage is a class which takes T as a generic parameter, but what is the "where ----, new()" part ? Hope you would be kind enough to explain.

Regards,

Chakravarthy

12/27/2005 8:55:00 AM

David Betz

With .NET generics you can add constraints as to what the incoming type can look like. This is saying that type T must implement ICollection(char) and must have a public default constructor.

12/27/2005 9:13:00 AM

Math Problem: 3 + 4 (type the answer in the box)

Notice: all comments are subject to moderation.

Comment saved. All comments are moderated and may not show up for some time.

Creative Commons License
This work is licensed under a Creative Commons Attribution 2.5 License.

Powered by the Minima Blog Engine, NetFXHarmonics Prominax, and Squid Micro-Blogging library.

Developed using NetFXHarmonics DevServer.

Mini-icons are part of the Silk Icons set of icons at famfamfam.com