orangey
orangey

Reputation: 55

Type confusion: A type is class, a variable and an object?

I'm currently working through the Pluralsight C# 5.0 course, and I'm relatively new to programming.

I previously thought I understood the concept of Data Types on a basic level, Int/Array/Strings etc. In this course it starts to introduce C#'s huge emphasis on Types, and creating your own custom types.

One of the course code snippets which I've included below, is refusing to sink in and I was hoping someone could provide some clarity or a different way of thinking about it.

Program.cs:

GradeStatistics stats = book.ComputeStatistics();

GradeStatistics.cs:

namespace Grades
    {
        public class GradeStatistics
        {
            public GradeStatistics()
            {
                HighestGrade = 0;
                LowestGrade = float.MaxValue;
            }

            public float AverageGrade;
            public float HighestGrade;
            public float LowestGrade;
        }
    }

Grades:

public GradeStatistics ComputeStatistics()
        {
            GradeStatistics stats = new GradeStatistics();

            float sum = 0f;
            foreach (float grade in grades)
            {
                stats.HighestGrade = Math.Max(grade, stats.HighestGrade);
                stats.LowestGrade = Math.Min(grade, stats.LowestGrade);
                sum += grade;
            }

            stats.AverageGrade = sum / grades.Count;
            return stats;
        }

I'm finding it particularly difficult to understand what exactly GradeStatistics is. In the course it is referred to as not only a class, but as a variable, and furthermore also being returned as an object in Grades.

Any clarity is appreciated, as I'm finding it a little difficult to follow with all of the above terms being thrown around.

Upvotes: 5

Views: 358

Answers (4)

Liam McInroy
Liam McInroy

Reputation: 4366

GradeStatistics is a class. From this snippet:

    public class GradeStatistics
    {
        public GradeStatistics()
        {
            HighestGrade = 0;
            LowestGrade = float.MaxValue;
        }

        public float AverageGrade;
        public float HighestGrade;
        public float LowestGrade;
    }

However, in the method ComputeStatistics, the return type is GradeStatistics, not void as you are probably accustomed to. This means that ComputeStatistics returns a GradeStatistics. Lets look at an easier example.

Suppose we have a class Foo. It is constructed like:

public class Foo
{
    public int a;
    public int b;

    public Foo()
    {
        a = 0;
        b = 0;
    }
}

Now we can create Foos and access them normally.

Foo foo = new Foo();
foo.a = 4;
foo.b /= 2;

Now suppose we want to do this, except many times, so we create a method.

public Foo IncrementFoo(int amount, int divideBy)
{
    Foo obj = new Foo();
    obj.a = amount;
    obj.b /= divideBy;
    return obj;
}

This method creates a new object of type Foo, and returns it. You can then use this code like:

Foo newFoo = IncrementFoo(4, 2);
//foo == newFoo

I would recommend you read more here, especially about return types.

Note: In this case, it may be better to write an Extension method, especially if we want to modify a single instance of a Foo, but we don't want to get into that. Also if we were really wanting to create a new instance of Foo like that, it would be better to use a constructor.

Upvotes: 2

Modika
Modika

Reputation: 6282

GradeStatics is a class by definition. When you use the new keyword you are creating an instance of that class (an object) which you can pass around or assign it to a variable.

So your snippets above, GradeStatistics is defined as a class in the gradestatics.cs file. Grades.cs creates an instance of this class in the computestatics method and assigns it to the stats variable which is populated in yhat method and then returned

Upvotes: 2

Zeeshan Elahi
Zeeshan Elahi

Reputation: 267

First, GradeStatistics is a Class.

Second, it is not referred as a variable anywhere. But stats is actually a variable of type GradeStatistics in this line.

GradeStatistics stats = new GradeStatistics();

Third, ComputeStatistics is a function which return GradeStatistics.

And if you have read OOP than you should know that any object of any class type can be returned as a function return value.

Upvotes: 3

Lasse V. Karlsen
Lasse V. Karlsen

Reputation: 391306

GradeStatistics is a class, from this declaration:

public class GradeStatistics

stats is a variable of type GradeStatistics, from this declaration:

GradeStatistics stats = new GradeStatistics();

The return type of ComputeStatistics is GradeStatistics, from this declaration:

public GradeStatistics ComputeStatistics()

So, it's a class. It is being used to declare a variable with a particular type, and it is used to declare what a particular method will return, an object of the type.

If it helps, you can sort of think of a type as a blueprint. You can have a blueprint of a house. This will tell you that "if you had a house", this is what it would look like.

When you construct an instance of the type, ie. build the house, you get an instance. It has a type (it follows the blueprint), but it may be different from other instances, having other property values (like the color of the paint used, or the style of doors).

Upvotes: 7

Related Questions