Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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
4  
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
add comment

3 Answers 3

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
3  
+1 Head First Design Pattern was a revelation for me. –  marco-fiset Jan 30 '13 at 15:10
add comment

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
add comment

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)
{
    bar.doSomething();
}

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
add comment

Your Answer

 
discard

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.