Sunday, December 24, 2017

Angular

Pre-requisite
1. Have npm, node installed.

Important facts
Components are the fundamental building blocks of Angular applications.They display data on the screen, listen for user input, and take action based on that input.

The ng serve command builds the app, starts the development server, watches the source files, and rebuilds the app as you make changes to those files.

You'll find the implementation of the shell AppComponent distributed over three files:
  1. app.component.ts— the component class code, written in TypeScript.
  2. app.component.html— the component template, written in HTML.
  3. app.component.css— the component's private CSS styles.

Sunday, November 19, 2017

Mutable vs Immutable

Mutable means can change and Immutable means can not change

string is immutable means whenver we assign a new value, a new string object is created in memory.


Stringbuilder is immutable, we can modify using different methods like append etc.

Random

Composition vs Inheritance - Inheritance shows -is-a relation. Composition shows - has-a relation.
extension methods in c#
  1. An extension method is defined as static method but it is called like as instance method.
  2. An extension method first parameter specifies the type of the extended object, and it is preceded by the "this" keyword.
  3. An extension method having the same name and signature like as an instance method will never be called since it has low priority than instance method.
  4. An extension method can't override the existing instance methods.
  5. An extension method cannot be used with fields, properties or events.
  6. The compiler doesn't cause an error if two extension methods with same name and signature are defined in two different namespaces and these namespaces are included in same class file using directives. Compiler will cause an error if you will try to call one of them extension method.
  1. //defining extension method
  2. public static class MyExtensions
  3. {
  4. public static int WordCount(this String str)
  5. {
  6. return str.Split(new char[] { ' ', '.', ',' }).Length;
  7. }
  8. }
  9.  
  10. class Program
  11. {
  12. public static void Main()
  13. {
  14. string s = "Dot Net Tricks Extension Method Example";
  15. //calling extension method
  16. int i = s.WordCount();
  17.  
  18. Console.WriteLine(i);
  19. }
  20. }
Chaining of extension method

  1. public static class ExtensionClass
  2. {
  3. public static string Pluralize (this string s) {...}
  4. public static string Capitalize (this string s) {...}
  5. }
  6. //we can do chainig of above methods like as
  7. string x = "Products".Pluralize().Capitalize();

Removing ambiguity

  1. static class StringExtension
  2. { // first method
  3. public static bool IsCapitalized (this string s) {...}
  4. }
  5. static class ObjectExtension
  6. {
  7. // second method
  8. public static bool IsCapitalized (this object s) {...}
  9. }
  10. // code here
  11. // first method is called
  12. bool flag1 = "Dotnet-Tricks".IsCapitalized();
  13. // second method is called
  14. bool test2 = (ObjectHelper.IsCapitalized ("Dotnet-Tricks"));

Javascript

- If we don't put var keyword variable becomes global.
- If we write  "use strict " , variable must be declared before use
- JavaScript can pull and reorganize variable declaration (javascript hoisting)
  alert(x);
var x=10;
above two lines throws undefined exception

When To Use IEnumerable, ICollection, IList And List

When To Use IEnumerable, ICollection, IList And List


IEnumerable, ICollection, IList



InterfaceScenario
IEnumerable, IEnumerable<T>The only thing you want is to iterate over the elements in a collection. You only need read-only access to that collection.
ICollection, ICollection<T>You want to modify the collection or you care about its size.
IList, IList<T>
You want to modify the collection and you care about the ordering and / or positioning of the elements in the collection.
..

Saturday, November 18, 2017

SOLID Principles


SOLID is 5 basic principle that helps us building good software architecture.

S -  SRP : Single Responsibility Principle
O - OCP : Open Closed Principle
L -  LSP : Liskov Substitution Principle
I  -  ISP  : Interface Segregation Principle  

Tuesday, September 12, 2017

Multi-threading & Task Parallel Library - II

How Threading works


  • Multi threading is managed by thread scheduler, a function CLR delegates to operating system.
  • On a single-processor computer, a thread scheduler performs time-slicing — rapidly switching execution between each of the active threads.
  • On a multi-processor computer, multi-threading is implemented with a mixture of time-slicing and genuine concurrency, where different threads run code simultaneously on different CPUs. It’s almost certain there will still be some time-slicing, because of the operating system’s need to service its own threads — as well as those of other applications.
  • A thread is said to be preempted when its execution is interrupted due to an external factor such as time-slicing. In most situations, a thread has no control over when and where it’s preempted.
  • thread is the basic unit to which the operating system allocates processor time.
  • process, in the simplest terms, is an executing program which provides resource to running thread.
Different ways to cretate thread and invoke it.

Using Delegate
Public delegate void Threadstart()
Thread t = new Thread(new Threadstart(MethodName())
t.start();

Directly via Method name
Thread t = new Thread( MethodName())
t.start();

Using Lambda Expression

Thread t = new Thread ( () => Console.WriteLine ("Hello!") );
 t.Start();




Multi-threading & Task Parallel Library - I


Introduction
  • A thread is an independent execution path, able to run simultaneously with other threads. 
  • Once started, a thread’s IsAlive property returns true, until the point where the thread ends. 
  • A thread ends when the delegate passed to the Thread’s constructor finishes executing. Once ended, a thread cannot restart.
  • The CLR assigns each thread its own memory stack so that local variables are kept separate. 
  • Separate copy of the cycles variable is created on each thread's memory stack.

class ThreadTest
{
  static void Main()
  {
    Thread t = new Thread (WriteY);          // Kick off a new thread
    t.Start();                               // running WriteY()
 
    // Simultaneously, do something on the main thread.
    for (int i = 0; i < 1000; i++) Console.Write ("x");
  }
 
  static void WriteY()
  {
    for (int i = 0; i < 1000; i++) Console.Write ("y");
  }
}

class ThreadTest
{
  bool done;
 
  static void Main()
  {
    ThreadTest tt = new ThreadTest();   // Create a common instance
    new Thread (tt.Go).Start();
    tt.Go();
  }
 
  // Note that Go is now an instance method
  void Go() 
  {
     if (!done) { done = true; Console.WriteLine ("Done"); }
  }
}
Main thread and Child thread can share data by using static variable or invoking method using object as in above example.
Since multiple thread if trying to do same operation or change same variable, there is possibility of conflicts , to avoid that we obtaining exclusive lock.
class ThreadSafe 
{
  static bool done;
  static readonly object locker = new object();
 
  static void Main()
  {
    new Thread (Go).Start();
    Go();
  }
 
  static void Go()
  {
    lock (locker)
    {
      if (!done) { Console.WriteLine ("Done"); done = true; }
    }
  }
}
A thread, while blocked, doesn't consume CPU resources.
Using JOIN we can force a thread to wit until other completes.
static void Main()
{
  Thread t = new Thread (Go);
  t.Start();
  t.Join();
  Console.WriteLine ("Thread t has ended!");
}
 
static void Go()
{
  for (int i = 0; i < 1000; i++) Console.Write ("y");
}
Thread.Sleep(0) relinquishes the thread’s current time slice immediately, voluntarily handing over the CPU to other threads. Framework 4.0’s new Thread.Yield() method does the same thing — except that it relinquishes only to threads running on the same processor.
If inserting Thread.Yield() anywhere in your code makes or breaks the program, you almost certainly have a bug.