Monday, September 9, 2024
Monday, August 19, 2024
Class/Record/Struct
Posted by Zuhair Ahmed on August 19, 2024 with No comments
Feature | Class | Record | Struct |
---|---|---|---|
Type | Reference Type | Reference Type (since C# 10, can be record struct ) | Value Type |
Memory Allocation | Typically allocated on the heap. | Typically allocated on the heap. | Typically allocated on the stack (or heap if used as part of a class or array) |
Default Constructor | Can have a parameterless constructor, and can be explicitly defined. | Implicit parameterless constructor is not allowed; parameters are required. | Parameterless constructor is provided by default and initializes all fields to default values. |
Inheritance | Supports inheritance (can inherit from other classes and be inherited). | Supports inheritance (can inherit from other records but not from classes). | Does not support inheritance (cannot inherit from or be inherited by other types). |
Equality | Uses reference equality by default, can override Equals and GetHashCode for value-based equality. | Uses value-based equality by default (based on all properties). Automatically implements Equals , GetHashCode , and ToString . | Uses value-based equality by default (based on all fields). Automatically implements Equals , GetHashCode , and ToString . |
Mutability | Mutable by default; properties can be changed after object creation. | Immutable by default; properties are readonly and set via constructor. | Immutable by default (properties are typically readonly and initialized in the constructor). |
Deconstruction | Not supported by default. | Supports deconstruction with tuple-like syntax. | Supports deconstruction with tuple-like syntax. |
Constructor | Can have multiple constructors with various parameters. | Primary constructor is defined with positional parameters; can also have additional constructors. | Constructor must initialize all fields; no parameterless constructor allowed unless provided by the compiler. |
Default Values | Fields initialized to default values if not explicitly set. | Properties are initialized via the constructor. | Fields are initialized to default values (e.g., 0 , null ). |
Use Cases | Used for complex objects with behavior and mutable state. | Ideal for immutable data models, DTOs, and scenarios where value-based equality is important. | Ideal for lightweight, immutable data structures where copying and stack allocation are beneficial. |
Friday, May 17, 2024
Angular Basics
Posted by Zuhair Ahmed on May 17, 2024 with No comments
Intro
• package.json
• (list
of dependencies)
• package-lock.json
• (details
list of that dependencies)
• angular.json
• (map
everything here: assets, scripts etc)
• tsconfig.json
• (typescript
configuration file)
• polyfills.ts
• (compatibility
file)
Components
• Modules
must be declared in the imports
• Components
must be declared in the declarations
• Standalone
components are not dependent in any modules and it must be declared in imports
• and Pipes are doing the transformation over text
Bindings - Two Way, Property, Event
Routing
• Basic
Routing
• Routes – for mapping path
& component
• routerLink – navigate to
different routes
• router-outlet– for displaying
matched route
• Child
routing
• Activated
Route (const routeId = route.snapshot.paramMap.get(‘id’);)
• Module
Routing
• const
routes : Routes
• Lazy
Loading
• Load when required (loadChildren then import)
Directives
• Add
additional behavior to elements
• Components
works as a directive
• Attribute
Directive
ngClass (Binding
Classes)
ngStyle (Binding
Styles, Only accept objects s = {})
ngModel (Two Way
Binding)
• Structural
Directive
*ngIf
*ngFor
*ngSwitch
Services
• Defined
with @Injectable
• All
are in observable format and without subscribe it won’t
work (Rxjs feature)
FormType
• TDF
–
handle in html file
• ngForm
• Reactive
–
handle in ts file
• FormGroup
Guard
• used
to control access in particular route
• canActivate
• canActivateChild
• canDeactivate
• canLoad
Interceptor
• Used
for token login
• Declares in app.module [Providers]
Rxjs
• Observable and Operators
• Promise (then, then, finally)
Monday, March 4, 2024
Reactive Programming
Posted by Zuhair Ahmed on March 04, 2024 with No comments
🔎Definition---
Pending: Initial state, neither fulfilled nor rejected.
Fulfilled: The operation completed successfully.
Rejected: The operation failed.
On the other hand, RxJS (Reactive Extensions for JavaScript) is a library for reactive programming using observables. An observable represents a stream of data that can be observed over time. It provides powerful tools for handling asynchronous and event-based programming.
🔩Conversion---
Converting a Promise to an Observable is a common operation in RxJS when integrating with libraries or APIs that return Promises. RxJS provides the
from
function to convert various data sources, including Promises, into Observables. Converting an Observable to a Promise is useful when you need to work with Promise-based APIs or when you want to consume Observables in a Promise-based context. RxJS provides the
toPromise()
operator to convert an Observable into a Promise.🔚Conclusion---
Promises are simpler and more straightforward for handling single asynchronous operations that produce a single value or error.
Observables are more powerful and suitable for handling multiple asynchronous values over time, supporting cancellation, and offering a wide range of operators for transforming and combining streams of data. When you promise you can't cancel and for cancelling you have to use another library. But you can Unsubscribe to cancel in observables. Also you have to unsubscribe every time that means writing same code couple of times when working on a big project but in Promises it will go on rejected state.
Posted in rxjs