Literal Improvement in C# 7.0

C# 7.0 now allows using underscore _ to be used a digit separator.

This adds more readability.

Similarly, we now have Binary Literals, so instead of specifying hexadecimal pattern we can specify bit patterns.

Hope it helps..

Using Out Variables (C# 7.0) instead of Out Parameters

Out Parameters must need to declared before they are used in the method which doesn’t seem intuitive.

Now with C# 7.0 we can now make use of Out variable.

Here in our second call to the method we have used out variable named outVariableString which we didn’t have to declare like output parameter.

Hope it helps..

Tuple Types and Tuple Literals in C# 7.0

Tuple are used when we need to get more than one value returned from the method.

C# 7.0 introduces Tuple Types and Tuple Literals to Tuples

We need to add System.ValueType Nuget Package to add the required assemblies to Visual Studio.

Below sample code shows usage of Tuple type and literal. In our second method, we have named the part of the tuple i.e. name and number.

Now to access the first method we use Item1 and Item2 field, however for our second method as we have named the part we can use those names to access them i.e. name and number.

Output in both the case

Hope it helps..

New Feature – Local Functions in C# 7.0

C# 7.0 adds a new feature called local functions.

Local functions allow us to define function within a function. It can be used to write helper methods that we need to use inside that function’s scope.

Here in the below sample code we have defined one main function (MainFunction) that has one local function(MultiplyBy10LocalFunction) defined inside it. And that local function has one more local function(Add10LocalFunction) defined inside it.

Hope it helps..

Sample Code to consume UNData API using JavaScript

Sharing a sample code that can be used to consume UNData API

The below code fetches the population data for country India.

Sample Code

var xmlHttp;
// creatng the xmlHttp object
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
// Calling the web service using post and true means asynchronous call"POST", "", false);

// Setting the request header to let the web service find the soap request we would be sending
xmlHttp.setRequestHeader("Content-Type", "text/xml; charset=utf-8");

xmlHttp.setRequestHeader("SOAPAction", "");

var soapRequest = "<!--?<span class="hiddenSpellError" pre="" data-mce-bogus="1"-->xml version='1.0' encoding='utf-8'?>"+
"<soap:Envelope xmlns:soap='' xmlns:xsi='' xmlns:xsd=''>"+
"<GetCompactData xmlns=''>"+
"<message:QueryMessage xsi:schemaLocation='' xmlns:generic='' xmlns:message='' xmlns:query='' xmlns:xsi=''>"+
"<message:Sender id='online-help' />"+
"<query:Dimension id='INDICATOR'>SP_POP_TOTL</query:Dimension>"+
"<query:Dimension id='AGE'>_T</query:Dimension>"+
"<query:Dimension id='SEX'>_T</query:Dimension>"+
"<query:Dimension id='LOCATION'>_T</query:Dimension>" +
"<query:Dimension id='REF_AREA'>DEU</query:Dimension>" +
"<query:Dimension id='SCENARIO'>H</query:Dimension>"+
"</soap:Body>" +

var xmlDoc = jQuery.parseXML(xmlHttp.responseText);
var popValue = xmlDoc.getElementsByTagName('ns1:Obs')['0'].attributes['OBS_VALUE'].nodeValue;
alert("Total Population " + popValue);


<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap=""
<Sender id="1B0">
<Name xml:lang="en">United Nations Statistics Division</Name>
<Source xml:lang="en">
United Nations Population Division World Population Prospects</Source>
<ns1:DataSet DATASET_NAME="World Population Prospects"
DATASET_SOURCE="United Nations Population Division"
DATASET_DESC="The United Nations World Population Prospects contains estimates and projections for every country in the world, including estimates and projections of demographic indicators such as birth rates, deaths rates, infant mortality rates and life expectancy." UNDATA_LASTUPDATE="20 Aug 2013" UNDATA_NEXTUPDATE="2015 release" METADATA=""><ns1:Series AGE="_T" FREQ="A" INDICATOR="SP_POP_TOTL" LOCATION="_T" SEX="_T" REF_AREA="DEU" SCENARIO="H"><ns1:Obs TIME_PERIOD="2015" OBS_VALUE="83178.615" UNIT="NUMBER" UNIT_MULT="3" TIME_SPAN="2015" /></ns1:Series></ns1:DataSet></CompactData></GetCompactDataResult></GetCompactDataResponse></soap:Body></soap:Envelope>

Hope it helps.

Sample code to consume UNData API in C#


Recently we were working on an application that had to fetch country specific details. For this we used the UNData API.

We can make use of SDMX Browser in helping us to write a query

Create a console app\windows application and add web reference to the following web service

Below is the sample C# Code

string queryMsg = @"<message:QueryMessage
<message:Sender id='online-help'/>
<query:Dimension id='INDICATOR'>SP_POP_TOTL</query:Dimension>
<query:Dimension id='AGE'>_T</query:Dimension>
<query:Dimension id='SEX'>_T</query:Dimension>
<query:Dimension id='LOCATION'>_T</query:Dimension>
<query:Dimension id='REF_AREA'>IND</query:Dimension>
NSIEstatV20Service service = new NSIEstatV20Service();
System.Xml.XmlDocument queryMessageDocument = new XmlDocument();
System.Xml.XmlNode queryMessage = queryMessageDocument.DocumentElement;
var result = service.GetCompactData(queryMessage);

The output

The query here basically returns the Total population of India

Hope it helps..