C# Programming for Beginners | C# Code for C# classes, methods, properties

If you missed my previous post on C# classes, C# methods and C# objects in code, please read that first (click the link to see that post).  The code that follows is a continuation of that post.

In Visual Studio, create a new console project.  I called mine TestClasses.  A console project runs like the old DOS command line console window. 

Right, so to the code.  Open the Program.cs class and click directly above the “
static void Main(string[] args)” line.  First of all you simply have to declare the class.    

A C# class is declared by using the “public” access modifier and the keyword “class”, followed by the name that you want your class to be, in this case, “Bird”.  If you don’t know what the access modifier is, don’t worry about it for now, just make sure it is public.  The curly brackets show where the beginning and end of the class is, anything in between them is part of the Bird class.

    public class Bird


So there, you have now declared your C# class.  The class is pretty useless without anything in it, so we’ll have to add a few things.  First of all we need a constructor.  The constructor enables us to create (construct) the objects later on.  Without it we could not create an instance of the class (in this case the Eagle and Chicken objects).
public class Bird

       //this is the constructor

        public Bird()



According to our earlier discussion we now need to add the C# methods of the Bird class.  These were Fly() , Eat() and Walk().  Methods are declared almost the same way as classes are.  First the access modifier (“public”) then you need a return type (“void”).  A return type is used if your method needs to return something for later use, here your method returns nothing, therefore here the return type is void.  So add these to your Bird class.
//Methods - What can the Bird do?
   public void Fly()

          //you would put an actions needed here

            Console.WriteLine("Bird is Flying");

        public void Eat()


            Console.WriteLine("Bird is Eating");

        public void Walk()


            Console.WriteLine("Bird is Walking");

So, now we have our class and our class has some methods, which enable it to do something.  The next step is to add the properties of Colour, Height, Weight, Predator.  To do this, first of all declare the variables as follows:
//variables for the properties

        private string colour;

        private string height;

        private string weight;

        private bool predator;
The variables will hold the information.  Notice here that the access modifiers are private.  This means that outside of the class they cannot be accessed.  We are going to set up the access to them so that they can only be accessed through their properties as follows:

The properties either get the contents of the private variable or set the private variable to the value that you will define.  You might have noticed the bool for pPredator.  Why is this bool?  Bool means either true or false, and so here the bird can either be a predator (true), or not (false).

And that is it for setting up your bird class.  Save your project as it is.

Next time here on C# Programming for Absolute Beginners, we’ll take a look at how to use this class to create your Eagle and Chicken objects and also how to use these objects to call the methods from the bird class.  

For more simple programming c# examples please click the link.  This will take you through the C# code for creating C# objects and calling C# methods.


simi said...


First of all. Thanks very much for your useful post.

I just came across your blog and wanted to drop you a note telling you how impressed I

was with the information you have posted here.

Please let me introduce you some info related to this post and I hope that it is useful

for .Net community.

There is a good C# resource site, Have alook