C# – Introduction to Classes and Objects

One of the main concepts which we need to know for Object Oriented Programming is classes and what kind of thing it represents in real world scenarios so we can better grasp it in a way that we understand it fully.

Class is a block of code that defines a data type.

An object is an instance of a class.

Metaphors:

Blueprint vs. Houses

Recipe vs. Cupcakes

Pattern vs. Bluejean

Cookie Cutter vs. Cookies

Classes have members, like Properties and Methods

Properties define the attributes that are set on an instance of the class / represent the “state” of an object. You can set (assign) and get (retrieve) property values on an object”

Methods define actions an instance of a class using the new keyword. Think “factory”

You can access the member of an object by using member access operator, dot(.)

Conceptually, classes are delegated a responsibility in the system or represent some domain concept in the system.

Classes are ultimately custom date type, more complex than the simple data types we have worked with. Therefore you can use them anywhere you use other data types (like as input parameters or return values from a method)

class Car{

}

Auto implemented properties – simple properties.

prop [tab] [tab] [enter] [enter]

this keyword – access a member of the current instance of the class

public void myMethod(){

  this.Year = 1966;

}

 

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: