Sunday, August 2, 2009

How to we write a class in C#?

I'm a beginner to the C# language. Please anybody know the concept of classes and its structure mail to me.

How to we write a class in C#?
A class is essentially a blueprint of anything in the world which programmers call objects. For example a class could be person, which will store data in fields. Fields would include Name, surname, address etc. Any data which involves the person.Also it can include methods which the person can perform e.g. payBill(). Basically that's it. To create a class simply do this:


class Person


{


private string name;


private string surname;


private string address;





public void payBill()


{


}


}





Then you have to create an instance of a class to use it. To create an instance of a class, type this in your main code:


Person myPerson = new Person();
Reply:Try this link
Reply:Wow. The best way to conceptualize a class is to view it as a recipe - a recipe for an object. The most commonly used example is for a class is a 'Car'. So, let's say we are creating a Class - or recipe - for a car. The 'ingredients' - the things that are needed to 'create' the object are called attributes. Attributes of a Car might be engine, wheels, paint, etc.


Now, a car also needs to perform 'actions'. These actions, in a Class, are called methods. Some methods might be 'accelerate', 'turn', 'start/stop', etc.


So, to make a class for a car - or any object - you need to have attributes, and if necessary, methods.


Now, how you go about that specifically, well..... that's what books and teachers are for. If you are not in school, perhaps you should take a class. If you are, I would suggest talking to your teacher. Good luck.
Reply:Can you asks this question again?


If English is your second language, ask it in your native lanugage and I'll have it translated for me.


No comments:

Post a Comment