1/52
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced |
---|
No study sessions yet.
2D array
A data structure that consists of a collection of elements organized in rows and columns, allowing for efficient storage and manipulation of grid-like data.
Methods
It is a block of codes that perform a specific task.
CONSTRUCTORS
Is the special method called when instantiating a class or create an object. It is used to initialize the attributes of an object or run a block of code when an object is created.
Encapsulation
The principle of bundling the data and methods that operate on the data within a single unit, or class, and restricting access to some of the object's components.
Argument
A value passed to a method or constructor when it is called, which can influence its behavior and output.
In declaring 2D arrays
the array elements must have the same number of values or columns. In declaring 2D arrays, rows and columns values cannot be interchange.
.GetLength
(row/column) may be used to get the number of rows or columns in a 2D array.
Static
means that the method belongs to the Program class and not an object of the Program class
Void
a keyword used to specify that a method does not return a value.
Global Variables
variables that are accessible from any method within a program, typically declared outside of all functions.
Local Variables
variables defined within a method, which can only be accessed inside that method.
METHOD PARAMETERS
METHODS WITH DEFAULT PARAMETER VALUE
Also known as optional parameter.
Every optional parameter contains a default value which is part of the definition.
Equal sign (=) is used in declaring a default parameter value in the method statement.
Default value is used when the method is called without an argument.
The optional parameters are always defined at the end of the parameter list
NAMED ARGUMENTS
It enables to specify an argument for a parameter by matching the argument with its name rather than with its position in the parameter list.
• When used, the arguments are evaluated in the order in which they appear in the argument list not in the parameter list.
RETURN KEYWORD
It is used to return a value from the method.
METHOD OVERLOADING
An important feature of Object-Oriented Programming that refers to the ability to redefine a method in more than one form.
METHODS WITH RETURN
Values returned from return keyword may be stored in a variable after calling method.
Values may also be printed after returned in the calling method inside Console.WriteLine method.
CLASSES
It is created by the programmer.
It will act as a blueprint of an object that will be implemented in the program.
They contain all the attributes and methods of an object.
CLASS INSTANTIATION
The process of creating an instance of a class so it can be used in the program.
Can also be called objects.
Multiple instances of a class can be created
Access Modifiers
Used to modify where classes, variables and methods are accessible
default or internal
The class can only be accessed in the same assembly not other assemblies.
public
The class can be accessed in all classes.
default or private
It can only be accessible inside its own class.
internal
The methods and variables can only be accessed in the same assembly not other assemblies.
protected
It can only be accessed in the same class or in a class that is inherited from that class.
readonly Keyword
keyword used so that a variable cannot be reassigned to a new value.
OBJECT-ORIENTED PROGRAMMING
It focuses on implementing real world objects using Classes to create variations of Objects, that has attributes and purpose.
• It helps us create much flexible and efficient code than procedural programming.
fields and methods
Fields and methods inside a class are called class members.
ATTRIBUTES / FIELDS
These are the global variables declared inside the class of an object.
• It is used to create variations of an object using only one class.
• Can be accessed by creating an object of the class, and by using the dot syntax ( . )
METHODS
These are used to perform certain actions.
• They define how an object of a class behave.
• Just like attributes/field, it can access with dot syntax ( . )
OBJECTS
It is created by instantiating a Class. It is anything that has an attribute and a purpose. It is an instance of a class. It holds data and can access methods and their properties. Ex. Person, Furniture, Food.
State
It is represented by attributes of an abject, and reflects the properties of an object
Behavior
It is represented by the methods of an object and also reflects the response of an object with other objects.
Identity
It gives a unique name to an object and enables one object to interact with other objects.
CLASS INSTANTIATION
The process of creating an object using a class so that it can be used in the program.
• All the instances share the attributes and the behavior of the class. But the values of those attributes are unique for each object.
• A single class may have any number of instances.
new keyword
is used to create an object of the class.
CONSTRUCTORS
Is the special method called when instantiating a class or create an object.
It is used to initialize the attributes of an object or run a block of code when an object is created.
Constructor method are named after their Class Name and it cannot have a return type (like void or int).
Default Constructor
Created automatically by C# if not defined. A constructor with an empty code and no parameters. It has every instance of the class to be initialized to the same value. It initializes all numeric field to zero and all string and object fields to null inside a class.
Parameterized Constructor
A constructor that have at least one parameter. It can initialize each instance of the class to different values. The values passed to the constructors are called arguments. Values must be the same number and type of values as parameters.
Copy Constructor
Used to create an object by copying data from another object of the same class. Useful when you need to duplicate an object or create a new object based on the state of another.
Private Constructor
It is when a constructor is created with a private specifier. It is impossible for other classes to derive from this class and it’s not possible to create an instance of this class. It is the implementation of the singleton class pattern. Can be used when class members are static. Can be accessed using the class name.
Static Constructor
A constructor that must be invoked only once in the class and it has been invoked during the creation of the first reference to a static member in the class. It initialized static fields or data of the class and is to be executed only once. It can’t be called directly. It does not take access modifiers or any parameters. It is called automatically to initialize the class before the first instance is created. Only one static constructor in a class.
this keyword
It refers to the class itself. It will enable to access global variables inside the class if you have the same variable names in the parameter. Cannot be used when variables in the parameter is not the same names with global variable. It refers to the current instance of the class.
Example
Automatically Add Constructor
Use Quick Actions (Ctrl + . )
Type ctor and tab key.
Object Methods
Are methods declared inside an object class.
It is considered as the object’s purpose.
Encapsulation
It is an OOP technique used to hide data from direct access.
The variables or data of a class are hidden from any other class and can be accessed only through any member function of its class in which they are declared.
It is also known as data-hiding.
It can be achieved by declaring all the variables in the class as private and using C# Properties in the class to set and get the values of variables.
Data protection and control
Data can be validated and controlled before storing in the variable.
Achieving Data Hiding
The user will have no idea about the inner implementation of the class.
Security
Helps to secure code since it ensures that other units(classes, interfaces, etc) can not access the data directly.
Flexibility
Makes code more flexible, allowing the programmer to easily change or update the code.
Getters & Setters
These are used to get and set encapsulated variables.
Note :
Do not use the name for the field and property.
Acts as the middleman between the main class access to the private field.
Create a getter and setter for every private field.
Property must be declared public so the main class can access it.
Remove the set line if the variable is read-only and get line if the variable is write-only.