Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am attempting to learn c# from the head first series, in addition i also consult other books such as Pro C# by Andrew Tolson.

Now the thing is that i perfectly understand the interface,generics syntax and can also use it for the programs given in the exercises.

What i find difficult is using these concepts into my own programs. For example the other day i was making a twitter account managing client and despite my best efforts i was unable to use any of the aforementioned concepts in code. I was even having difficulty in making useful classes.

What are some guidelines I can use to decide what classes I need to create for my program, assuming I know the syntax of how to create a class, but not the idea of when to do so?

share|improve this question
Are you writing unit tests for your code? Unit tests tend to provide a need for interfaces. – jk. Jan 30 '13 at 13:02
+1 for unit tests, this is where I really began to understand the need for interfaces in my own code. – stuartmclark Jan 30 '13 at 14:14
Question's title is misleading. Who said it was difficult? :D – Thomas Eding Jan 30 '13 at 18:52
up vote 8 down vote accepted

If you want to develop a sense for when to use interfaces, generics, and design patterns, then you should read 'Head First Design Patterns'.

It's a very approachable book for somebody at your level with a desire to learn more.

Please don't misunderstand me. Troelsen's book is good too. Just think of 'Head First Design Patterns' as "Level 2". Also, don't let the fact that the code samples are written in Java scare you away. The underlying concepts apply equally to Java and C#.

share|improve this answer
+1 Head First Design Pattern was a revelation for me. – marco-fiset Jan 30 '13 at 15:10

I think you are taking a natural progression that all programmers go through. Explanation and examples of programming concepts are almost always presented in the simplest context possible. The goal is to focus on the concept and not get distracted by a lot of other concepts. Ever notice that many examples don't include error-handline? You get a try/catch at most.

The next step is to do something more complicated. Let's use your Twitter client as an example. You built it and it works and you see no 'need' for any of this. Very common. Now, see if you can make this same application handle managing a Facebook, Evernote or some other online service account. What parts are difficult to change? How do you avoid code duplication? Try and research some of the concepts you mentioned and see if you can find out "why" you would use them.

The next step, try and add another programmer to your project.

share|improve this answer

Think of the English meaning of interface. Both C#-interfaces and generics provide the English meaning of interface. The concept of interface goes beyond it's typical use in OOP style code.

Generics are used to acheived polymorphic behavior on the syntax of the language. For example

void foo<T>(T bar)
    //type T must implement the "+" interface
    bar + baz; 

The "+" operation is built into the syntax of the language. Type T implements the "+" interface (the English meaning). In effect polymorphic behavior is achieved. Internally the "+" operation is very different for integers and decimals, but the foo method handles both! You don't need to care how "+" is implemented. You just care if the type implements the "+" interface.

C# interfaces are used to achieve polymorphic behavior on defined methods (rather than source code level syntax).

void foo(IBar bar)

There may be several types which implement IBar. They each doSomething() very differently. Much the same as with generics you dont' care how they doSomething(). You only care that they implement the interface. The difference is the "interface" is a contract of defined methods rather than source code syntax.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.