Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --anonymous class-------------------------
- -convenient way of encapsulating read-only properties into a single object without the need to explicitly define a type first.
- -use with LINQ
- var anAnonymousObject = new { Name = "Tom", Age = 65 };
- -----------------------------------
- --static class---------------------------
- -use static class to encapsulate some useful functionality, rather than to represent an instance of anything.
- -static class is a class that cannot be instantiated.
- -to call a method on a static class, you call the method on the class name itself instead of on an instance name
- Conversions.KilosToPounds(weightInKilos);
- ----------------------------------
- --properties------------------------
- Auto-implemented properties make property-declaration more concise when creating simple accessor methods (getter and setter). They also enable client code to create objects. When you declare a properties this way, the compiler will automatically create a private, anonymous field in the background that can only be accessed through the get and set accessors.
- public string Name { get; set; }
- to make a property read-only or write-only only include either the getter or setter
- // read only
- public string Name { get; }
- // write only
- public string Name { set; }
- --------------------
- --structs-------------------------
- structs should be small, simple (one-level) collections of related properties, that are immutable once created; for anything else, use a class.
- -structs can't derive from any other class or struct type
- -structs can't explicitly define a default parameterless constructor
- -structs are value types
- ---------------------
- --enums----------
- enum Day { Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday };
- By default enum values start at 0 and each successive member is increased by a value of 1. As a result, the previous enum 'Day' would contain the values:
- Sunday = 0
- Monday = 1
- Tuesday = 2
- You can change the default by specifying a starting value for your enum as in the following example.
- enum Day { Sunday = 1, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday };
- // Set an enum variable by name.
- Day favoriteDay = Day.Friday;
- -------------------------
- --arrays---
- //declare empty with array size of 10
- int[] arrayName = new int[10];
- //declare with intilialized values
- int[] arrayName = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
- //number will contain the value 3
- int number = oldNumbers[2];
- // Create an array that is 10 long(rows) by 10 wide(columns)
- int[ , ] arrayName = new int[10,10];
- //multi dimensional
- In order to access elements in a multidimensional array, you must include all indices as in the example code here.
- // Access the element in the first row and first column
- int value = arrayName[0,0]
- //Access the element in the first row and second column
- int value2 = arrayName[0, 1];
- //Access the element in the second row and first column
- int value2 = arrayName[1, 0];
- -----------------
- --indexer example-------------------
- public struct Menu
- {
- private string[] beverages;
- // This is the indexer.
- public string this[int index]
- {
- get { return this.beverages[index]; }
- set { this.beverages[index] = value; }
- }
- // Enable client code to determine the size of the collection.
- public int Length
- {
- get { return beverages.Length; }
- }
- }
- ----------------------------
- --abstract class------
- -abstract classes cannot be instantiated
- -if employee is the parent class of manager and cashier, it should be abstract because an instance of employee should not exist (only managers and cashiers
- -Once you create an abstract class, you decide which methods "must" be implemented in the sub classes and which methods "can" be implemented, or overridden, in the sub class.
- -declare methods as virtual if they will be overridden
- -declare methods as abstract they must be implemented
- An abstract method cannot exist in non-abstract class
- An abstract method is not permitted to have any implementation, including curly braces
- An abstract method signature must end in a semi-colon
- An abstract method MUST be implemented in any sub class. Failure to do so will generate a compiler warning in C#.
- abstract class Person
- {
- public int Age{get; set;}
- public string Gender{get; set;}
- //set as protected so that its subclasses can use the this constructor to initialize values
- protected Person()
- {
- this.Age = 46;
- this.Gender = "F";
- }
- }
- private class Teacher: Person
- {
- //using base enables the use of the abstract classes defined constructor
- public Teacher(): base()
- {
- }
- }
- -------------------------------
- --sealed classes------
- -sealed classes cannot be inherited
- -----------------------------------
- --interface--------------
- -interface specifies a set of characteristics and behaviors by defining signatures for methods, properties, events, and indexers, without specifying how any of these members are implemented.
- -when a class implements an interface, the class provides an implementation for each member of the interface.
- -by implementing the interface, the class is thereby guaranteeing that it will provide the functionality specified by the interface.
- // Declaring a Class that Implements Multiple Interfaces
- public class Coffee: IBeverage, IInventoryItem
- {
- }
- -if need be (like implementing multiple interfaces) you can explicitly implement the interface properties/methods/etc...
- // explicitly implementing the IBeverage method IsFairTrade
- public bool IBeverage.IsFairTrade { get; set; }
- interface polymorphism
- // Representing an Object as an Interface Type
- Coffee coffee1 = new Coffee();
- IBeverage coffee2 = new Coffee();
- // implicitly casting to an Interface Type
- IBeverage beverage = coffee1;
- // Casting an Interface Type to a Derived Class Type
- Coffee coffee3 = beverage as Coffee;
- // OR
- Coffee coffee4 = (Coffee)beverage;
- Implementing Multiple Interfaces
- ----------------------------------
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement