AntKneeM;719520 said:
Iam not sure if this is allowed or not, but ive searched and ive yet to come accross and C# Tutorial to keep my attention long enough to explain to me what a class is ( Example ) iam looking for a decent C# Tutorial that is extensive BUT breaks things down and explains things to me and can help me understand better the C# language ?? so if anyone knows of a good one for beginners please let me know!~
Goto your local book store, or online such as amazon and buy the bool "Learning C#" by O'Reilly Publishing. This book is my #1 recommendation to people looking to really learn C# but can't quite grasp the C# tutorials on the web (same problem I had ). Anyway this book is awsome cause it descibes things with metaphors(which I love).
But to answer your question, "What is a class". The simply answer would be to descibe it like school. You goto school and you have 'classes' each 'class' contains information reguarding a subject. Generally, when you goto school you start with the basics and move on up the ladder to more advanced things, but they are all relative to the base class. In programming you have the same principals. You start making objects with a base class and move on up with new classes with the same base class. Lets look at an example.
Lets take my metaphor and pretend we are writing code for Math 101.
So what do we know about Math 101. We know that it is going to be able Math, and we know we will learn some basic math functions. So we would start out with
Code:
public class Math101
{
public Math()
{
}
}
Now, in Math 101 we learn things like Add, Subtract, Multiply and Divide. So our base Math class needs to have these functions(also called methods).
Code:
public class Math101
{
public Math()
{
}
public int Add(int a, int b)
{
return a + b;
}
public int Subtract(int a, int b)
{
return a - b;
}
public int Multiply(int a, int b)
{
return a * b;
}
public int Divide(int a, int b)
{
return a / b;
}
}
So there, now we have our basic math class we have called Math101
If we wanted to use our Math101 class in some code we could now do so like this
Code:
Math101 mathClass = new Math101();
int result = Math.Add(2, 2);
In this example result would be 4 since our add function returns the result of 2 + 2. Now, like I was saying earlier, in school we have a base class and we move up to more advanced classes. However something to keep in mind is that the base functions always stay with us as we progress through the more advanced classes
In programming we call this 'Inheritance'. Basically this means our new class will contain the same functions and functionality of its base class, just as Math 102 would contain the same math functions in Math 101.
So for example
Code:
public class Math102 : Math101
{
public Math102() : base()
{
}
}
Now since this is a more advanced class it needs new, more advanced functions. So lets assume in Math 102 we learn how to average numbers. We would need to Add all the number supplied to our function together then divide them by the count of numbers passed to be averaged. Well since we already learned how to Add and Divide in our Math 101 class, we can use these same functions to help us with our work.
Lets take a look:
Code:
public class Math102 : Math101
{
public Math102() : base()
{
}
public int Average(int[] values)
{
int added = 0;
for(int i = 0; i < values.Length; i++)
{
added = Add(added, values[i]);
}
return Divide(added, values.Length);
}
}
See how I am able to use the functions from the base class(Math 101). This is basically how classes work, and how they should be used.
If you have questions regarding this please feel free to ask.