Extension Methods in C#

0

Category :

Using extension methods, you can use your new methods as a part of int/any class in .NET.

An extension method is a special kind of static method that allows you to add new methods to existing types without creating derived types.

The extension methods are called as if they were instance methods from the extended type, For example: x is an object from int class and we called it as an instance method in int class.

How to Create my Extension Method?

Simply, you create your own static method in your class and put this keyword in front of the first parameter in this method (the type that will be extended).
public static class MyExtension 
{ 
    public static int factorial(this int x) 
    { 
        if (x <= 1) return 1; 
        if (x == 2) return 2; 
        else 
            return x * factorial(x - 1); 
    }

    public static IEnumerable> Combinations(this IEnumerable elements, int k)
    {
        return k == 0 ? new[] { new T[0] } :
            elements.SelectMany((e, i) =>
            elements.Skip(i + 1).Combinations(k - 1).Select(c => (new[] { e }).Concat(c)));
    }
}
Usage
int x = 3;
x.factorial();
Combinations Usage

List td = new List();
td.Add(new TransportDetails() { MaxPax = 8, Price = decimal.Parse("88.04") });
td.Add(new TransportDetails() { MaxPax = 20, Price = decimal.Parse("128.70") });
td.Add(new TransportDetails() { MaxPax = 31, Price = decimal.Parse("161.37") });

var res = td.Combinations(2);

General Tips in Extension Methods Usage

  1. This keyword has to be the first parameter in the extension method parameter list.
  2. It is important to note that extension methods can't access the private methods in the extended type.
  3. If you want to add new methods to a type, you don't have the source code for it, the ideal solution is to use and implement extension methods of that type.
  4. If you create extension methods that have the same signature methods inside the type you are extending, then the extension methods will never be called.
my thanks to:
http://www.codeproject.com/Articles/34209/Extension-Methods-in-C
http://msdn.microsoft.com/en-us/library/bb383977.aspx
http://www.blackwasp.co.uk/ExtensionMethods.aspx
http://www.extensionmethod.net/
101-LINQ-Samples

Using struct and class - what's that all about?

0

Category :

Class allocates memory on the stack to hold a reference to a MyClass instance in future, it does not create an instance of MyClass, and you will have seen that before - when you try to use any property or method of a class and you get a "Object reference not set to an instance of an object" exception and your program crashes, it's because you created a variable, but didn't create or assign an actual instance to the variable.

Struct what this does is create a new instance of MyClass on the heap, and assign the reference to it to the variable mc. This is important, because the stack and the heap are different "types" of memory: the heap is a big "lump" of memory which is sorted out by the Garbage collector and all classes, methods and threads share it. The stack on the other hand is specific to a thread, and everything on the stack is discarded when you exit the method - which means that the mc variable is lost, but the data it references is not - if you have copied the reference to a variable outside the method, then you can still access it from the rest of your program.

When to use a Struct

CONSIDER defining a struct instead of a class if instances of the type are small and commonly short-lived or are commonly embedded in other objects.

AVOID defining a struct unless the type has all of the following characteristics:
  1. It logically represents a single value, similar to primitive types (int, double, etc.).
  2. It has an instance size under 16 bytes.
  3. It is immutable.
  4. It will not have to be boxed frequently.

http://msdn.microsoft.com/en-us/library/ms229017%28v=vs.110%29.aspx
my thanks to:
http://www.codeproject.com/Articles/728836/Using-struct-and-class-whats-that-all-about

Pass Collection to Function or Procedure

0

Category :


IF EXISTS (SELECT * FROM sys.types WHERE is_table_type = 1 AND name = 'TestPassList')
 DROP TYPE TestPassList
GO

CREATE TYPE dbo.TestPassList
AS TABLE
(
 BookingIdMaster INT,
 BookingId  INT
);
GO


CREATE PROCEDURE sp_TestPassList
  @List AS dbo.TestPassList READONLY
AS
BEGIN
 SELECT *
  FROM @List

END
GO


IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'fn_TestPassList') AND type in (N'P', N'TF'))
 DROP FUNCTION fn_TestPassList
GO

CREATE FUNCTION fn_TestPassList(@List AS dbo.TestPassList READONLY)
RETURNS @temptable 
 TABLE (  
   BookingId INT
 )       
AS 
BEGIN 
  
 INSERT INTO @temptable    
  SELECT BookingId
  FROM @List
 
 RETURN      
END
GO

----------- test
DECLARE @TestPassListVar dbo.TestPassList
INSERT INTO @TestPassListVar 
SELECT TOP 10 billing_id Booking_IdMaster,
    Booking_Id
 FROM d_booking
 
SELECT * 
 FROM fn_TestPassList(@TestPassListVar)
 

SOLID: Part 4 - The Dependency Inversion Principle

0

Category :

Definition

  • A. High-level modules should not depend on low-level modules. Both should depend on abstractions.
  • B. Abstractions should not depend upon details. Details should depend upon abstractions.


my thanks to:
http://code.tutsplus.com/tutorials/solid-part-4-the-dependency-inversion-principle--net-36872

Pivot - SQL Server

0

Category :

“Pivot is a sql server operator that can be used to turn unique values from one column, into multiple columns in the output, there by effectively rotating a table”. Column values are converted into column names

Read Pivot Statement

We are performing this aggregate function on the 1st column for 2nd column for column in “in”



Represented in cross tab format…

my thanks to: http://www.youtube.com/watch?v=h3BtudZehuo

SOLID: Part 3 – Liskov Substitution & Interface Segregation Principles

0

Category : ,

http://net.tutsplus.com/tutorials/php/solid-part-3-liskov-substitution-interface-segregation-principles/

Subtypes must be substitutable for their base types.

SOLID: Part 2 – The Open/Closed Principle

0

Category : ,

To design our modules, classes and functions in a way that when a new functionality is needed, we should not modify our existing code but rather write new code that will be used by existing code.


http://net.tutsplus.com/tutorials/php/solid-part-2-the-openclosed-principle