C# Generics: Digesting Tim Corey’s C# Generics Video

Digesting Tim Corey’s C# Generics Video

I’ve just compled a learning session on C# Generics using Tim Corey’s YouTube video on C# Generics as my primer.

Below are helpful links to get you started so you can follow along with the video as well as my learning notes below to help you get the basics of C# Generics.

Tim Corey’s C# Generics Video

You can get all the files to go along with Tim Corey’s C# Generics video at Time’s web site for the cost of providing Tim with your email address for his email list. You can unsubscribe anytime.

Below is Corey’s Video. Watch it, then continue if you’re still interested. My personal training notes will be shared below. This information might help you if you’re feeling stuck or want or want to make sure you understand C# Generics.

What are C# Generics

C# generics are a feature of the C# programming language that allow you to create classes, interfaces, methods, or delegates that operate on specified data types without committing to a particular data type at compile time. Generics provide a way to write reusable and type-safe code by enabling you to parameterize your code with one or more type parameters.

Here are some key points about C# generics:

  1. Type Safety: Generics ensure type safety by allowing you to work with strongly typed data. This means that you can catch type-related errors at compile time rather than runtime.
  2. Reusability: With generics, you can create classes or methods that can work with different data types. This promotes code reusability and reduces the need to duplicate code for each specific data type.
  3. Code Efficiency: Generics eliminate the need for casting data types, making code more efficient and readable.
  4. Collections: Generic collections, such as List<T>, Dictionary<TKey, TValue>, and Queue<T>, are widely used in C# to store and manipulate data of various types in a type-safe manner.
  5. Constraints: You can apply constraints on generic type parameters to specify that they must implement certain interfaces, inherit from specific classes, or have default constructors, which further restrict the types that can be used with your generic code.

Here’s a simple example of a generic method in C#:

public T FindMax<T>(T[] array) where T : IComparable<T>
{
    if (array.Length == 0)
    {
        throw new ArgumentException("The array is empty.");
    }
    T max = array[0];
    foreach (T item in array)
    {
        if (item.CompareTo(max) > 0)
        {
            max = item;
        }
    }

    return max;
}

In this example, T is a generic type parameter that allows you to find the maximum value in an array of any data type that implements the IComparable<T> interface. This method can be used with various types, making it highly reusable and type-safe.

How to Get Started Programming in Python / Python Crash Course

Crash Course in to Python Programming

I had really been wanting to find a couple of hours to do a crash course on python and finally found an opportunity to do it last night.

Having been a programmer for a while, I’m finding many of the Python videos quite boring as they are for absolute beginners which has you plodding through the basics of programming which makes learning a new programming language painfully slow.

Luckily, I had found this great Python primer video by Derek Banas on YouTube.

I followed along and completed all the code in the video and had working examples of most of the important code snippets for much of how stuff gets done in Python.  Success was not without some pain, see problems and  resolutions below.

Video: Learn Python in One Video

I will save you some pain by telling you there is a very frustrating code issue at the end of this long video.

See code fix below.

When you are in the Dog class referencing the Animal super class from which it inherits the name, weight, height and sound, properties or values you have to use the setter and getter class methods instead of the object “self” reference like we could in the Animal object.  Once you change that, the code works.

For the Dog class, the toString() method only worked for me the example below:

def toString(self):
        return "{} is {} cm tall and {} kilograms, says {} and owner is {}".format(
                                                                    self.get_name(),
                                                                    self.get_height(),
                                                                    self.get_weight(),
                                                                    self.get_sound(),
                                                                    self.__owner)
...instead of:
(this doesn't)
def toString(self):
        return "{} is {} cm tall and {} kilograms, says {} and owner is {}".format(
                                                                    self.__name,
                                                                    self.__height,
                                                                    self.__weight,
                                                                    self.__sound,
                                                                    self.__owner)

As a bonus I would add watching this video to help solidify the idea of polymorphism if you’re having difficulty getting a grasp of it.

Hope this helps someone!

Video: What is Polymorphism?