Techaray

Shailendra Saxena's Technical Blog

LINQ -Querying

clock December 16, 2012 01:18 by author ShailendraSaxena

 

Introduction

For Linq Queries you must have a source defined before we write a query against it. LINQ can query nearly all collections of data in .net Framework.

For Practicing LINQ Queries LinqPad is a great tool. You can download it from www.linqpad.net/.

 

//Data in Array 

 

 string[] employees = {"Shailendra","Sachin","Rohit","Chawla"};

 

 var empQuery =from emp in employees

 where emp.Length>5

 select emp;

 empQuery.Dump();

 

 //Order By Clause

 var empQuery =from emp in Employees

 where emp.Country=="USA"

 orderby emp.EmployeeID

 select emp;

 empQuery.Dump();

 

 

  //Group By Clause

 var empQuery =from emp in Employees

 group emp by emp.Title;

 

 empQuery.Dump();

 

   //Join Query

var empQuery =from emp in Employees

join et in EmployeeTerritories on emp.EmployeeID equals et.EmployeeID

select  new { EmployeeId=emp.EmployeeID,EmployeeName=emp.FirstName + "" + emp.LastName};

empQuery.Dump();



Serialization

clock December 14, 2012 05:52 by author ShailendraSaxena

 

Serialization is the process of converting an object into a stream of bytes in order to store the object or transmit it to memory, a database, or a file. Its main purpose is to save the state of an object in order to be able to recreate it when needed. The reverse process is called deserialization.

How Serialization Works

This illustration shows the overall process of serialization.




The object is serialized to a stream, which carries not just the data, but information about the object's type, such as its version, culture, and assembly name. From that stream, it can be stored in a database, a file, or memory.

Uses for Serialization

Serialization allows the developer to save the state of an object and recreate it as needed, providing storage of objects as well as data exchange. Through serialization, a developer can perform actions like sending the object to a remote application by means of a Web Service, passing an object from one domain to another, passing an object through a firewall as an XML string, or maintaining security or user-specific information across applications.

Making an Object Serializable

To serialize an object, you need the object to be serialized, a stream to contain the serialized object, and a Formatter. System.Runtime.Serialization contains the classes necessary for serializing and deserializing objects.

Apply the SerializableAttribute attribute to a type to indicate that instances of this type can be serialized. A SerializationException exception is thrown if you attempt to serialize but the type does not have the SerializableAttribute attribute.

If you do not want a field within your class to be serializable, apply the NonSerializedAttribute attribute. If a field of a serializable type contains a pointer, a handle, or some other data structure that is specific to a particular environment, and the field cannot be meaningfully reconstituted in a different environment, then you may want to make it nonserializable.

If a serialized class contains references to objects of other classes that are marked SerializableAttribute, those objects will also be serialized.

Binary and XML Serialization

Either binary or XML serialization can be used. In binary serialization, all members, even those that are read-only, are serialized, and performance is enhanced. XML serialization provides more readable code, as well as greater flexibility of object sharing and usage for interoperability purposes.

Binary Serialization

Binary serialization uses binary encoding to produce compact serialization for uses such as storage or socket-based network streams.

XML Serialization

XML serialization serializes the public fields and properties of an object, or the parameters and return values of methods, into an XML stream that conforms to a specific XML Schema definition language (XSD) document. XML serialization results in strongly typed classes with public properties and fields that are converted to XML. System.Xml.Serialization contains the classes necessary for serializing and deserializing XML.

You can apply attributes to classes and class members in order to control the way the XmlSerializer serializes or deserializes an instance of the class.

SOAP Serialization

XML serialization can also be used to serialize objects into XML streams that conform to the SOAP specification. SOAP is a protocol based on XML, designed specifically to transport procedure calls using XML. As with regular XML serialization, attributes can be used to control the literal-style SOAP messages generated by an XML Web service.

Basic and Custom Serialization

Serialization can be performed in two ways, basic and custom. Basic serialization uses the .NET Framework to automatically serialize the object.

Basic Serialization

The only requirement in basic serialization is that the object has the SerializableAttribute attribute applied. The NonSerializedAttribute can be used to keep specific fields from being serialized.

When you use basic serialization, the versioning of objects may create problems, in which case custom serialization may be preferable. Basic serialization is the easiest way to perform serialization, but it does not provide much control over the process.

Custom Serialization

In custom serialization, you can specify exactly which objects will be serialized and how it will be done. The class must be marked SerializableAttribute and implement the ISerializable interface.



Delegates

clock December 14, 2012 03:21 by author ShailendraSaxena

 


A delegate is a form of type-safe function pointer used by the .NET Framework. Delegates are often used to implement callbacks and event listeners. A delegate does not need to know anything about classes of methods it works with.

A delegate is a reference type. But instead of referring to an object, a delegate refers to a method.

Delegates are used in the following cases:

  • Event handlers
  • Callbacks
  • LINQ
  • Implementation of design patterns

There is nothing that is done with delegates that cannot be done with regular methods. Delegates are used, because they bring several advantages. They foster flexibility of the application and code reuse. Like interfaces, delegates let us decouple and generalize our code. Delegates also allow methods to be passed as parameters. When we need to decide which method to call at runtime, we use a delegate. Finally, delegates provide a way of specializing behavior of a class without sub classing it. Classes may have complex generic behavior, but are still meant to be specialized. Classes are specialized either through inheritance or via delegates.

Delegates are of two types

1.       Simple delegate


Delegate keyword is used to create delegate.
The delegate name can be anything but definition must be same as the signature of method which we want to point to. E.g. the following delegate can point to any method taking two integers and returning an integer.

public delegate int DelegateName(int x, int y);

 

 

 

 

 

 

2.       Multi Cast Delegate

A multicast delegate maintains a list of functions that will all be called when the delegate is invoked. If you want to add a method to the invocation list of a delegate object, you simply make use of the overloaded += operator, and if you want to remove a method from the invocation list you make use of the overloaded operator -=.



Web User Control V/S Web Custom control in asp.net

clock December 12, 2012 00:17 by author ShailendraSaxena

 

Web user controls

Web custom controls

Easier to create

Harder to create

Limited support for consumers who use a visual design tool

Full visual design tool support for consumers

A separate copy of the control is required in each application

Only a single copy of the control is required, in the global assembly cache

Cannot be added to the Toolbox in Visual Studio

Can be added to the Toolbox in Visual Studio

Good for static layout

Good for dynamic layout



Abstract Class in .Net

clock December 7, 2012 03:04 by author ShailendraSaxena

 


Abstract class is a class which is declared with abstract keyword and it cannot be instantiated it can be inherited only.

Abstract class can have completed or uncompleted methods in it. If all the methods are uncompleted then it is similar to an interface.



Search

Calendar

<<  August 2017  >>
SuMoTuWeThFrSa
303112345
6789101112
13141516171819
20212223242526
272829303112
3456789

Archive

Tags

Categories


Visitors

FlagCounter

Flag Counter

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

© Copyright 2017

Sign In