C# Programming for Beginners | C# Classes, C# Methods and C# Objects in Programming

To gain a better understanding of programming concepts such as C# classes, methods, and objects we are going to look at real life things around us that will help us understand the code structure.


What is a C# Class?
A class is a very important part of object oriented techniques.  A class is sometimes described as a “blueprint” which you can use to create an object.  For instance if you had to think of a “Bird” you will probably immediately think of a creature that can fly, with two wings, a beak, two eyes, two feet and covered in feathers.  That is essentially what a class is, an idea of what the object is going to look like.  The class contains properties, for instance: Colour, Height, Weight, Predator.  A class is an abstract idea - it isn’t anything concrete.  To get to something concrete we must use an object.  We’ll look at objects later but first let’s look at methods.


What are C# Methods?
A method is an action that the class can perform.  Looking quickly at the bird class, ask yourself, “How does the bird function, what are the verbs?”  We could say that some methods would be:  Fly() ; Eat() ; Walk().  Therefore these doing words will become the Methods of our class.


What are C# Objects?
An object is an instance of a class.  What would be an instance of our Bird class?  Let’s choose two different objects -Chicken and Eagle to see how they would differ as objects.





Class Bird:
Object Chicken:
Methods:
Fly()
Eat()
Walk()
Properties:
Colour = yellow
Height = 30cm
Weight = 200grams
Predator = no



Class Bird
Eagle
Methods:
Fly()
Eat()
Walk()
Properties:
Colour = brown
Height = 90cm
Weight = 1100grams
Predator = yes


Can you see how the object is different from the class?  They are both of the bird class yet are quite different objects when you look closely at their properties.  The Chicken is much smaller, has a different colour, weighs a different weight and is not a predator.  The Eagle is brown, quite large, heavy and is a predator.  They both however can Fly, Eat, and Walk.
That's it for now, next time on C# Programming for Absolute Beginners, I'll provide the code for the above example using C#.


Here is alink to that post: C# code for classes, methods and properties.


0 comments: