C# Programming
Classes
A blueprint for a specific object.
Collection of objects of similar type.
Model for creating objects.
Objects
template/blueprint that describes the behaviour/state that an object of its type support
Instance of a class
Process of creating an object = instantiation
Fields are actual variables in your object that stores a particular piece of information.
The data and the methods are called members of an object.
Communicate through methods
Creating an object means allocating memory to store the data of variables temporarily
State is stored in fields and behaviour is stored in methods
Classes are the Nouns in your analysis of the problem
Methods in an class correspond to the Verbs that the noun does
Properties are the Adjectives that describe the noun
Abstraction
hide the implementation details and display only essential features of the object
Once a class has been declared as an Abstract class, it cannot be instantiated; it means the object of that class cannot be
created.
Encapsulation
variables of a class will be hidden from other classes, and can be accessed only through the methods of their current class
let’s you control the data and operations within a class that are exposed to other classes
To encapsulate declare the variables of a class as private
Fields are like variables because they can be read or set directly, subject to applicable access modifiers
Properties have get and set accessors, which provide more control on how values are set or returned. (read-write property)
If you don’t want the user to change the value do not add a set property = read-only
Property without get accessor = write-only
Private field = public property (normally)
Eg.
class Person
{
private string name; // field
public string Name // property
{
get { return name; } // get method
set { name = value; } // set method
}
}
, Enumeration
Special class that represents a group of constants
Eg. class Program
{
Private Gender gender;
{
Public enum Gender
{
Male;
Female;
}
gender = Gender.Female;
Constructors
special type of method that’s executed when an object is instantiated
enable the programmer to set default values, limit instantiation, and write code that is flexible and easy to read
Instantiating a class
objects created at runtime from a class are called instances of that particular class
Inheritance
allows one to create new classes that reuse, extend and modify the behaviour that is defined in other classes.
Class whose members are inherited = base class/ parent class
Class that inherits those members = derived class/ child class
A derived class can only have one base class
Derived class is a specialization of the base class
Derived class gains all the members of the base class except its constructors and destructors
Derived class can reuse the code in the base class without having to re-implement it.
Derived class can add more members
Derived class extends the functionality of the base class
Eg. public class Circle:Shape (Shape is the base class)
If you define a constructor in the base or derived class you should do the same in the opposite
Polymorphism
Occurs when we have many classes related to eachother by inheritance
Uses the methods from Inheritance to perform different tasks, which allows one to perform a single action in different ways.
Static polymorphism:
The benefits of buying summaries with Stuvia:
Guaranteed quality through customer reviews
Stuvia customers have reviewed more than 700,000 summaries. This how you know that you are buying the best documents.
Quick and easy check-out
You can quickly pay through EFT, credit card or Stuvia-credit for the summaries. There is no membership needed.
Focus on what matters
Your fellow students write the study notes themselves, which is why the documents are always reliable and up-to-date. This ensures you quickly get to the core!
Frequently asked questions
What do I get when I buy this document?
You get a PDF, available immediately after your purchase. The purchased document is accessible anytime, anywhere and indefinitely through your profile.
Satisfaction guarantee: how does it work?
Our satisfaction guarantee ensures that you always find a study document that suits you well. You fill out a form, and our customer service team takes care of the rest.
Who am I buying this summary from?
Stuvia is a marketplace, so you are not buying this document from us, but from seller Saaji. Stuvia facilitates payment to the seller.
Will I be stuck with a subscription?
No, you only buy this summary for R79,00. You're not tied to anything after your purchase.