Saturday, October 11, 2025

🧱 Understanding the Core Building Blocks of Angular

 

🌟 Introduction

Angular is one of the most powerful front-end frameworks used for developing modern single-page applications (SPAs). What makes Angular unique is its structured architecture, which is built upon several key building blocks.

In this article, we’ll explore these core Angular building blocks—Modules, Components, Templates, Directives, Services, Pipes, and Routing—with clear explanations and examples to help you understand how they all work together.


1. 🧩 Modules (NgModule)

Modules are the organizational units of an Angular application. They group related components, directives, pipes, and services together into a cohesive block of functionality.

Every Angular app has at least one root module called AppModule, defined in the app.module.ts file.

✅ Example:

@NgModule({ declarations: [AppComponent, HomeComponent], imports: [BrowserModule, FormsModule], bootstrap: [AppComponent] }) export class AppModule { }

πŸ’‘ Purpose:

  • Organizes code into functional areas.

  • Supports lazy loading for better performance.

  • Simplifies dependency management.


2. ⚙️ Components

Components are the heart of Angular applications. Each component controls a specific section of the UI and defines its behavior and appearance.

A component is made up of:

  • HTML Template (View)

  • TypeScript Class (Logic)

  • CSS/SCSS Styles (Design)

✅ Example:

@Component({ selector: 'app-hello', template: `<h1>Hello {{name}}!</h1>`, styles: [`h1 { color: blue; }`] }) export class HelloComponent { name = 'Angular'; }

πŸ’‘ Purpose:

  • Defines the UI logic and view.

  • Reusable across the application.

  • Connects data and behavior using data binding.


3. 🧾 Templates

Templates define what the user sees in the browser. They combine HTML with Angular directives, pipes, and binding expressions.

✅ Example:

<div *ngIf="isLoggedIn"> Welcome, {{ userName | uppercase }} </div>

πŸ’‘ Purpose:

  • Defines the layout and structure of the component.

  • Uses data binding ({{ }}) and directives (*ngIf, *ngFor) to make the view dynamic.


4. 🧭 Directives

Directives are used to add behavior or modify DOM elements in the template.

🧱 Types of Directives:

  1. Structural Directives – Change the structure of the DOM (*ngIf, *ngFor)

  2. Attribute Directives – Change the appearance or behavior of elements ([ngClass], [ngStyle])

✅ Example:

<p *ngIf="showMessage">Hello Angular!</p> <button [disabled]="!isActive">Click Me</button>

πŸ’‘ Purpose:

  • Enhance HTML elements dynamically.

  • Add custom interactive behaviors.


5. 🧠 Services & Dependency Injection

Services are used to share data or logic across multiple components. They are the backbone of business logic in Angular.

They often handle tasks like API calls, data fetching, and application-wide state management.

✅ Example:

@Injectable({ providedIn: 'root' }) export class DataService { getData() { return ['Apple', 'Banana', 'Cherry']; } }

πŸ’‘ Purpose:

  • Promote code reusability.

  • Keep components lightweight.

  • Implement Dependency Injection (DI) for efficiency.


6. 🌐 Routing

Routing enables navigation between different views or pages without reloading the entire application. It’s what makes Angular apps behave like single-page applications.

✅ Example:

const routes: Routes = [ { path: 'home', component: HomeComponent }, { path: 'about', component: AboutComponent } ];

πŸ’‘ Purpose:

  • Manages navigation within the app.

  • Supports lazy loading and guarding routes for security.


7. πŸ”„ Pipes

Pipes are used to transform data before displaying it in the template. Angular provides several built-in pipes such as uppercase, date, and currency. You can also create custom pipes.

✅ Example:

<p>{{ today | date:'fullDate' }}</p>

πŸ’‘ Purpose:

  • Simplify data formatting in templates.

  • Reusable and easy to integrate.


πŸ“‹ Summary Table

Building BlockDescriptionExample
ModuleOrganizes the application into logical unitsAppModule
ComponentDefines UI and logicHomeComponent
TemplateDefines the view’s HTML<h1>{{title}}</h1>
DirectiveAdds behavior to elements*ngFor, *ngIf
ServiceShares data or logicDataService
RoutingManages navigation/home, /about
PipeFormats or transforms data`{{name

🎯 Conclusion

Angular’s building blocks work together to create a powerful, maintainable, and scalable application structure.
By mastering Modules, Components, Templates, Directives, Services, Pipes, and Routing, developers can build high-performing web applications with ease and flexibility.

Whether you’re a beginner or an experienced developer, understanding these building blocks is the first step toward becoming an Angular expert

Thursday, October 9, 2025

🧠 What is Generics in C#? Complete Guide with Examples

 πŸ“˜ Introduction

In C#, Generics are one of the most powerful features introduced with the .NET Framework 2.0.
They allow you to define classes, methods, interfaces, and collections that can work with any data type — while still maintaining type safety and performance.

In simple words, Generics help you write code once and use it with any data type.


⚙️ What Are Generics?

Generics let you define a placeholder type (called a type parameter) that will be replaced with a specific type when you create an instance of a class or call a method.

This means you can write:

List<int> List<string> List<Employee>

—all using the same generic List<T> class.


πŸ’‘ Example Without Generics

public class Calculator { public int Add(int a, int b) { return a + b; } }

If you want to add float or double values, you’ll have to write multiple methods for each data type.
This leads to code duplication and poor reusability.


⚙️ Example With Generics

public class Calculator<T> { public T Add(T a, T b) { dynamic x = a; dynamic y = b; return x + y; } } class Program { static void Main() { Calculator<int> intCalc = new Calculator<int>(); Console.WriteLine("Sum (int): " + intCalc.Add(10, 20)); Calculator<double> doubleCalc = new Calculator<double>(); Console.WriteLine("Sum (double): " + doubleCalc.Add(10.5, 20.7)); } }

Output

Sum (int): 30 Sum (double): 31.2

Here, T is a type parameter. It allows the Calculator class to operate with any data type (int, double, string, etc.).


🧱 Generic Method Example

You can also create generic methods inside normal classes.

public class DisplayData { public void ShowData<T>(T data) { Console.WriteLine("Value: " + data); } } class Program { static void Main() { DisplayData obj = new DisplayData(); obj.ShowData<int>(101); obj.ShowData<string>("Hello Generics"); } }

Output

Value: 101 Value: Hello Generics

Here, the method ShowData<T>() can accept any data type without overloading.


πŸ“š Generic Collections in C#

C# provides several built-in generic collections inside the System.Collections.Generic namespace.
These are type-safe and more efficient than old non-generic collections like ArrayList or Hashtable.

Generic CollectionDescription
List<T>Dynamic array of type T
Dictionary<TKey, TValue>Key-value pair collection
Queue<T>FIFO (First-In-First-Out) collection
Stack<T>LIFO (Last-In-First-Out) collection

Example:

List<string> names = new List<string>(); names.Add("Hasitha"); names.Add("Cherry"); foreach (var name in names) { Console.WriteLine(name); }

Output

Hasitha Cherry

🧩 Benefits of Generics in C#

BenefitDescription
Type SafetyPrevents type mismatches at compile time
PerformanceAvoids boxing/unboxing for value types
ReusabilityWrite code once, use with any type
MaintainabilityCleaner and less repetitive code

πŸ”„ Generic Constraints (Optional but Powerful)

Sometimes, you might want to restrict which data types can be used with your generic class.

Example:

public class DataManager<T> where T : class { public void Display(T data) { Console.WriteLine("Data: " + data); } }

Here, where T : class means only reference types can be used.


πŸš€ Conclusion

Generics in C# make your code reusable, efficient, and type-safe.
They help developers build scalable and maintainable applications, especially in large projects using .NET Core or ASP.NET.

Don't Copy

Protected by Copyscape Online Plagiarism Checker

Pages