C# Flashcards
STRUCT
IS STORED IN STACK MEMORY; OPTIMIZED & EFFICIENT; ONLY USED FOR ENCAPSULATING SIMPLE DATA, LITTLE TO NO BEHAVIOR. GENERALLY SIMPLE DATATYPE OR PROPERTIES &
VERY SIMPLE FUNCTION OF METHODS
DICTIONARY
Dictionary − Dictionary is a collection of keys and values in C#. Dictionary is included in the System.Collection.Generics namespace.
HASHSETS
~UNORDERED COLLECTIONS OF UNIQUE ELEMENTS; DUPLICATES NOT ALLOWED
~DYNAMIC COLLECTION, but ONLY STORE SAME TYPES OF ELEMENTS
~SET OPERATIONS- INTERSECTIONS, UNIONS, & DIFFERENCES
HashSet in C# eliminates duplicate strings or elements in an array. In C#, it is an optimized set collection.
Properties
behave like fields when they are accessed. However, unlike fields, properties are implemented with accessors that define the statements executed when a property is accessed or assigned.
are first class citizens in C#. The language defines syntax that enables developers to write code that accurately expresses their design intent.
{get; set;}
Stack
(LIFO): Major Operations: Push - Add element into stack Pop - Remove an element from TOP Peek - Retrieve the TOP element
Queue
(FIFO): Major Operations: Enqueue - Add element into Queue Dequeue - Remove element from Queue Peek - Retrieve the TOP element
Abstract methods
~implicitly a virtual method.
~declarations are only permitted in abstract classes
~abstract method declaration provides no actual implementation,
~there is no method body; the method declaration
~ simply ends with a semicolon and there are NO curly braces { } following the signature.
STATIC
~SINGLE COMMAND DETERMINES WHETHER A METHOD OR VARIABLE BELONGS TO A CLASS AS A WHOLE OR A ONLY AS AN INSTANCE
~ANY METHOD OR VARIABLE LABELED ARE AVAILABLE WITHOUT HAVING TO CREATE OBJECTS OF THAT CLASS
~MAIN METHOD ALWAYS LABELED…
INDEXERS
~…FOR COLLECTIONS [ ] (square brackets)
~PROPERTIES FOR VARIABLES { } (curly braces)
~DECLARE USING KEYWORD AND DECLARING ARGUMENTS WITHIN SQUARE BRACKETS [ ]
~SIMILAR TO PROPERTIES; ENABLE INDEX PROPERTIES;
~PROPERTIES REFERENCED USING ONE OR MORE ARGUMENTS
~THE ARGUMENTS PROVIDE AN INDEX INTO SOME COLLECTION OF VALUE
VIRTUAL METHODS
~ALLOWS THE METHOD FOR THE BASE CLASS TO BE OVERRIDDEN
~ESSENTIAL FOR METHOD OVERRIDING & POLYMORPHISM
~DOESNT ALLOW METHODS TO BE PRIVATE (CAN’T BE INHERITED)
~A virtual method is used to override specified base class implementation when a runtime object is of the derived type.
~virtual method is declared abstract
can’t use this the virtual modifier with the static, abstract, private, or override modifiers
ABSTRACT VS. VIRTUAL
~ABSTRACT METHOD: NO IMPLEMENTATION MUST OVERRIDE WITH ABSTRACT BECAUSE THERE IS ONLY METHOD DECLARATION BUT NO IMPLEMENTATION
~VIRTUAL METHOD: ALREADY HAS IMPLEMENTATION IN BASE CLASS PROVIDE ABILITY TO OVERRIDE
base keyword; modified method in base class
ABSTRACT MODIFIER
~ENABLES YOU TO CREATE INCOMPLETE IMPLEMENTATION OF WHATEVER YOU APPLIED IT TO & MUST BE IMPLEMENTED IN CLASS
~IMPLICITLY USED BY INTERFACES
~EXPLICITLY USED BY ABSTRACT CLASSES
~ABSTRACT CLASS & INTERFACE CAN’T BE IINSTANTIATED BUT MEANT TO BE INHERITED/IMPLEMENTED
~ABSTRACT/CONCRETE CLASS CAN IMPLEMENT ONE OR MORE INTERFACES BY WHICH MULTIPLE INHERITANCE IS ACHIEVED BUT THE CLASS CAN ONLY INHERIT ONE CLASS AND ONE OR MORE INTERFACES
Abstract Method
~IMPLICITLY A VIRTUAL METHOD
~DECLARATION ARE ONLY PERMITTED IN ABSTRACT CLASSES
~ DECLARATION PROVIDE NO ACTUAL INPLEMENTATION
~NO METHOD BODY; DECLARATION ENDS W/ SEMICOLON & NO CURLY BRACES FOLLOWING THE SIGNATURE
6 ACCESS MODIFIERS
~PUBLIC ~PRIVATE ~PRIVATE PROTECTED ~PROTECTED ~PROTECTED INTERNAL ~INTERNAL
PUBLIC ACCESS MODIFIER
~ACCESSED BY ANY OTHER CODE IN THE SAME
~ASSEMBLY OR ANOTHER ASSEMBLY THAT REFERENCES IT
CONTROLLED BY THE ACCESSIBILITY ITSELF
PRIVATE ACCESS MODIFIER
~CAN ONLY BE ACCESSED BY CODE IN SAME CLLASS OR STRUCT
~ASSESSED BY CLASS ONLY
*hint: “curly brace boundary”
PRIVATE PROTECTED ACCESS MODIFIERS
CAN BE ACCESSED BY TYPES FROM CLASS THAT ARE DECLARED WITHIN CONTAINING ASSEMBLY
PROTECTED ACCESS MODIFIER
TYPE OR MEMBER CAN BE ACCESSED ONLY BY THE CODE IN THE SAME CLASS, OR IN A CLASS DERIVED FROM THE CLASS CAN BE ACCESSED BY THE FOLLOWING:
~CLASS
~CHILD WITHIN ASSEMBLY
~ONLY CHILD IN & OUTSIDE ASSEMBLY
USED COMMONLY WITH INHERITANCE
PROTECTED INTERNAL ACCESSE MODIFIERS
~CAN BE ACCESSED BY ANY CODE IN THE ASSEMBLY IN WHICH ITS DECLARED, OR FROM WITHIN DERIVED CLASS IN ANOTHER ASSEMBLY
~CAN BE ACCESSED OUTSIDE ASSEMBLY ONLY IN CHILD CLASS
INTERNAL ACCESS MODIFIER
~TYPE OR MEMBER CAN BE ACCESSED BY ANY CODE IN THE SAME ASSEMBLY BUT NOT FROM ANOTHER ASSEMBLY; CODE FROM A PART OF THE SAME COMPILATION
~ACCESSED BY NON-DERIVED CLASS IN SAME ASSEMBLY
sn: Kinda private; scope is entire object declared in
DEFINE: ACCESS MODIFIERS
~PERMISSION LEVELS OTHER CLASSES HAVE TO THIS METHOD, CLASS OR VARIABLE;
~METHODS AND VARIABLES SUPPORT ALL ACCESS MODIFIERS
~WHEN YOU DECLARE A VARIABLE OR METHOD YOU SPECIFY THE MODIFIER IN FRONT
~CLASSES ONLY SUPPORT PUBLIC OR DEFAULT LEVELS OF ACCESS
sn: common pattern to create Private variables and Public accessor methods
STATIC MODIFIER
to declare a static member, which belongs to the type itself rather than to a specific object.
~can be used with classes, fields, methods, properties, operators, events, and constructors, but it cannot be used with indexers, finalizers, or types other than classes.
A CONSTANT
or type declaration is implicitly a static member.
CONSTRUCTOR
is used to initialize the state of an object.
must not have a return type.
is invoked implicitly.
The Java compiler provides a default constructor if you don’t have any constructor in a class.
The constructor name must be same as the class name.
Advantages of OOPs over Procedure Oriented Programming
~OOPs makes development and maintenance easier whereas in a procedure-oriented programming language it is not easy to manage if code grows as project size increases.
~OOPs provides data hiding whereas in a procedure-oriented programming language a global data can be accessed from anywhere.
Collections in C#
~classes are specialized classes for data storage and retrieval. These classes provide support for stacks, queues, lists, and hash tables. Most collection classes implement the same interfaces. ~classes serve various purposes, such as allocating memory dynamically to elements and accessing a list of items on the basis of an index etc. These classes create collections of objects of the Object class, which is the base class for all data types in C#.
System.Collection namespace
ArrayList
HashTable
Stack
Queue
NULLABLE
~MAKES DATATYPES OPTIONAL
~DENOTES BY USING ‘?’
~ENABLES OTION FOR DATATYPE TO HOLD A NULL VALUE
~USEFUL FOR COMPILER TO HELP OUT, CONSIDER GIVING A NULL…
bool? x -means ‘x’ can hold true, false, or null value
int? y -means ‘y’ can hold numbers or null
string? i- ‘i’ can hold characters or null
NON ACCESS MODIFIERS
remember....V.A.R.O.S. ~VIRTUAL ~ABSTRACT ~READONLY ~OVERRIDE ~STATIC
REFERENCE VARIABLE
~USE A CLASS AS A TYPE THEY CAN HOLD THE ADDRESS OF ANY OBJECT THAT IS AN INSTANCE OF THAT CLASS
Dog myDog= new Dog( );
~OBJECTS ARE STORED IN HEAP MEMORY & THEIR ADDESSES ARE STORED IN…..?
~SOMEWHAT LIKE A MASK OVER OBJECTS CODE
ex: Polymorphism
VALUE TYPES
~STORE IN DIRECT VALUE;
~STORED IN MEMORY STACK
~STACK IS ALWAYS FASTER TO RETRIEVE DATA THAN HEAP
~SET OF MEMORY SET ASIDE FOR IT TO OCCUPY
ex predefined: (int, long, short, byte, data type, char)
structs - like a class but gets stored in the stack for memory retrieval efficiency
enums - defines a set of named integral constants
REFERENCE TYPES
~DATATYPES STORED IN THE HEAP & REFERENCE VARIABLES STORED IN THE STACK
~THE ACTUAL OBJECT ITSELF IS STORED IN THE HEAP
~RETRIEVING A VALUE FROM HEAP IS AN EXPENSIVE PROCESS
~HEAP IS DYNAMICALLY CHANGING
ex predefined: string, arrays, collections, classes, interfaces, delegates
SIGNIFICANCE OF OVERRIDING
ALLOWS FOR THE CHILD CLASSES TO PROVIDE NEW IMPLEMENTATION FOR THEIR PARENT FUNCTIONALITY
hint: useful for polymorphic references
METHOD OVERLOADING
~CREATION OF METHODS WITH THE SAME NAME AS ANOTHER IN THE SAME CLASSBUT DIFFERS IN PARAMETERS (order number, type)
~EXAMPLE OFSTATIC/ COMPILE TIME POLYMORPHISM;
~METHOD WITH SAME NAME BEHAVES DIFFERENTLY BASED ON SIGNATURES(parameters)
Signatures can be different in 3 ways:
Number of parameters.
Datatype of parameters.
Sequence of parameters.
OVERRIDE METHODS or METHOD OVERRIDING
~METHOD OVERRIDING IS AN EXAMPLE OF RUNTIME POLYMORPHISM; WHICH IS REDEFINIG THE METHOD OF THE PARENT CLASS INTO CHILD CLASS
~TO OVERRIDE A METHOD YOU MUST FIRST HAVE A METHOD ON A PARENT CLASS. IN THE CHILD CLASS YOU NEED TO SPECIFY A METHOD OF THE SAME SIGNATURE
~It is also known as dynamic/runtime polymorphism. Redefining the method of base class in child class.
~necessary to make a method overridable by using abstract or virtual keyword in base class
In child class use the keyword override to override these methods.
OBJECT ORIENTED PROGRAMMING (OOP)
~PROGRAMMING PARADIGGM TO MAKE PROGRAMS BETTER
~ORGANIZED INTO CLASSES ACCESSED BY OBJECTS
suited for large complex projects ; actively updated
~BENEFITS: READABILITY, REUSABILITY (inheritance), SCALABILITY, & EFFICIENCY
~STRUCTUE “building blocks”: CLASSES, OBJECTS, METHODS, ATTRIBUTES
~CLASSES w/ ATTRIBUTES/ PROPERTY
~TYPES w/ BEHAVIORS AND OPERATIONS (methods)
~OBJECTS ARE AN INSTANCE OF A CLASS, ALLOCATED MEMORY
~WHEN CLASS INSTANTIATED OBJECTS COMES TO LIFE
~ATTRIBUTES CAN BE STATIC OR DYNAMIC
WHAT ARE THE PILLARS
"A PIE" ~ABSTRACTION ~POLYMORPHISM ~INHERITANCE ~ENCAPSULATION
ABSTRACTION
occurs when you simplify a complex thought or complex system
~like a template
~shows only essential features of the program instead of unnecessary details
~in c# it is achieved by “abstract” classes and interfaces
hint ex: “tax system” … the black box is the functionality
ENCAPSULATION
“WRAPPING OF DATA”
~methods enclose data field
~class/type encloses a method
~namespace encapsulates the type
“DATA HIDING”
~provide protection from outside members/world
-use access modifiers to provide levels of access
INHERITANCE
~GIVE ACCESS TO THE VARIABLES AND METHODS OF ONE CLASS TO ANOTHER
(extends a type so its properties & behaviors can be extended/branched further)
~Child and Parent relationship “is-a(n)” relationship
~a class can only extend one other class
~Single = A>B ~Multi-Level = A>B>C ~Hierarchal = "FAMILY TREE" ~Multiple Inheritance (A,B...) >C ~2 or more parents ~Hybrid (all of the above)
C# doesn’t support MULTIPLE OR HYBRID INHERITANCE