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