Search This Blog

Thursday, November 21, 2013

HashSet C# Example

C# > Generics > HashSet

HashSet is an optimized set of value collection. A set is a collection that contains no duplicate elements.

Note: The elements are not in a particular order

Example:
 
Use UnionWith to merge two sets with millions of elements
            
            HashSet<int> s1 = new HashSet<int>();
            HashSet<int> s2 = new HashSet<int>();
            for (int i = 0; i < 1000000; i++)
            {
                s1.Add(i);
            }
            for (int i = 1000001; i < 2000000; i++)
            {
                s2.Add(i);
            }
            HashSet<int> s3 = new HashSet<int>(s1);
            s3.UnionWith(s2);
            if (s3.Contains(1500000))
            {
                MessageBox.Show("1,500,000 element found in HashSet"); 
            }




Design Patterns C#

C# > Design patterns

A design pattern provides a framework for building an application. Design patterns makes the design process cleaner and more efficient. You can use existing design patterns to make templates for your objects and allowing you to build software faster.





Singleton pattern C# Example

 C# > Design Patterns > Singleton

Singleton is used to restrict instantiation of a class to one object. In this way we ensure that the class has only one instance.

When to use?

Usually singletons are used for centralized management  and provide a global point of access to themselves.
  • logging
  • configuration
  • resources

Example

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    class Singleton
    {
        private static Singleton instance;
        protected Singleton()
        {
          // protected constructor
        }
 
             public static Singleton GetInstance()
        {
            if (instance == null)
            {
                lock (typeof(Singleton))
                {
                    instance = new Singleton();
                }
            }
            return instance;
        }
    }

    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            // Singleton st = new Singleton(); Error  'WindowsFormsApplication1.Singleton.Singleton()' is inaccessible due to its protection level
            Singleton st1 = Singleton.GetInstance();
            Singleton st2 = Singleton.GetInstance(); // st2=st1
        }
    }
}

 





Wednesday, November 20, 2013

LINQ to DataSet - Filtering with DataView C#

C# > System.Data   > DataSet > Linq to DataSet

DataView can be created from a LINQ to DataSet query.

Example:

Create a DataView from a LINQ to DataSet query with a Where clause

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            DataSet ds = new DataSet();
            ds.Tables.Add("Product");

            DataTable dt = ds.Tables[0];
            dt.Columns.Add("id", typeof(Int32));
            dt.Columns.Add("name", typeof(string));
            dt.Columns.Add("quantity", typeof(Int32));

            fillDataTable(dt);

            EnumerableRowCollection<DataRow> query = from p in ds.Tables[0].AsEnumerable()
                                                     where p.Field<Int32>("quantity") > 50 && p.Field<Int32>("quantity") < 70
                                                     select p;
            DataView view = query.AsDataView();
            BindingSource bs = new BindingSource();
            bs.DataSource = view;
        }

        void fillDataTable(DataTable dt)
        {
            for (int i = 0; i < 100; i++)
            {
                DataRow dr = dt.NewRow();
                dr[0] = i;
                dr[1] = "Product ";
                dr[2] = i;
                dt.Rows.Add(dr);
            }
        }
    }
}





What is Oracle PL/SQL?

Oracle PL/SQL

PL/SQL is an language that was designed for processing of SQL commands. 
PL/SQL stands for Procedural Language extension of SQL.
Server-side PL/SQL is stored and compiled in Oracle Database and runs within the Oracle executable.

PL/SQL is a block structured language, which means program is in logical blocks of code.

Structure


DECLARE   
      <declarations section>
BEGIN   
      <executable command(s)>
EXCEPTION   
      <exception handling>
END;

Example

DECLARE
   msg varchar2(50):= 'PL/SQL Example';
BEGIN
   dbms_output.put_line(msg);
END ;
/

Links






String functions SQL Server

SQL Server > Built-in Functions > String

String functions perform an operation on a string input value and return a string or numeric value.
  • SUBSTRING





Operators SQL Server

SQL Server > Operators

An operator is a symbol specifying an action that is performed on expressions

Arithmetic
Relational
Set