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?
3
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#.
0
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.
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.