Mapa mental
OOP #c
Conteúdo do mapa
OOP #c
Class
- To define a class: class SampleClass
- Struct
- To define a structure: struct SampleStruct
Propetis and fields
- Fields
- class SampleClass { public string sampleField; }
- Auto-implemented property
- class SampleClass { public int SampleProperty { get; set; } }
- Fields
Methods
- A method is an action that an object can perform.
- class SampleClass { public int sampleMethod(string sampleParam) { // Insert code here } }
- Overload Method
- public int sampleMethod(string sampleParam) {}; public int sampleMethod(int sampleParam) {}
Constructors
- To define a constructor for a class: C#Copy public class SampleClass { public SampleClass() { // Add code here } }
- Finalizers
- Finalizers are used to destruct instances of classes. In the .NET Framework, the garbage collector automatically manages the allocation and release of memory for the managed objects in your application. However, you may still need finalizers to clean up any unmanaged resources that your application creates. There can be only one finalizers for a class.
- Events
- Events enable a class or object to notify other classes or objects when something of interest occurs. The class that sends (or raises) the event is called the publisher and the classes that receive (or handle) the event are called subscribers. For more information about events, how they are raised and handled,
- Nested Classes
- A class defined within another class is called nested. By default, the nested class is private. C#Copy class Container { class Nested { // Add code here. } }
- Instantiating Classes
- To create an object, you need to instantiate a class, or create a class instance. SampleClass sampleObject = new SampleClass();
Static Classes and Members
- To define a static member: C#Copy static class SampleClass { public static string SampleString = "Sample String"; } To access the static member, use the name of the class without creating an object of this class: Console.WriteLine(SampleClass.SampleString);
Anonymous Types
- Anonymous types enable you to create objects without writing a class definition for the data type. Instead, the compiler generates a class for you. The class has no usable name and contains the properties you specify in declaring the object.
- Code
- // sampleObject is an instance of a simple anonymous type. var sampleObject = new { FirstProperty = "A", SecondProperty = "B" };
Inheritance
- C# doesn't support multiple inheritance. That is, you can specify only one base class for a derived class. To inherit from a base class: C#Copy class DerivedClass:BaseClass{} By default all classes can be inherited. However, you can specify whether a class must not be used as a base class, or create a class that can be used as a base class only. To specify that a class cannot be used as a base class: C#Copy public sealed class A { } To specify that a class can be used as a base class only and cannot be instantiated: C#Copy public abstract class B { }
Interfaces
- To define an interface: C#Copy interface ISampleInterface { void doSomething(); } To implement an interface in a class: C#Copy class SampleClass : ISampleInterface { void ISampleInterface.doSomething() { // Method implementation. } }
Generics
- To define a generic class: C#Copy public class SampleGeneric<T> { public T Field; } To create an instance of a generic class: C#Copy SampleGeneric<string> sampleObject = new SampleGeneric<string>(); sampleObject.Field = "Sample string";
Delegates
- To create a delegate: public delegate void SampleDelegate(string str);
- To create a reference to a method that matches the signature specified by the delegate
- class SampleClass { // Method that matches the SampleDelegate signature. public static void sampleMethod(string message) { // Add code here. } // Method that instantiates the delegate. void SampleDelegate() { SampleDelegate sd = sampleMethod; sd("Sample string"); } }
Informações
- Descrição
- OOP #c Mapa Mental sobre OOP #c, criado por Martti Zeiger em 10-04-2018.
- Criado por
- Martti Zeiger
- Visualizações
- 0
- Avaliação