C#
---
#### Handout 04: Classes and Objects
---
**I. Classes and Objects**
- **Class:** an object-oriented mechanic that defines a set of variables and methods for a declared object, AKA the "blueprints" of objects.
- **Object:** a `concrete entity` based on a class, often called the "instance of a class" and usually represents how a class should be represented as an entity in a program.
**II. Value and Reference Types**
- **Value Types:**
- uses built-in data types to declare variables.
- used for `static memory allocation` because the memory size is always consistent.
- stores their value in a memory location called the `stack`.
- **Reference Types:**
- used primarily for `storing objects`.
- used for dynamic memory allocation; the memory size can change during runtime.
- the data for the object is stored in the `heap`, while the heap's memory address (the pointer) is stored in the `stack`.
- **III. Encapsulation:** AKA `information hiding`. A mechanic where members are combined together iwhtin a class and used to restrict outside access to the inner workings of that class.
- **A. Benefits of Encapsulation**
- **Data Security:** controls how data is accessed or modified.
- **Flexibility:** allows code to be more adaptable to new requirements.
- **Code Isolation:** allows programmers to change one part of the code without affecting other parts.
- **B. Common Access Modifiers**
- `public`: accessible to anything outside the class.
- `private`: accessible only within the class; it is hidden from outside classes.
- `protected`: accessible only by the same class or a derived class.
- **IV. Constructors**
- special member methods that are automatically executed whenever a new object of a class is created.
- must have the exact same name as the parent class.
- must be public and cannot have a return type.
- by default, has no parameters, but can be added if needed.
- **V. Properties**
- member subclasses that provide a flexible way to read, write, and compute the value of a private field.
- acts like a public data members but allow the programmer to control the logic of access.
- **Accessors:** properties use `get` (to read or compute) and `set` (to write) statements. Any accessor can be omitted to change how the property is used.
- **Auto-Implemented Properties:** `auto-properties` that allow for a fast and short declaration of private members.
---
#### Handout 05: Arrays and Strings
---
**I. Arrays:** built-in classes in C# designed to store and is a `data structure` used to manipulate a collection of data of the same type.
- **A. Declaring and Instantiating Arrays**
- To declare an array, write the data type followed by square brackets ` [ ] ` and the identifier.
- Since arrays are objects, they must be instatiated using the `new` keyword, where the square brackets specify the number of elements the array will hold.
- **B. Assigning Values**
- You can omit the array size and the `new` keyword statement during declaration to simplify the code.
- **Single Assignment:** use the array's identifier and a specific index number. C# uses zero-indexing, meaning the first element is always at index 0.
- **Multiple Assignment:** you can assign initial values during declaration using curly braces `{ }` with values separated by commas.
- **C. Accessing Values:** Elements are accessed during the identifier and the index number in square brackets. These values can be used for display, mathematical expressions, or conditional evaluations.
**II. Arrays and Loops**
- **Loops:** generally used to iterate through array elements to make assignment based on calculations or to read and display values.
- **`foreach` Loop:** provides a shorter, more efficient way to access elements. The variable used within the foreach loop must match the data type of the array elements.
**III. Multidimensional Arrays:** `matrices`, arrays with multiple dimensions where each dimension is interpreted as another array. They are declared using commas within square brackets `[ ]`. To access an element, you must provide index values for every dimension.
- **2D Arrays:** The simples version is the `table` (two-dimensional).
**IV. Array Properties and Methods:** The Array class provides members accessed via **dot syntax**.
- **`Length` Property:** returns the total number of elements; highly useful for determining how many times a `for` loop should run.
- **`Rank` Property:** reutnrs the number of dimensions in the array.
- **Mathematical Methods**
- **`Min()`:** returns the smallest value.
- **`Max()`:** returns the largest value.
- **`Sum()`:** returns the sum of all elements.
**V. Working with Strings**
- **Strings as Objects:** Strings are objects of the String class.
- **String Properties and Methods**
- **`Length`:** returns the total characters in the string, including spaces.
- **`IndexOf(value)`:** returns the zero-indexed position of the first occurence of a specific value.
- **`Insert(index, value)`:** adds a value at the specified index.
- **`Remove(index)`:** deletes all characters after the specified index.
- **`Replace(oldValue, newValue)`:** swaps a specific value with a new one.
- **`Substring(index, length)`:** extracts a portion of the string. If the length is omitted, it goes to the end of the string.
- **`Contains(value)`:** returns a boolean true if the string contains the specified value.