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