skip to main
|
skip to sidebar
Classes and Objects
Class
- The class represents or defines the common characteristics (attributes) of an object of a particular type.
- In simple words, Class is a collection of fields, properties, methods and events, where a method is a collection of statements and an event is an action on the object.
- The syntax for defining classes in C# is simple
- [attributes] [modifiers] class <classname> [:baseclassname]
{
[class – body]
}[;]
- For e.g., you might want to define a class of "Person". Every person will share some characteristics (attributes) like first name, last name, address, etc.,
public class person
{
private string _FirstName;
private string _LastName;
private string _Address;
}
- So, a class serves as a blueprint for similar type of object.
- A class is made up of attributes and behavior. Attributes are defined in terms of member variables and behavior is expressed in terms of function.
Objects
- An object is an instance of a class.
- It is a representation of a real world thing.
- Objects can have both attributes (data) and behaviors.
- For e.g., You and me are instances of the class"Person". We share common attributes like we both have a first name, last name and so on. We both have some common behaviors like walking, talking, etc.,
- By default, an object have the following properties:
- ToString ()
- Equals ()
- GetType ()
- GetHashCode ()
- Objects can access only public data
- A variable which is under control of an object is called an instance variable.
- The base class for all objects is Object in the System namespace.
0 comments :
Post a Comment