C# 4.0 new Features.

Dynamice Language Runtime

Dynamic  Lookup

dynamic keyword : These Object type need not be known till runtime. Member’s signature is not know till it is executed.

E.g. System.Reflection

Programming against COM IDispatch

Programming against XML or HTML DOM

Dynamic Language Runtime (DLR) behaves more like Python or Ruby.

Dynamic in C# is a type for e.g.

Dynamic WildThings(dynamic  beast, string name)


Dynamic whatis = beast.Wildness(name);


return whatsits;


dynamic : statically declared on object type, when object is marked to be dynamic that object is recognized by the compiler and it replaces the object metadata to be used during runtime, the runtime  then check to resolve the call which would be invoked either as dynamic dispatch or throws runtime error.

dynamic != var

Var  keyword is used for type inference and compile time check is made.

Dynamic keyword is used for object that is unknown during compilation and hence compile time  check is not made.

dynamic cannot be used for Extension methods

dynamic methods invocation cannot use anonymous methods  as parameter.

dynamic heisenberg;

Void LocationObserver(float x, float t) {}

Heisenberg.Observer(LocationObserver); –> right way of using call

Heisenberg.Observer(delegate (float y, float t){});–> wrong way of using call

Heisenberg.Observer((x,t)=>x+t);–> wrong way of using call

dyanmic  objects cannot be used in LINQ.

Dynamic collection = {1,2,4,5 ,6, 7,8}

Var result = collection.Select(e=>e.size>25)

  1. Select is an extension method
  2. Selector is a lambda

Dynamic Language Runtime is loaded everytime  dynamic objects are executed.

It reduces the efficiency because for caching only for the first time and then subsequent execution is same as normal execution as no caching will be required.

DLR is a normal assembly part of System.Core , dynamic objects implement IDispatch or IDynamicObject Interface. Using Dynamic XML now we can shorten the invocation for e.g. element.Lastname instead of element.Attribute[LastName].

COM support in C# 4.0

COM interops is feature where COM Interface methods are used to interact with Automation Object like Office Automation. Now ref keyword can ignored while using COM Interops and PIA objects.

Now the publisher creates the COM interops assembly using COM Interface which was earlier release done by the developer of COM Interops. With the latest release of C# there is no option of PIA, hence code is generated or implemented only for the COM Interface methods  that were used by the application.

Named Parameters and  Optional Parameters


Optional Parameters sets a default value for the parameter used; Optional parameter is used for consistence in C# syntax; Optional parameter takes the default value if the parameter is not passed with method invocation.

Static void Entrée(string name, decimal price=10.0M, int servers=1, bool vegan =false)

Static void main ()


Entrée(“Linuine Prime”, 10.25M,2, true); -> overrides all default values

Entrée(“Lover”, 11.5M,2); -> overrides bool

Entrée(“Spaghetti”, 8.5M); ->overrides bool int

Entrée(“Baked Ziu”); -> overrides bool int decimal


Named parameters : Bind values to parameters e.g. using Microsoft.Office.Tools.Word;

Document doc;

Object filename = “MyDoc.docx”;

Object missing = System.Reflection.missing.Value;

Doc.SaveAs(ref fileName, ref missing, ref missing ,…ref embeddedTTFS,…..);

Now it can be used as doc.SaveAs(FileName:ref fileName, embeddedTTFS: ref embedTTFS);

the method invocation will contain the parameters that are mentioned and other missing parameter will now have default values.

e.g. Thing(string color=”white”, string texture=”smooth”, string slope=”square”, string emotion=”calm”, int quantity =1)

Publi static void Things()




Thing(texture :”Furry”,shape:”triangular”);



Benefits : No longer creating overload() simply for the convenience of omitting parameter

Office Automation COM interops use optional parameters

No longer have to scorn about VB language.

It uses principle of Least surprise while mapping of the method.

Liabilities: Complicates overload resolution of optional parameter

Events in C# 4.0 

Syntax for events :

public event EventHandler<TickEventArgs>Tick;

Public void OnTick(TickEventArgs e){ Tick(this,e);}

Public class TickEventArgs:EventArgs


public string Symbol {get; private set;}

public string Price {get; private set;}

public TickEventArgs(symbol, decimal, price)


Symbol = symbol;

Price = price;



In C#4.0, events is now implemented based on compose and swap technique.

Now Events works for static & instance types, events works for reference and value types.

Covariance and ContraVariance:

Covariance : Modifier out on a generic Interface or delegate e.g. IEnumerable<out T>

The parameter type T can only occur in an output position, if used in input position it will throw error, if used in output position then an argument of a less derived type can be passed.

Enumeration of giraffe is also Enumeration of animals

Contravariance: Modifier in on a generic interface or delegate e.g. IComparable<in T>

Type T can only occur in input position, Compiler will generate  contravariant  conversions. It means an argument of a more derived type can be passed.

So variance can be used for comparison and enumeration of collections in type safe manner.

AutoProperties in C# 

Type inference changes in C# 4.0 has now allowed developer to declare properties and their corresponding accessor  and mutator method are generated by compilier defaultly. For e.g.

Public class Pt{

Public int X { get; set;}

Public int Y { get; set;}

} and compiler generates the back field which is inaccessible.

This type of property is now known as Auto properties.

Implicitly typed local variables : These variables can occur

1 inside foreach.
2 Initialization of for
3 Using statement
4 Local variable declaration

Initializers specifies values for fields and properties in single statement.

Var p1 = new Point {X=1, Y=2};

Var p2= new Point (1){Y=2};

Collection Initializers:

The class should have on Add public method which would take on one Key parameter and the other value parameter then we can use collection initializers as follows

Public class Dictionary <Tkey, Tvalue>:IEnumerable


public void Add(Tkey key, Tvalue value) {…}



Var namedCircles = new Dictionary<string, Circle>


{“aa”, new Circle{Origin=new PT{X=1,Y=2}, Radius=2}}

{“ab”, new Circle{Origin=new PT{X=2,Y=5}, Radius=3}}


Lambda in C#

Anonymous methods is a delegate function which is inlined as a block of code.

Lambda is a functional declarative syntax way of writing Anonymous method and it is a single statement.

Lambda function has an operator “=>” known as ‘goesto’

Delegate int SomeDelegate(int i);

SomeDelegate squareint = x =>x*x;

Int j =squareint(5); //25
(x,y) => x ==y;  //type infered
(int x, string s) => s.Length > x; //type declared.
() => Console::WriteLine(“Hi”); // no args

Statement Lambda :e.g.

Delegate void Another Delegate(string s);

AnotherDelegate Hello = a => {

string w = String.Format(“Hello, {0}”,a);



Hello(“world”);  == Hello world

Extension Methods :

Extension Methods are static methods that can be Invoked using instance method syntax. Extension method are less discoverable and has less functionality. Extension method are static methods has one parameter ‘this’.

Using Extension Methods

  • Must define inside non generic static class
  • Extension methods are still external static methods
  • Cannot hide, replace or override instance methods
  • Must import namespace for extension method.

System.Linq defines extension methods for IEnumerable and IQueryable <T>

Shrinking Delegates using lambda expression Func<int, int> sqr = x=>x*x

What if entries are not in memory then use lambda expression for that we need to import System.Ling.Expression.

Lambda functions as delegates become opaque code and treat it as special type, the alternative is Expression<TDelegate>. Expression Trees is used for runtime analysis.


Int[] digits={0,1,2,3,4,5,6};

Int [] a = digits.Slice(4,3).Double()

Is same as Instance Syntax i.e.

Int []a = Extension.Double(Extension.Slice(digits,4,3));


Introduction: W3C-Compilant DOM a.k.a. XMLDocument, XMLReader & XMLWriter are part of namespace System.Xml.Linq.

What is DOM: declarations, element, attribute value and text content can be represented with a class, this tree of objects fully describe a document. This is called a document object model or DOM.

The LINQ to XML DOM: Xdocument, Xelement and Xattribute, Xdom -> LINQ friendly: This means LINQ has methods that emit useful IEnumerable sequences upon which you can query. It constructors are designed or create an XDOM tree through LINQ project.

XDOM Overview:

Types of Elements


XObject is the root element inheritance hierarchy.

XElement & XDocument are roots of the containership.

XObject is the abstract base class of XDocument.

XNode is the base class which excludes attributes and it is the ordered collection of mixed types.


Helloworld              à XText

<subelement1/>   àXelement

<!—comment – -> àXComment

<subelement2/> à Xelement



XElement ———————————————————|————————————————-XDocument

XDocument: is the root of an XMLTree wraps the root Xelement adding an Xdeclaration.

Loading and Parsing: XElement, XDocument loads and parse methods to build X-DOM tree from existing source.

–          Loads builds an XDOM from a file, URI, Stream, TextReader or XmlReader

–          Parse builds an X-Dom from a string

–          XNode is created using ReadFrom() from XmlReader.

–          XmlReader/XMLWriter reads or write from XNode via from CreateReader() or CreateWriter()

Saving and Serializing: Saving and Serializing of XMLDom is done using the save method from file or stream using TextWriter/XMLWriter

Instantiating an X-DOM using the Add method of XContainer, for e.g.

Xelement lastName = new Xelement (“lastName”, “Blogs”);

LastName.Add(new Xcomment(“nicename”);

Functional Construction: XDOM supports Functional Construction (it is a mode of instantiation), you build an entire tree in a single expression.

Automatic Deep Cloning : An already parent node is added to second parent node and deep cloning is made, this process in known as deep Cloning. This automatic duplication keeps X-DOM object instantiation free of side effects.

Navigating and Querying:

XDOM returns single value or sequence implementing IEnumerable when a LINQ query is executed.

FirstNode, LastNode returns first child and last child

Nodes () returns all children, Elements () return child nodes of XElement type

SelectMany Query

Elements () is an extension method that implements IEnumerable<XContainer>

Element () is same as Elements ().FirstorDefault ()

Recursive function: Descendants / Descendant Nodes return recursively child elements/Nodes

Parent Navigation: XNode have parent property and AncestorXXX methods, A parent is always XElement, To access the XDocument we use Document property and Ancestor method return XElement Collection when first element is Parent.

XElement customer =

(new XElement (“Customer”,

new XAttribute (“id”,12),

new XElement (“firstname”, ”joe”),

new XElement(“lastname”,”Bloggs”),

XComment(“nice name”)



Advantage of Functional Construction is

–          Code resembles the shape of the XML.

–          It can be incorporated into the select clause of the LINQ query.

Specific Content: XElement overloaded take params object array. Public XElement (XName name, params object[] content) here are the decision made by the XContainer.


Attribute Navigation: XAttribute define PreviousAttribute () and NextAttribute ().

Updating an XDOM:

Most convenient methods to update elements and attributes are as follows

SetValue or reassign the value property

SetElementValues /SetAttributeValue



Add –> appends a child node

AddFirst -> adds @ the beginning of collection

RemoveAll  è {RemoveAttributes (), RemoveNodes ()}

ReplaceXXX => Removing and then adding,

AddBeforeSelf, AddAfterSelf, Remove and ReplaceWith are applied to Collections.

Remove () -> removes current Node from its Parent

ReplaceWith -> Remove and then insert some other content at the same position.

E.g. Removes all contacts that feature the comment “confidential” anywhere in their tree

Contacts. Elements ().Where (e=>e.Descendant.Nodes ()

.OfType<XComment> ()

.Any (c=>c.Value ==”confidential”)).Remove();

Internally Remove () —-Copiesà temporary list –enumerateà temporary list àperform deletionsà avoids errors while deleting and querying at the same time.

XElement —Values()à the content of that node.

Setting Values: SetValue or assign the value property it accepts any simple data types

Explicit casts on XElement & XAttribute

All standard numeric types

String, bool, DateTime, DateTimeOffset, TimeSpan & Guid Nullable<> versions of the aforementioned value types

Casting to a nullable int avoids a NullReferenceException or add a predicate to the where clause

For e.g. where cust.Attributes(“Credit”).Any() && (int)cust.Attribute

Automatic XText Concatenation: If you specifically create XText nodes but end up with multiple children

Var e = new XElement(“test”, new Xtext(“Hello”), new Text(“World”));

e.Valueè HelloWorld


XDocument: It wraps a root XElement and adds XDeclaration, It is based on XContainer and it supports AddXXX, RemoveXXX & replaceXXX.

XDocument can accept only limited content

-a single XElement object (the ‘root’)

-a single XDeclaration

– a single XDocumentType object

– Any number of XProcessing Instruction

– Any number of XComment objects

Simplest valid XDocument has just a root element

var doc= new XDocument(XElement (“test”,”data”));

XDeclaration is not an XNode and does not appear in document Nodes collection.

XElement & XDocument follow the below rules in emitting xml declarations:

–          Calling save with a filename always writes a declaration

–          Calling save with an XMLWriter writes a declaration unless XMLWriter is instructed otherwise

–          The toString() never emits XML declaration

XMLWriter will be set with the following settings OmitXmlDeclaration and Conformance Level properties to produce XML without declaration.

The purpose of XDeclaration is

What text encoding to use

What to put in the XML declaration encoding /standalone attributes.

XDeclaration Constructors parameters are

  1. Version
  2. Encoding
  3. Standalone

Var doc = new XDocument ( new Xdeclaration(“10”,”utf-8”,”yes”),new XElement(“test”, “data”));

File.WriteAllText è encodes using UTF-8

Namespace in XML: Customer element in the namespace

OReilly.Nutshell.CSharp is defined as

<customer xmlns=”OReilly.Nutshell.CSharp”/>

Xmlns = sql reserved attribute: It specifies a namespace for the element in question.

It specifies a default namespace for all the descendants’ element.

Prefix is an alias that you assign to a namespace to save typing. Two ways of using a Prefix are

<nut:customer  xmlns:nut=”OReilly.Nutshell.CSharp”/>

Nut:customer assigns the newly allocated prefix to the customer element.

Attributes:  Assign namespace to attributes

<customer xmlns: nut = “OReilly.Nutshell.CSharp” nut: id=”123”/>

An exception is with general purpose/metadata attributes such as the nil attributes defined by W3C

<customer xmlns: xsi=http://www.w3c.org/2007/XMLSchema-instance>


<lastname xsi: nil=”true”/>


Unambiguously xsi: nil attributes informs that lastname is nil.

Specifying Namespace in the X-DOM

  1. Var e = new XElement(“{http://domain.com/xmlpsace}customer”,”Bloggs”);
  2. Use the XNamespace and XName types

Public sealed class XNamespace


Public string Namespace Name {get ;}


Public sealed class XName


Public string LocalName {get ;}

Public XNamespace Namespace {get ;}


Both types define implicit casts from string, so the following is legal,

XNamespace ns = “http://domain.com/xmlspace”;

XName localName = “customer”;

XName fullName = “{http://domain.com/xmlspace/customer}”;

XName overloads +operator

XElement, namespace must be explicitly given otherwise it will not inherit from parent.

XNamespace ns=”http://domain.com/xmlspace”;

var data = new XElement (ns+”data”, newXElement(ns+”customer”,”Bloggs”), new

XElement (ns+”purchase”, “Bicycle”));


<data xmlns= http://domain.com/xmlspace>




For nil attribute we write it as <dos xsi:nil=”true”/>

Annotations: Annotations are intended for your own private use and are treated as black boxes by X-DOM. Following are XObject add & remove annotations

Public void AddAnnotations(object annot)

Public void RemoveAnnotations<T> () where T: class

Annotations methods to retrieve a sequence of matches

The source can be anything over which LINQ can query such as

-LINQ to SQL or Entity Framework queries

-A load collections

-Another X-DOM

Regardless of the source, the strategy is the same in using LINQ to emit X-DOM

For e.g. retrieve customers from a db into XML


<customer id=’1’>




We start by writing a functional construction expression for the X-DOM

Var customers = new XElement (“customers”, new XElement (“customer”, new XAttribute (“id”, 1), new XElement (“name”,”sue”), new XElement (“buys”, 3)));

We then turn this into a projection and build a LINQ query around it.

Var customers = New XElement(“customers”, “from c in dataContext.Customers select

New XElement(“customers” new XAttribute(“id”,c.ID),

new XElement (“name”,c.Name),

new XElement(“bugs”,c.Purchase.count)

)   );

IQueryable <T> is interface used during enumeration of database query and execution of SQL statement. XStreaming Element is a cut down version of XElement that applies to deferred loading semantics to its child content. This queries passed into an XStreaming Element constructor are not enumerated until you call save, toString or writeTo on the element: this avoids loading the whole X-DOM into memory at once.

XStreaming Element doesn’t expose methods such as Elements or Attributes. XStreaming Element is not based on XObject.

Concat operator preserves order so all elements/ nodes are arranged alphabetically.

System.XML namespace:                             System.XML.*

XMLReader & XMLWriter



  • XPathNavigator -Information and API




LINQ centric version of XMLDocument

XmlConvert – a static class for parsing and formatting XML Strings

XMLReader is a high performance class for reading

XMLStream is a low level and forward only manner class for I/O operations

XMLReader – instantiated using the Create Method

XMLReader rdr = XMLReader. Create (new System.IO.StringReader (myString));

XmlReader settings object used to create parsing of validation options:

XMLReaderSettings settings = new XMLReaderSettings();

Settings.IgnoreWhitespace = true

Settings.IgnoreProcessingInstructions = true

Settings.IgnoreWhitespace = true

Using ( XMLReader reader =  XmlReader.Create(“customer.xml” ,settings));

XMLReaderSettings.CloseInput() to close the underlying stream when the reader is closed. The default value for CloseInput and CloseOutput  = true;

The units of XML stream are XMLNodes; reader traverses the stream in depth first order. Depth property returns the current depth of the cursor.

The most primitive way of reading is Read (), it first calls positions cursor to first node.

-When Read() returns false means it went past last node, Attributes are not included in Read based traversal.

Node Type is of XMLNodeType then its enum members are as follows

Name , Comment , Document, XmDeclaration, entity, Documentype, Element, EndEntity, DocumentFragment, EndElement, EntityReference, Notation, Text Processing Instruction Whitespace, Attribute, CDATA, Significant Whitespace,

String properties of Reader: Name & Value.

Switch (r.NodeType)




Case XMLNodeType.XmlDeclaration: Console.Writeline(r.value);


Case XMLNodeType.DocumentType: Console.Writeline(r,name+”-“+r.value);



An entity is like a macro; a CDATA is like a verbatim string(@”…”) in C#.

Reading Elements : XmlReader provides few methods to read XMLDocument. XmlReader throws an XmlException if any validation fails. XmlException has line number and line Position.

ReadStartElement() verifies that the current NodeType is StartElement

ReadEndElement() verifies that the current NodeType is EndElement and then calls Read.

Reader.ReadStartElement (“firstName”);



ReadElementContentAsString -> reads a start Element a text node and an end element, returning as a String;

Similarly ReadElementContentAsInt -> reads a end Element as Int.

MoveToContent() skips over all the fluff: XMLdeclarations  whitespace, comments and processing instructions.

<customer/> -> ReadEndElement throws exception because there is no end element for xml reader.

The workaround for the above scenario is

bool Empty = reader.IsEmptyElement();


if(!isEmpty) reader.ReadEndElement();

The ReadElementXXX() handles both kinds of empty elements.

ReadContentAsXXX parses a text node into type XXX using the XMLConvert class.

ReadElementContentAsXXX apply to element nodes rather than text node enclosed by the element.

ReadInnerXML returns an element and all its descendants, when used for attribute returns the value of the attribute.

ReadOuterXML includes the element at the cursor position and all its descendants

ReadSubtree is a proxy reader that provides a view over just the current element.

ReadToDescendant moves the cursor to the first descendant

ReadToFollowing moves the cursor to the start of the first node

ReadToNextSibiling moves the cursor to the start of the first sibling node with the specified name/namespace.

ReadString and ReadElementString same as ReadContentAsString except these methods throw an exception if there’s more than a single text node with the element or comment.

To make it easy the forward only rule is released during attribute traversal jump to any attribute by calling MoveToAttribute().

MoveToElement(): returns start element from any place within the attribute node diversion.

Reader.MoveToAttribute(“XXX”); returns false if the specified attribute doesn’t exists.

Namespaces and Prefixes:

XmlReader provides two parallel systems


-Namespace URI and LocalName.Name()

<c: customer…>               c:customer

So reader.StartElement(“c:Customer”);

The second system is aware of 2 namespace-aware properties – NamespaceURI and LocalName

e.g. <customer xmlns=”DefaultNamespace”, xmlns:other = “OtherNamespace”>

you can see what prefix was used through the prefix property and convert it into a namespace by calling LookupNamespace.

XMLWriter: XMLWriter is a forward only writer using XMLWriter wrt = new XMLWriter(“…\\…\\foo.xml”,settings);






WriteString () = WriteValue with a string it automatically exposes characters such as & < > and extended Unicode character. Towrite non string values, call WriteStartAttribute, WriteValue and then WriteEndAttribute.

For writing other kinds of nodes use the following: writeBase64 WriteCData WriteDocType WriteBinHex WriteComment

WriteRaw -> injects a string into the output stream. WriteNode -> echos everything from the given XMLReader.

Namespaces and Prefixes:

Write an XmlDocument with all the elements associated with the http ://oreilly.com namespace, declaring the prefix O at the customer element:

Writer.WriteStartElement(“O”, “customer”, “http://oreilly.com”);

Writer.WriteElementString(“O”, “firstname”, “http://oreilly.com”, ”Jim”);

Writer. WriteElementString(“O”, “lastname”, “http://oreilly.com”, “Bo”);


The output is as follows:

<? Xml verison=”1.0” encoding =”utf-8” standalone=”yes” ?>

<o:customer xmlns:o=”http://oreilly.com”>




Patterns for using XmlReader/XMLWriter: The best approach is not to write one big method, but to encapsulate XML functionality in the Entity/Business/Application related types e.g. Customer & supplier types themselves by writing ReadXML & WriteXML methods on these types.

–          ReadXML and WriteXML leave the reader/writer at the same depth when they exit.

–          ReadXML reads the outer element, whereas WriteXML writes only its inner content.

The reason for not making WriteXML not symmetrical in this regard is twofold

–          The caller might need to choose how the outer elements is named

–          The caller might need to write extra XML attributes, such as the element subtype.

Another advantage is implementation is compatible with IXMLSerializable

Mixing XMLReader/XMLWriter with an X-DOM: using the XDOM to handle inner elements with XMLReader & XMLWriter is a good way of implementation. XNode.ReadFrom() is not greedy, it reads just the end of the current subtree.

For e.g.

<log><logentry id=”1”> <date>….</date><source>….</source> </logentry>….</log>

XmlReaderSettings settings = new XmlReaderSettings();

Settings.IgnoreWhitespace = true;

Using(XMLReader r = XmlReader.Create(“logfile.xml”,settings))



while(r.Name == “logentry”)


XElement logEntry = (XElement)Xnode.ReadFrom(r );

Int id= (int) logEntry.Attribute(“id”);

DateTime dt = (DateTime)logEntry.Element(“date”);

String source = (string)logEntry.Element(“source”);




By implementing as shown above, you can slot a XElement into a custom type’s ReadXML or WriteXML method without the caller ever knowing you’ve cheated. XElement collaborates with XmlReader to ensure that namespace are kept intact and prefixes are properly expanded. Using XMLWriter with XElement to write inner Elements into an XmlWriter. The following code writes 1 million logentry elements to an XML file using XElement without storing the whole thing in memory:

Using (XmlWriter w = XmlWriter.Create(“log.xml”)


w.writeStartElement (“log”);

for (int I =0; I < 1000000; i++)


XElement e = new XElement(“logentry”, new XAttribute(“id”,i), new XElement(“source”,”test”));





Using XElement incurs minimal execution overhead.

XMLDocument: It is an in memory representation of an XML document, Its object model and methods conform to a pattern defined by the W3C.

The base type for all objects in an XMLDocument tree is XmlNode. The following types derive from XmlNode:






XmlLinkedNode è exposes Next Sibling and Prev Sibling.

XmlLinkedNode is an abstract base for the following subtypes








Loading and Saving the XmlDocument: instantiate an XmlDocument and invoke Load () or LoadXML ()

–          Load accepts a filename, stream, TextReader or XMLReader

–          LoadXML accepts a literal XML String.

e.g. XmlDocument doc = new XmlDocument();



using ParentNode property, you can ascend backup the tree,

Console.WriteLine (doc.DocumentElement.ChildNodes [1].ParentNode.Name);

The following properties also help traverse the document

FirstChild LastChild NextSibling PreviousSibling

XmlNode express an attributes property for accessing attributes either by name or by ordinal position.

Console.WriteLine (doc.DocumentElement.Attributes[“id”].Value);

InnerText property represents the concatenation of all child text nodes

Console.WriteLine (doc.DocumentElement.ChildNodes[1].ParentNode.InnerText);

Console.WriteLine (doc.DocumentElement.ChildNodes[1].FirstChild.Value);

Setting the InnerText property replaces all child nodes with a single text node for e.g.

Wrong way => doc.DocumentElement.ChildNodes[0].Innertext=”Jo”;

Right way => doc.DocumentElement.ChildNodes[0].FirstChild.InnerText = “jo”

InnerXML property represents the XML fragment within the current node. Console.WriteLine (doc.DocumentElement.InnerXML);

Output <firstname>Jim</firstname><lastname>Bo</lastname>

InnerXML throws an exception if the node type cannot have children

Creating and Manipulating Nodes

  1. Call one of the CreateXXX methods on XMLDocument.
  2. Add the new node into tree by calling AppendChild, prependChild, InsertBefore or InsertAfter on the desired parent node.

To remove a node, you invoke RemoveChild, ReplaceChild or RemoveAll

Namespaces: CreateElement & CreateAttribute () are overloaded to let you specify a namespace and prefix

CreateXXX(string name);

CreateXXX(string name, string namespaceURI);

CreateXXX(string prefix, string localName, string namespaceURI)

E.g. XmlElement customer = doc.CreateElement(“o”,”customer”,”http://oreilly.com”);

XPath : Both DOM and the XPath DataModel represents an XMLDocument as a tree.

XPath Data Model is purely data centric, abstracting away the formatting aspects of XMLText.

For e.g. CDATA sections are not required in the XPath Data Model

Given a XML document

XPath queries within the code in the following ways :

Call one of the SelectXXX methods on an XMLDocument or XMLNode

–          Spawn an XPath Navigator from either

  • XmlDocument
  • An XPathDocument

Call an XPathXXX extension method on an XNode.

The SelectXXX methods accept an XPath query string

XmlNode n = doc.SelectSingleNode (“customers/customer [instance=’Jim’] “);

Console.WriteLine (n.Innertext); // Jim +Bo

The SelectXXX methods delegate their implementation to XPathNavigator which is used directly over XMLDocument or read-only XPathDocument

XElement e = e.XPathSelectElement(“customer/customer[firstname =’Jim’]”);

The extension method used with XNodes are CreateNavigator (); XPathEvaluate (); XPathSelectElement (); XpathSelectElements ();

Common XPath Operators are as follows

Operator | Description

/                              Children

//                            Recursively children

.                               CurrentNode

..                             ParentNode

*                            Wildcard

@                            Attribute

[]                             Filter

:                               namespace separator

XPathNavigator: It is a cursor over the XPathDataModel representation of an XML document It is loaded with the primitive methods that move the cursor around the tree

XPathNavigator Select * () take XPath string / queries and return more complex navigations or multiple nodes.

E.g. XPathNavigator nav = doc.CreateNavigator();

XPathNavigator jim = nav.SelectSingleNode(“customers/customer[firstname=’Jim’]”);

Console.WriteLine (jim.Value);

The SelectSingleNode method returns a single XPathNavigator. The Select method returning returns XPathNode Iterator which iterates over multiple XPathNavigators.

XPathNavigator nav = doc.CreateNavigator();

String xPath = “customers/customer/firstname/text()”;

Foreach (XPathNavigator nav in nav.Select(xPath))

Console.WriteLine (nav.Value)

For faster queries, compile XPath to XPathExpression then pass it to Select* method

XPathNavigator nav = doc.CreateNavigator ();

XPathExpression expr = nav.Compile (“customers/customer/firstname”);

Foreach (XPathNavigator a in nav.Select (expr))

Console.WriteLine (a.Value);

Output: Jim Thomas.

Querying with Namespace:

XmlDocument doc = new XmlDocument ();


XmlNameSpaceManager xnm = new XMLNamespaceManager (doc.NameTable);

We can add prefix/namespace pairs to it as follows:

Xnm.AddNamespace (“o”,”http://oreilly.com” );

The Select * methods on XMLDocument & XPathNavigator have overloads that accept as XMLNamespaceManager

XmlNode n =doc.SelectSingleNode (“o: customers/o: customers”, xnm);

XPathDocument: An XPathNavigator backed by an XPathDocument is faster than an XmlDocument but it cannot make changes to the underlying document:

XPathDocument doc = new XPathDocument (“customers.xml”);

XPathNavigator nav = doc.CreateNavigator ();

Foreach (XPathNavigator a in nav.Select (“customers/customer/firstname”))

Console.WriteLine (a.Value);

XSD and Schema Validation: For each domain XML file confirms to a pattern / schema to the standardize and automate the interpretation and validation of XML documents widely used is XSD (XML Schema Definition) which is supported in System.XML

Performing Schema Validation: You can validate an XML file on one or more schemas before processing it. The validation is done for following reasons

–          You can get away with less error checking and exception handling.

–          Schema validation pciks up errors you might otherwise overlook

–          Error messages are detailed and informative.

When XmlDocument is loaded into an XMLReader containing schema, validation happens automatically

Settings.ValidationType = ValidationType.Schema;


Using (xmlReader r = XmlReader.Create(“customers.xml”, settings))

Settings.ValidationFlags |= XmlSchemaValidationFlags.ProcessInlineSchema

if schema validation fails then XmlSchemaValidationException is thrown.


try {

While (r.Read());

} catch (XmlSchemaValidationException ex)



You want to report on all errors in the document, you must handle the ValidationEventhandler event;

Settings. ValidationEventHandler t = ValidationHandler;

Static void ValidationHandler(object sender, ValidationEventArgs e)


Console.WriteLine (“Error:”+e.Exception.Message);


The exception property of ValidationEventArgs contains the XmlSchemaValidationException that would have otherwise been thrown. You can also validate on XDocument or XElement that’s already in memory by calling extensions methods in System.XMLSchema. These methods accept XMLSchemaSet and a validationHandler


XMLSchemaSet set = new XMLSchemaSet ();

Set.Add (null,@”customer.xml”);

Doc.Validate (set, (sender, args) => {error.AppendLine (args.Exception.message);});

LINQ Queries:

Linq is a set of language and framework feature for constructing type safe queries over in-memory collections and remote data sources. It enables us to query a collection implementing IEnumerable<T>. LINQ offers both validations i.e. compile time and run time error checking.

The basic units of data in LINQ are sequences and elements. A sequence is any object that implements IEnumerable<T> and an element is each item in the sequence.

Query operators are methods that transform/project a sequence. In the Enumerable class in System.Linq there are around 40 query operators which are implemented as extension methods. These are called standard query operators.

Query operators over in-memory local objects are known as LINQ-to-Objects queries. LINQ also support sequence implementing IQueryable<T> interface and supported by standard query operators in Queryable class.

A query is an expression that transforms sequence with query operators e.g.

String[] names= {“Tom”, “Dick”, “Harry”};

IEnumerable<string> filteredNames = names.Where(n=>n.Length>=4)

Foreach(string name in filteredNames)


Next query operators accept lambda expression as an argument. Here it is the signature of where query operator.

Public static IEnumerable<TSource> where <TSource>(this IEnumerable<TSource>source, Func<TSource, bool> predicate)

C# also provides another syntax for writing queries called query expression syntax. IEnumerable<string>filteredNAmes from n in names where n.Contains(“a”) select n;

Chaining Query Operators: To build more complex queries you append additional query operators  to the expression creating a chain. E.g. IEnumerable<string> query = names. Where(n=>n.Contains(“a”))



Where, OrderBy and select are standard query operators that resolve to extension methods in the Enumerable class

Where operator: emits filtered verison of the input sequence.

Orderby operator: emits sorted version of the input sequence.

Select operator: emits a sequence where each input element is transformed or projected with a given lambda expression.

The following are the signatures of above 3 operators

public static IEnumerable<TSource> Where<TSource>(this IEnumerable<TSource>source, func<TSource, bool>predicate)

public static IEnumerable<TSource> OrderBy<TSource>(this IEnumerable<TSource>source, func<TSource, Tkey>keyselector)

public static IEnumerable<TResult> Select<TSource, TResult>(this IEnumerable <TSource>source, Func<TSource,TResult>selector);

without extension methods the query loses its fluency as shown below

IEnumerable<string> query = Enumerable.Select(Enumerable.OrderBy(Enumerable.Where( names, n => n.Contains(“a”)), n=>n.Length),n=>n.ToUpper());

Whereas if we use extension methods we get a natural linear shape reflect the left-to-right flow of data and keeping lambda expression alongside their query operators

IEnumerable<string> query = names.Where (n=>n.contains (“a”)).Orderby (n=>n.Length).Select (n=>n.ToUpper ());

The purpose of the lambda expression depends on the particular query operator. An expression returning a bool value is called a predicate. A lambda expression is a query operator always works on individual elements in the input sequence not the sequence as a whole.

Lambda expressions and Func signatures: The standard query operators utilize generic Func delegates. Func is a family of general purpose generic delegates in System.Linq, defined with the following intent: The tye arguments in Func appear in the same  order they do in lambda expression . Hence  Func<TSource, bool> matches TSoruce => bool Func<TSource, TResult> matches TSoruce => TResult.

The standard query operators use the following generic type names

TSource                ElementType for the input sequence

TResult                 ElementType for the output sequence if different from TSource.

TKey                      ElementType for the key used in sorting grouping or joining.

TSource is determined by the input sequence. TResult and they are inferred from your lambda expression. Func<TSource, TResult> is same as TSource=>TResult lambda expression. TSource and TResult are different types,  so the lambda expression can change the type of each  element, further the lambda expression  determines the output sequence type.

The where query operator is simpler and requires no type inference for the output because the operator merely filters elements it does not transform them.

The orderby query operator has a predicate/Key selector as Func<TSource, Tkey> maps an input element to a sorting key. This is inferred from lambda expression and is separate from the input and output element types.

Query operators in Enumerable class refer to methods instead of lambda expression to emit expression trees. Query operators in Queryable class refer to lambda expression to emit expression trees.

Natural Ordering: the original ordering of elements in input sequence is important  in LINQ. Operators such as Where and Select preserve the original ordering of the input sequence. LINQ preserves the ordering wherever possible.

Some of the operators which do not return sequence are as follows

Int numbers ={10,9,8,7,6};

Int firstnumber = numbers.First();

Int Lastnumber = numbers.Last();

Int secondnumber = numbers.ElementAt(1);

Int LowestNumber = number.OrderBy(n=>n).First();

The aggregation operators return a scalar value

Int count = numbers.Count();

Int min = numbers.Min();

The quantifiers return a bool value

Bool hasTheNumberNine = numbers.Contain(9);

Bool hasMorethanZeroElement = numbers.Any();

Bool hasAnOldElement = numbersAny(n=>n%2==1);

Some query operators accept two input sequence for e.g.

Int[] seq1 = {1,2,3}; Int[] seq1 = {3,4,5};

IEnumerable <int> concat = seq1.Concat(seq2);

IEnumerable <int> union = seq1.union(seq2);

C# provides a syntactic shortcut for writing LINQ queries called query expressions. Query expression always start with a form clause and ends with either a select or group clause. The from clause declares an range variable similar to traversing the input sequence.

e.g. IEnumerable<string> query = from n in names where n.contains(“a”) orderby n.length select n.toUpper();

Range Variables: The identifier immediately following the from keyword syntax is called the range variable refers to the current element in the sequence

Query expression also let you introduce new range variable via the following clauses: let into An additional from clause.

Query Syntax vs Fluent Syntax

Query syntax is simpler for queries that involve any of the following

  1. A let clause for introducing a new variable alongside the range variable.
  2. SelectMany, Join or GroupJoin, followed by an outer range variable reference.

Finally there are many operators that no keyword in query syntax. These require that you use fluent syntax. This means any operator outside of the following : where select selectmany orderby thenby orderbydescending thenbydescending groupby join groupjoin.

Mixed Syntax Queries: If a query operator has no query syntax support you can mix query syntax and fluent syntax. The only constraint is that each query syntax component must be complete.

Deferred Execution: An important feature of most query operators is that they execute not when constructed but when enumerated.

e.g. IEnumerable<int>query = numbers.Select(n=>*10)

foreach(int n in query)

Console.Write(n + “/”); //10 / 20

All standard query operators provide deferred execution with the following exceptions:

–          Operators that return a single element or scalar value such as First or Count

–          The following conversion operators toArray, ToList, ToDictionary, ToLookup cause immediate query execution because their result type have no mechanism for providing deferred execution.

Deferred Execution is important because its decouples query construction from query execution. This allows you to construct a query in several steps as well as making database queries possible.

A deferred execution query is reevaluated when you re-enumerate:

IEnumerate<int>query = numbers.Select(n=>n*10);

Foreach (int n in query) Console.Write(n+”/”); o/p= 10/20/


Foreach (int n in query) Console.Write(n+”/”); o/p = nothing

There are a couple of disadvantages:

Sometimes you want to freeze or cache the results at a certain point in time.

Some queries are computationally intensive so you don’t want to unnecessarily repeat them.

Query’s captured variable : Query’s lambda expression reference local variables these variables are subject to captured variable semantics. This means that if you later change their value, the query changes as well.

Int[] numbers = {1,2};

int factor =10;

IEnumerable<int> query = numbers.Select(n=>n*factor);

Factor =20;

Foreach(int n in query)Console.Write(n+”|”);//20|40|

A decorator sequence has no backing structure of its own to store elements. Instead it wraps another sequence that you supply at runtime to which it maintains a permanent dependency. Whenever you request data from a decorator, it in turn must request data from the wrapped input sequence.

Hence when you call an operator such as select or where you are doing nothing more than instantiating a enumerable class that decorates the input sequence.

Changing query operators create a layer of decorators When you enumerate query, you are querying the original array, transformed through a layering or chain of decorators.

Subqueries: A subquery is a query contained within another query’s lambda expression. E.g. string[] musos = {“David”,”Roger”,”Rick”}; IEnumerable<string>query = musos.Orderby(m=>m.split().last());

m.split() converts each string  into a collection of words upon which we then call the last query operator. M.split().last is the subquery; query references the outer query.

Subqueries are permitted because you can put any valid C# expression on the right hand side of a lambda. In a query expression, a subquery amounts to a query referenced from an expression in any clause except the from clause.

A subquery is primarily scoped to the enclosing expression and is able to reference the outer lambda argument ( or range variable in a query expression). A subquery is executed whenever the enclosing lambda expression is evaluated. Local queries follow this model literally interpreted queries follow this model conceptually. The sub query executes as and when required to feed the outer query.

An exception is when the sub query is correlated meaning that it references the outer range variable.

Sub queries are called indirectly through delegate in the case of a local query or through an expression tree in the case of an interpreted query.

Composition Strategies : 3 strategies for building more  complex queries

–          Progressive query construction

–          Using into keyword

–          Wrapping queries

There are a couple of potential benefits however to building queries progressively :

It can make queries easier to write

You can add query  operators conditionally For e.g.

If(includeFilter)query = query.Where(….)

This is more efficient than

Query = query.Where(n=>!includeFilter||expressions) because it avoids adding an extra query operator if includeFilter is false. A progressive approach is often useful in query comprehensions, In fluent syntax we could write this query as a single expression

IEnumerable<string>query = names.Select(n=>n.Replace(“a”,””) .Replace(“e”,””) .Replace(“i”,””) .Replace(“o”,””) .Replace(“u”,””)).where(n=>n.length>2).orderby(n=>n);


We can rewrite the query in progressive manner as follows

IEnumerable<string>query = from n in names.

Select n.Replace(“a”,””) .Replace(“e”,””) .Replace(“i”,””) .Replace(“o”,””) .Replace(“u”,””);

Query = from n in query where n.length > 2 orderby n select n;


The INTO keyword: The into keyword lets you continue a query after a projection and is a shortcut for progressively querying . With into we can rewrite the preceding query as :

IEnumerable<string> query = from n in names

Select n.Replace(“a”,””) .Replace(“e”,””) .Replace(“i”,””) .Replace(“o”,””) .Replace(“u”,””) into noVowel where noVowel.Length > 2 orderby noVowel select noVowel;

The only place you can use into is after a select or group clause “into” restarts a query allowing you to introduce fresh where, orderby and select clauses.

Scoping rules: All queries variables are out of scope following an into keyword. The following  willnot compile

Var query = from n1 in names select n1.Toupper() into n2 where n1.contains(“x”) select n2;

Here n1 is not in scope so above statement is illegal.

To see why,

Var query = names.Select(n1=>n1.Toupper())


Wrapping queries: A query built progressively can be formulated into single statement by wrapping one query around another query. In general terms:
var tempQuery = tempQueryExprn

Var finalQuery = from … in tempQuery can be reformulated as

Var finalQuery = from … in (tempQueryExprn).

Reformulated in wrapped form, it’s the following

IEnumerable<string> query = from n1 in (

From n2 in names

Select n2.Replace(“a”,””) .Replace(“e”,””) .Replace(“i”,””) .Replace(“o”,””) .Replace(“u”,””)).where n1.length>2 orderby n1.select n1

Projection Strategies: All our select clauses have projected scalar element types. With C# object initializers, you can project into complex types, for e.g. we can write the following class to assist:

Class TempProjectionITem


Public string Original;

Public string Vowelless;


And then project into it with object initializers:

String[] names = {“Tom”,”Dick”, “Harry”, “Mary”,”Jay”};

IEnumerable <TempProjectionItem>temp =  from n in names select new TempProjectionItem {

Original =n ,

Vowelless=n.Replace(“a”,””) .Replace(“e”,””) .Replace(“i”,””) .Replace(“o”,””) .Replace(“u”,””))};

The result is of the type IEnumerable<TempProjectionItem> which we can subsequently query

IEnumerable<string>query = from item in temp where item.Vowelless.length>2 select item.original;

This gives the same result as the previous example, but without needing to write one-off class. The compiler does the job instead, writing a temporary class with fields that match the structure of our projection. This means however that the intermediate query has the following type:


We can write the whole query more succinctly with the keyword

Var query=from n in names

Select new


Original =n,

Vowelless = n.Replace(“a”,””) .Replace(“e”,””) .Replace(“i”,””) .Replace(“o”,””) .Replace(“u”,””))} into temp where temp.Vowelless.Length>2 select tempOriginal;

The let keyword: introduces a new variable alongside the range variable. With Let we can write a query as follows

String[] names = {“Tom”, “Dick”, “Harry”, “Mary”,”Jay”};

IEnumerable<string>query = from n in names

Let vowelless = n.Replace(“a”,””) .Replace(“e”,””) .Replace(“i”,””) .Replace(“o”,””) .Replace(“u”,””)) .where vowelless.Length>2.orderby vowelless .select n;

The compiler resolves Let clause by projecting into a temporary anonymous type that contains both the range variable and new expression variable

Let accomplishes two things:

–          It projects new elements alongside existing elements

–          It allows an expression to be used repeatedly in a query without being rewritten.

Let approach is particularly advantageous in this example because it allows the select clause to project either the original name (n) or its vowel-removed version(v).

You can have any number of let statements. A let statement can reference variables introduced in earlier let statements. Let reprojects all existing variables transparently.

Interpreted Queries: LINQ provides two parallel architectures: Local queries for local object collections and interpreted queries for remote data sources. Local queries resolve to query operators in the enumerable class, which in turn resolve to chains of decorator sequences. The delegates that they accept whether expressed in query sysntax, fluent syntax or traditional delegates are fully local to IL code.

By contrast, interpreted queries are descriptive. They operate over sequences that implement IQuerable<T> and they resolve to the query operators in the Queryable class which emit expression trees that are interpreted at runtime.

These are two IQueryable<T> implementations in the .NET framework :



Create Table Customer


ID int not null primarykey,

Name varchar(30)


Insert customer values (1,”Tom”)

Insert customer values (2,”Dick”)

Insert customer values (3,”Harry”)

Insert customer values (4,”Mary”)

Insert customer values (5,”Jay”)

We can write Interpreted Query to retrieve customers whose name contains the letter “a” as follows

Using System;

Using System.Ling;

Using System.Data.Linq;

Using System.Data.Linq.Mapping;

[Table] public class Customer


[column(Isprimarykey=true)] public int ID;

[column]public string Name;


Class Test


Static void main()


Datacontext datacontext = new DataContext(“connection String”);

Table<customer>customers = dataContext.GetTable<Customer>();

IQueryable<string>query = from c in customers where c.Name.contains(“a”).orderby(c.Name.Length).select c.Name.ToUpper();

Foreach(string name in query)Console.WriteLine(name);



LINQ to SQL would be as follows

SELECT UPPER([to][Name])as[value] FROM[Customer]AS[to]WHERE[to].[Name]LIKE@po ORDER BY LEN([to].[Name])

Here customers is of type table<>, which implements IQueryable<T>. This means the compiler has a choice in resolving where it could call the extension method in Enumerable or the following extension method in Queryable:

Public static IQueryable<TSource>Where<TSource>(this IQueryable<TSource>source, Expression<Func<TSource,bool>>predicate)

The compiler chooses “Queryable.Where” bcoz its signature is a more specific match.

“Queryable.Where” accepts a predicate wrapped in an Expression<TDelegate> type. This instructs the compiler to translate  the supplied lambda expression in otherwords, n=>n.Name.contains(“a”) to an expression tree rather than a compiled delegate. An expression tree is an object model based on the types in System.Linq expression that can be inspected at runtime.

When you enumerate over an integrated query the outermost sequence runs a program that traverse the entire expression tree, processing it as a unit. In our example LINQ to SQL translates the expression tree to a SQL statement, which it then executes yielding the results as a sequence.

A query can include both interpreted and local operators. A typical pattern is to have the local operators on the outside and the interpreted components on the inside; this pattern works well with LINQ-to-DB queries.

AsEnumerable: Enumerable.AsEnumerable is the simplest of all query operators. Here its complete definition

Public static IEnumerable <TSource>AsEnumerable <TSource>(this IEnumerable<TSource>source)

{ return sources;}

Its purpose is to cast an IQueryable<T>sequence to IEnumerable<T> forcing subsequent query operators to bind to Enumerable operators instead of Queryable operators. This causes the  instead of Queryable operators. This causes the remainder of the query to execute locally.


Regex wordcounter = new Regex(@”\b(\w[-]+]\b”);

Var query =dataContext.MedicalArticales

.where(article=>article.Topic == “influenza”)


.where(article=>wordCounter.Matches(article.Abstract).Count <100);

An alternative to calling AsEnumerable is to call toArray or toList. The advantage of AsEnumerable is deferred execution.


5 thoughts on “C# 4.0 new Features.

  1. Thank you for your blog post. Velupe and I have already been saving to get a new publication on this subject and your article has made us to save the money. Your notions really solved all our concerns. In fact, more than what we had acknowledged just before we ran into your excellent blog. My spouse and i no longer nurture doubts and a troubled mind because you have actually attended to our needs above. Thanks

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s