Saturday, February 20, 2016

Freshers Serialization in C# & Types (JSON /XML/ BINARY)

Serialization  in C#
Definition:
Process to convert a object into stream of data into memory cache/ physical file/ database

Usage: For Maintain/Preserve data present inside a class  will be store for  future usage/ reference using Serialization for easy understanding below sample


Sample 1:

using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;


/* This class information will be serialized and  stored in the file  Serializable attribute above the class name will make all fields present inside the class to be serialized if you need to omit any variable from serializing you need to placed [NonSerializable()] attribute above the field operaion* /

[Serializable()]
public class employeeInfo
{
public string empName {get;set;}
public int empId {get; set;}
public string designation {get; set;}

  public employeeInfo (string name, int id, string role)
  {
     empName =Name;
     empId = id;
     designation = role; 
    }
}

public class mainprogram
{
      static void main ()
      {
          Console.WriteLine("Provide your  Option 1 -Read or 2- Write");
          switch( Console.Readline())
          {
              case "1":
              using (Stream stream= File.Open("data.bin", FileMode.Create))
               {
                 BinaryFormatter bin = new BinaryFormatter();
                 var empList = (List <employeeInfo)bin.Deserialize(stream);
                 foreach(employeeInfo emp in empList)

                 {
                    Console.WriteLine("{0},{1},{2}", 
                    employeeInfo.empName, employeeInfo.empId, employeeInfo.designation);
                  }
               }
           }
           break;     
        case "2":
   
       break;
              case "2":
               var emplList = new List<Lizard>();
 emplList.Add(new employeeInfo("mponraj",846281, "Developer"));
 emplList.Add(new employeeInfo("msadasi",853909, "Developer"));
 emplList.Add(new employeeInfo("msriniv",828467, "Developer"));
    using (Stream stream = File.Open("data.bin", FileMode.Create))
 {
   BinaryFormatter bin = new BinaryFormatter();
   bin.Serialize(stream, emplList);
 }
      }
      
      break;
             default:
             break;
           }


       }

}



Saturday, April 5, 2014

Dependency Property in WPF

Dependency properties have more uses in WPF and having advanced features. WPF Classes used more properties rather than methods.
Before going to Dependency properties in WPF, we need to understand our normal DotNet Properties.

Dot Net Properties
Class A
{
private int  _myvalue;


public int MYVALUE
{
get{return _myvalue;}
set{ _myvalue=Value;}
}

}

A property must associated with private field in class and represent that field to other classes. This field is backing field.
backing field nothing but private variable which holds the property value.

Assign value
When ever value get assigned to the property, value is passed to Set accessor reflect value to backing field.
Read Value
When ever value get read from the property, Get accessor just return the value from backing field

Dot net property can be easily assigned and read directly from private variables whereas WPF Dependency properties value is resolved dynamically
When calling GETVALUE() method that is inherited from Dependency Object.

WPF -Dependency Properties


Advantages
Reduce Memory Usage:
      If we set value to parent Control it will easily reflect to all child controls. modified properties present in the instance. The Default value stored once with dependency property.
Value Inheritance:
   Dependency property value is resolved using value resolution strategy. if no local value being set, the dependency property will travel from top to bottom until find its value.
Change Notification:
      Dependency properties having new feature built in notification whenever a value modify it will notify the changes. By registering a callback data in the mechanism. Used in DataBinding techniques.

Features
Value of  dependency property  is not stored in field of object, but it will stored in dictionary of keys and value will provided by the base class Dependency Object.

VALUE RESOLUTION STRATEGY


Example
XAML File which set FontWeignt bold it will reflect to all child controls unless explicitly modification/Changes in the controls.

<Window X:Class="Inherit.Font.Window1"....>
<GroupBox FontWeight="Bold">
<GroupBox.Header>
Buttons
</GroupBox.Header>
</GroupBox>
<StackPanel>
<Button FontWeight="Medium">Update</Button>
<Button>Reset</Button>
</StackPanel>
</Window>
Sample Dependency property

// Dependency Property
public static readonly DependencyProperty CurrentTimeProperty = DependencyProperty.Register( "CurrentTime", typeof(DateTime), typeof(MyClockControl), new FrameworkPropertyMetadata(DateTime.Now));

// .NET Property wrapper
public DateTime CurrentTime
{
get { return (DateTime)GetValue(CurrentTimeProperty); }
set { SetValue(CurrentTimeProperty, value); }
}

Saturday, February 1, 2014

How to Create Web Service using C# (For Beginners)


 Creating Web Service in C# Step by Step manner (Use full for freshers)
  •   Create new project in Visual studio framework



  • Choose the visual studio 3.5 framework using C# programming language in Web Application Template Choose ASP.NET Web Service Application create new web service project.



















  • After Opening the solution the default web method "HELLO WORLD ()" in the Code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;

namespace WebService1
{
    /// <summary>
    /// Summary description for Service1
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    // [System.Web.Script.Services.ScriptService]
    public class Service1 : System.Web.Services.WebService
    {

        [WebMethod]
        public string HelloWorld()
        {
            return "Hello World";
        }

              
    }
}

  • Created custom method for getting input parameter in the below web method. In this below method i get  input string parameter.  
          [WebMethod]
        public string Welcome(string name)
        {
            return string.Format("Welcome {0} to Tutorial Web Service", name);
        }


  • After executing the Web Service Application the all methods displayed in the screen


  • Input Parameter method invoke Operation

  • After invoke Operation result of the web method the result will appear as below,




















I hope the above article will meet the expectation for simple Creating Web Service.

Wednesday, October 9, 2013

Difference between Abstract Class VS Interaces

Abstract Class

  •    with keyword Abstract in the class Contain both abstract and non abstract methods. Abstract  methods does not contain implementaion in base classs only contains  method name with parameters with keyword abstract in the methods. Non Abstract methods contain implementation in the base Class itself.
  • Abstract method should be override in the derived class by defining it. 
  • A class can be inherited from only one abstract class.
  • Abstract class can't able to instantiate. It can be accessed through derived class objects.
  • Abstract class have access specifiers for Function/Properties/Methods
Interface
   
  • Interface is not a class it is a Entity defined by interfaces. Methods defined inside interface does not contain implementation only method  name with parameter. Classes which inherited interfaces all methods defined will get Implemented in the base class.
  • A class can have more than One Interfaces . It supports multiple inheritance.(C# does not support multiple inheritance.) 
  • Interface does not contain Access Specifier for Function/Properties/Methods default as Public.
  


LINQ IN SQL

LINQ - Language Integrated Query

  • Feature Introduced in Visual Studio 2008 
  • Supported from Visual Studio 2008 & later versions of visual Studio.
  • Used or Query the result set from  Objects/XML/ADO.Net (Object collection/Data Set /SQL Server/Entities/XML).
  • Using Linq 
For Querying result in our front end C# language support LINQ.

Sample Query for Linq


var q=(from item in TAA_Parcels
where item.IsDeleted==false
 select new{
                item.ParcelID,
                 item.ParcelNumber,
           }).ToList(); 
  
q.Dump();

Result Set



































INNER JOIN uisng  LINQ Query

from item in Metadatas 
 join item1 in BusinessRules on item.DatabaseID equals item1.DatabaseID
where item.DatabaseID == 1 orderby item.ReportTypeID 
select new
{
item.MetadataID,
item1.RuleName,
item.ReportTypeID,
item1.FormulaExpression

}



CROSS JOIN using LINQ

from meta in Metadatas
from type in  MetadataTypes
where meta.DatabaseID == 1 && meta.MetadataTypeID==1
select new
{
       meta.MetadataID,
       meta.MetadataTypeID,
       meta.DatabaseID,
       type.CreatedBy,
       type.MetadataTypeName
}












































Sunday, September 8, 2013

Generics in C#

GENERICS IN C# 2.0

                           Generics features was introduced in the .net framework 2.0. Generic refer to the technique for writing a class without specifying data type that the class works with. This generic classes allowing to work with many different data type  without needing to be rewritten it


Generic Parameter
  Genereic parameter in the generic class can be identified by  "<" and " >" brackets in class. 

public class Stack<T>
{
   T[] m_Items; 
   public void Push(T item)
   {...}
   public T Pop()
   {...}
}
Stack<int> stack = new Stack<int>();
stack.Push(1);
stack.Push(2);






Friday, August 23, 2013

About Interfaces-properties methods in OOPS Concept

INTERFACES OOPS Concept

All the classes will Inherit interfaces methods. Abstract classes serve some similar operation  of interfaces. Inside the interface methods does not implemented. Interface will be define methods, properties and events

Interface is not a class it will represent with the key word 'interface'
Interface name contain with I prefix in the interface name.
Interface contain public as its modifiers.
Interface methods will be implemented in the class file which inherited interfaces.

A class can be Inherit more interfaces. Interfaces will support multiple inheritance.

code: 

//Interface contain methods without implementation inside the interface
public interface Ivalue
{
    string Name();
}

//Interface method declare in the inherited classes of the intefaces
Public class Operation : Ivalue
{
   public string Name()
  {
     return "MANOJ KUMAR";
   }
}

SAMPLE PROJECT 

//Interface
    public interface Isample
    {
        string fname
        {
            get;
            set;
        }

        string lname
        {
            get;
            set;
        }

        string Name();
    }

//Class inherited with interfaces

    public class Operation : Isample
    {

       protected string _fname;
       protected string _lname;

        public string fname
        {
            get
            {
                return _fname;
            }
            set
            {
                _fname = value;
            }
        }

        public string lname
        {
            get
            {
                return _lname;
            }
            set
            {
                _lname = value;
            }
        }

        public string Name()
        {
            return "First Name is " + fname + " & Last Name is " + lname;
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            Isample sam = new Operation();
            sam.fname = "Manoj";
            sam.lname = "Kumar";
            Console.WriteLine(sam.Name());
            Console.ReadLine();
        }
    }
}

Result Set