Posts

Showing posts from August, 2015

Overriding GetHashCode , Equals methods.

While browsing through questions of C# ,  I came across this question If A.equals(B) is true then A.getHashcode & B.getHashCode must always return same hash code I found this interesting example in the site  --https://vkreynin.wordpress.com/2008/07/05/explaining-gethashcode-method/ Which explains this questions . A hash code is a numeric value that is used to insert and identify an object in a hash-based  collection such as the Dictionary<TKey, TValue> class, the Hashtable class, or a  type derived from the DictionaryBase class. The GetHashCode method provides this  hash code for algorithms that need quick checks of object equality.   Two objects that are equal return hash codes that are equal. However, the reverse is not true: equal hash codes do not imply object equality, because different (unequal) objects can have identical hash codes.     using System; public class Point { private readonly int _x; private readonly int _y;

Async and Await

Asynchronous programming Basics and Understanding -- Thanks http://naweenblogs.blogspot.in/2014/04/async-and-await-actions-in-aspnet-mvc-4.html for the wonderful explanation. ASP.NET has a pool of threads to service requests, when a new request comes a new thread is picked from the thread pool to service the request. Now this thread that is selected to serve the request cannot serve any other request until the initial request completes. And if this request has a high latency, such as a network operation, then the thread is also blocked until the request finishes execution. So assuming that there are a lot of concurrent high latency calls majority of the threads will be just blocked waiting for the request to finish. So as majority of the threads are just waiting and not doing any processing most of the time, the threads can be better utilized by using asynchronous programming. In the .NET 4.5 the default maximum size of the thread pool is 5, 000 threads. Thoug

Weak Event Pattern

Weak Event Pattern Incorrect usage of Event can be a reason for Memory leak in c#.Net Application.An event(publisher/Source) keeps a strong reference to its listerner (subscriber/Destination Handler) objects through delegates. This prevents the Garbaggage Collector from freeing up the memory occupied by the listerner when they are not being used.This is perticularly true for the static events which will be present till the lifetime of the application along with the reference to the listener.If the application adds more and more handlers to the events without timily removing or unsubscribing them ,then memory usage will grow till the usage of the application ,till there is no more memory left for allocation. The solution to the problem is to unsubscribe the event when not being used ,but it is not always obvious to know when you can unsubscribe.Its very difficult to know when an object will be out of scope so that it can unsubscribe to it. This is where the Weak Event Pattern. It keeps

IDisposable pattern.

IDisposable pattern. Finalize - implement of finalize method in our class to release the unmanaged resources or objects. Managed objects are object that are developed in .Net . But Unmamanged resources are one which are like windows componenet . So to release the unmanaged resource Finalize should be implemented. But we cannot access the Finalize Method and this will be called by the Garbage Collector. When an object is created it will be in GENERATION1 .GC comes to play and check if some object is unused /inactive then GC will released and deallocate the memory. GC will check the object header and check to see if there is any finalize method mentioned there or not . If its there then it will be moved to Finalized Queue which is also managed by the GC.But when the GC will go and check the Finalized queue to released the memeory is not known and we have no control to it. The Finalize is implemented by using the destructor of the class.The descrutor will get converted to Finalize meth

MANUALREASETEVENT VS AUTORESETEVENT

 MANUALREASETEVENT VS AUTORESETEVENT using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; namespace ManualResetEvent_ {     class Program     {         static ConsoleColor prevColor = Console.ForegroundColor;         static AutoResetEvent autoreset = new AutoResetEvent(false);        //declaring Autoreset event and markes it as static so all class can share it         static ManualResetEvent manualreset = new ManualResetEvent(false);  //declaring Manualreset event and markes it as static so all class can share it         static void Main(string[] args)         {             Console.Title = "AutoResetEvent and ManualResetEvent Example";             Thread t1 = new Thread(new ThreadStart(AutoResetMethod));       //AutoResetEvent is the method which will start  when the thread starts             Thread t2 = new Thread(new ThreadStart(ManualResetMethod));     //ManualResetEvent is the metho

InterLocked synchronisation with Multithreading

using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; using System.Windows.Forms; namespace InterlockedClassDemo {  public partial class Form1 : Form  {  int Number = 0;  public Form1()   {    InitializeComponent();   }  private void button1_Click(object sender, EventArgs e)   {   Number =0;                                                      //whenever a button is clicked the Number is reassigned to 0              int totalThread = 10000;                                         //the number of threads thats need to be created is 10000             Thread[] interLockedThread = new Thread[totalThread];            // Array of types Thread is created             for (int i = 0; i < totalThread; i++)                            // Declared For Loop to creat 10000 thread for performing the increment operation             {           

LAWS OF PROGRAMMING

Thanks to  Meysam Mefouzi to posting here - http://www.codeproject.com/Articles/4852/Murphy-s-Computer-Law-others   Bove's Theorem The remaining work to finish in order to reach your goal increases as the deadline approaches. Brook's Law Adding manpower to a late software project makes it later. Cann's Axiom When all else fails, read the instructions. Deadline-Dan's Demon Every task takes twice as long as you think it will take. If you double the time you think it will take, it will actually take four times as long. Demian's Observation There is always one item on the screen menu that is mislabeled and should read "ABANDON HOPE ALL YE WHO ENTER HERE." Dr. Caligari's Come-Back A bad sector disk error occurs only after you've done several hours of work without performing a backup. Finagle's Rules: To study an application best, understand it thoroughly before you start. Always keep a record of data.

LINQ AND XML AND GRID VIEW

LOADING DATA FROM XML TO GRID VIEW using System; using System.Collections.Generic; using System.ComponentModel; using System.Diagnostics; using System.Linq; using System.Security;          /* the class that contains the encrypted string*/ using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.Drawing; using System.Xml.Linq; namespace ConsoleApplication7 {     class GUI :Form     {               private Label nameLabel1;        private Label nameLabel2;        private Button nameButton;        private ComboBox nameComBox;        private ComboBox nameComBox1;        private List<string> nameList;        private List<string> nameList1;        private DataGridView nameDataGridView;         public GUI()         {             myform();         }            public void myform()         {                         nameButton = new Button();             this.nameButton.Top = 100;             this.nameButton.Left = 100;             this.nameButto

LINQ and XML POC

THE CODE IS GIVEN BELOW-  using System; using System.Collections.Generic; using System.ComponentModel; using System.Diagnostics; using System.Linq; using System.Security;          /* the class that contains the encrypted string*/ using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.Drawing; using System.Xml.Linq; namespace ConsoleApplication7 {     class GUI :Form     {               private Label nameLabel1;        private Label nameLabel2;        private Button nameButton;        private ComboBox nameComBox;        private ComboBox nameComBox1;        private List<string> nameList;        private List<string> nameList1;         public GUI()         {             myform();         }            public void myform()         {                         nameButton = new Button();             this.nameButton.Top = 100;             this.nameButton.Left = 100;             this.nameButton.Text = "finished";             nameLabel1 =