Advertisement
sixlsix

c# notes

May 9th, 2015
225
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.13 KB | None | 0 0
  1. --anonymous class-------------------------
  2. -convenient way of encapsulating read-only properties into a single object without the need to explicitly define a type first.
  3. -use with LINQ
  4. var anAnonymousObject = new { Name = "Tom", Age = 65 };
  5. -----------------------------------
  6.  
  7. --static class---------------------------
  8. -use static class to encapsulate some useful functionality, rather than to represent an instance of anything.
  9. -static class is a class that cannot be instantiated.
  10.  
  11. -to call a method on a static class, you call the method on the class name itself instead of on an instance name
  12. Conversions.KilosToPounds(weightInKilos);
  13. ----------------------------------
  14.  
  15. --properties------------------------
  16. 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.
  17.  
  18. public string Name { get; set; }
  19.  
  20. to make a property read-only or write-only only include either the getter or setter
  21.  
  22. // read only
  23. public string Name { get; }
  24.  
  25. // write only
  26. public string Name { set; }
  27. --------------------
  28.  
  29. --structs-------------------------
  30. structs should be small, simple (one-level) collections of related properties, that are immutable once created; for anything else, use a class.
  31.  
  32. -structs can't derive from any other class or struct type
  33. -structs can't explicitly define a default parameterless constructor
  34. -structs are value types
  35. ---------------------
  36.  
  37. --enums----------
  38.  
  39. enum Day { Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday };
  40.  
  41. 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:
  42.  
  43. Sunday = 0
  44. Monday = 1
  45. Tuesday = 2
  46.  
  47. You can change the default by specifying a starting value for your enum as in the following example.
  48.  
  49. enum Day { Sunday = 1, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday };
  50.  
  51. // Set an enum variable by name.
  52. Day favoriteDay = Day.Friday;
  53. -------------------------
  54.  
  55. --arrays---
  56. //declare empty with array size of 10
  57. int[] arrayName = new int[10];
  58.  
  59. //declare with intilialized values
  60. int[] arrayName = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
  61.  
  62. //number will contain the value 3
  63. int number = oldNumbers[2];
  64.  
  65. // Create an array that is 10 long(rows) by 10 wide(columns)
  66. int[ , ] arrayName = new int[10,10];
  67.  
  68. //multi dimensional
  69. In order to access elements in a multidimensional array, you must include all indices as in the example code here.
  70.  
  71. // Access the element in the first row and first column
  72. int value = arrayName[0,0]
  73. //Access the element in the first row and second column
  74. int value2 = arrayName[0, 1];
  75. //Access the element in the second row and first column
  76. int value2 = arrayName[1, 0];
  77. -----------------
  78.  
  79. --indexer example-------------------
  80. public struct Menu
  81. {
  82.     private string[] beverages;
  83.     // This is the indexer.
  84.     public string this[int index]
  85.     {
  86.         get { return this.beverages[index]; }
  87.         set { this.beverages[index] = value; }
  88.     }
  89.     // Enable client code to determine the size of the collection.
  90.     public int Length
  91.     {
  92.         get { return beverages.Length; }
  93.     }
  94. }
  95. ----------------------------
  96.  
  97. --abstract class------
  98. -abstract classes cannot be instantiated
  99. -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
  100. -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.
  101.  
  102. -declare methods as virtual if they will be overridden
  103. -declare methods as abstract they must be implemented
  104.  
  105. An abstract method cannot exist in non-abstract class
  106. An abstract method is not permitted to have any implementation, including curly braces
  107. An abstract method signature must end in a semi-colon
  108. An abstract method MUST be implemented in any sub class.  Failure to do so will generate a compiler warning in C#.
  109.  
  110.     abstract class Person
  111.         {
  112.             public int Age{get; set;}
  113.             public string Gender{get; set;}
  114.  
  115.         //set as protected so that its subclasses can use the this constructor to initialize values
  116.             protected Person()
  117.             {
  118.                 this.Age = 46;
  119.                 this.Gender = "F";
  120.             }
  121.         }
  122.  
  123.         private class Teacher: Person
  124.         {
  125.         //using base enables the use of the abstract classes defined constructor
  126.             public Teacher(): base()
  127.             {
  128.  
  129.             }
  130.     }
  131. -------------------------------
  132.  
  133. --sealed classes------
  134. -sealed classes cannot be inherited
  135. -----------------------------------
  136.  
  137. --interface--------------
  138. -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.
  139.  
  140. -when a class implements an interface, the class provides an implementation for each member of the interface.
  141.  
  142. -by implementing the interface, the class is thereby guaranteeing that it will provide the functionality specified by the interface.
  143.  
  144. // Declaring a Class that Implements Multiple Interfaces
  145. public class Coffee: IBeverage, IInventoryItem
  146. {
  147. }
  148.  
  149. -if need be (like implementing multiple interfaces) you can explicitly implement the interface properties/methods/etc...
  150.  
  151. // explicitly implementing the IBeverage method IsFairTrade
  152. public bool IBeverage.IsFairTrade { get; set; }
  153.  
  154. interface polymorphism
  155. // Representing an Object as an Interface Type
  156. Coffee coffee1 = new Coffee();
  157. IBeverage coffee2 = new Coffee();
  158. // implicitly casting to an Interface Type
  159. IBeverage beverage = coffee1;
  160.  
  161. // Casting an Interface Type to a Derived Class Type
  162. Coffee coffee3 = beverage as Coffee;
  163. // OR
  164. Coffee coffee4 = (Coffee)beverage;
  165. Implementing Multiple Interfaces
  166.  
  167.  
  168. ----------------------------------
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement