Generic Function to convert string to Enum

private T GetEnumFromString<T>(string value)
  {
     try
        {
           T t;
           t = (T)Enum.Parse(typeof(T), value);
           return t;
        }
     catch (Exception E)
        {
           return (T)((object)0);
        }
   }

The Heartbleed Bug (CVE-2014-0160)

Neel Metha from Google Security discovered incorrect memory handling in Open TLS Heartbeat extension. By which attacker can access upto 64K of memory of client or server and can expose Private key and other secret data.
Affected users should upgrade to OpenSSL 1.0.1g. Users unable to immediately upgrade can alternatively recompile OpenSSL with -DOPENSSL_NO_HEARTBEATS.



For complete details, please visit references

References

Heartbleed
Ubuntu / Security Notice USN-2165-1
OpenSSL Security Advisory (published 7th of April 2014, ~17:30 UTC)

Dock panel like animated buttons in WPF(XAML)

Like dockpanel buttons(Buttons which grows up as mouse enters on button) in windows, we can have the buttons in WPF without writing any C# or VB code. This can be achieved by using EventTriggers & DoubleAnimations.


As mentioned in above image as mouse enters on Recycle Bin icon, it grows up, similar thing can be achieved in wpf, sample XAML is written below.

Code:

Deferred Execution in LINQ

Deferred Execution of Query

  class Sample
    {
      public int i;
      public String str;
    }

  static void Main(string[] args)
    {
      List lst = new List();
      lst.Add(new Sample() { i = 1, str = "String1" });
      lst.Add(new Sample() { i = 2, str = "String2" });
      lst.Add(new Sample() { i = 3, str = "String3" });
      lst.Add(new Sample() { i = 4, str = "String4" });
      int a = 1;
      IEnumerable lStr = (from l in lst
                          where l.i == a
                          select l);  //It will just store query in lStr variable.
     }

When we write any LINQ query as mentioned, it doesn't get executed immediately. It actually stores  query in the variable and every time we use/enumerate the variable it executes the query and use the result. It is called deferred execution.

    Sample s = lStr.First();  ///Query will be executed here
    Console.WriteLine(s.str);

So that whenever filter variable(a) changes prior to the use of object(lStr), output will automatically changes.

Factory Design pattern

Factory design pattern provides you an interface creating a single object, but let subclass decide which class to instantiate. This pattern is used most frequently when we are having common functionality with different methods for different objects.

In that scenario, we will create an interface with defining common functionality which will be implemented by the classes. A class will be defined which will be used to identify which class to be instantiated for the object.

For example, We have to create reading utility and we are having 3 different kind of files 1st is pdf, 2nd is doc and 3rd is txt file. On all the files we have to perform open operation but way to open the file will be different. In this case we will create an Interface will contain OpenFile Operation. There will be classes for each file type and the classes will be implementing the factory interface(Example is given in C# Code Example.

C# Code Example:

TargetInvocationException was unhandled "Exception has been thrown by the target of an invocation." issue in WPF

In WPF, most probably the issue occurs when we are accessing null object in the constructor of the code file of the XAML or If you are invoking "PropertyChanged" event without having null check inside the constructor.



Example 1:

"if" condition inside Select Clause of LINQ Query.

In LINQ you can't use "if" statement directly in select clause, but you can achieve this by using ternary operator.

Consider you are querying an Array of object & the object internally contains another object. We have to select some field from inner object. But it may be chance that for some cases inner object is null. So to prevent your query from throwing "Object Reference not set to instance of object exception.", you need to check for null object in select clause.

Code:

class Periods
        {
            public int StartYear;
            public int EndYear;
        }



class student
        {
            public int RollNo;
            public string name;
            public DateTime DOB;
            public Periods coursePeriod;
        }



//There is an array of student object which internally contains Periods object.

student[] arrStudents= new student[100];

// You have to fetch list of students and time period spend in college
//code to fill arrStudents Array.
//output object
class studentCousePeriod
        {
            public int Rollno;
            public int StartYear;
            public int EndYear;
        }

LINQ :