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..

Step by step – Upload files to Azure Blob storage

Here we will be creating a storage account of type blob storage and a container inside it. Then we will create a console application, add required nuget packages and upload a file to the container.

Log in to Azure Portal

https://portal.azure.com

Click on Add to add a new storage account.

Create a new container in it to store the blob files

Now we’d write a console app to connect to this container and upload a file.

Create a new console application and add references to below Nuget Packages.

  • Windows.Azure.Storage
  • Windows.Azure.ConfigurationManager

In Azure Portal – Storage Account, go to Access Keys and copy the connection strings for the storage account.

Inside console application add an appSettings section and add a key and paste the above copied connection string there.

The source code to upload the Blob file

// Retrieve storage account from connection string.
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(
CloudConfigurationManager.GetSetting("StorageConnectionString"));

// Create the blob client.
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();

// Retrieve a reference to a container.
CloudBlobContainer container = blobClient.GetContainerReference("myblogcontainer");

// Retrieve reference to a blob named "myblob".
CloudBlockBlob blockBlob = container.GetBlockBlobReference("WeekendChamps.jpg");

// Create or overwrite the "myblob" blob with contents from a local file.
using (var fileStream = System.IO.File.OpenRead(@"C:\Users\Bliss\Downloads\WeekendChamps.jpg"))
{
blockBlob.UploadFromStream(fileStream);
}

The file uploaded in the container

Hope it helps..

Insert a Chart in Excel Spreadsheet using EPPlus Library.

EPPlus is .NET Library that makes it very easy to manipulate Excel programmatically. It is based on Open XML.

https://epplus.codeplex.com/

Below is the sample code we can use to insert a chart in a excel spreadsheet.


public void UpdateExcelUsingEPPlus(string fileName)
{
FileInfo fileInfo = new FileInfo(fileName);

ExcelPackage p = new ExcelPackage(fileInfo);

// access the first sheet named Sheet1
ExcelWorksheet myWorksheet = p.Workbook.Worksheets["Sheet1"];

// specify cell values to be used for generating chart.
myWorksheet.Cells["C2"].Value = 10;
myWorksheet.Cells["C3"].Value = 40;
myWorksheet.Cells["C4"].Value = 30;

myWorksheet.Cells["B2"].Value = "Yes";
myWorksheet.Cells["B3"].Value = "No";
myWorksheet.Cells["B4"].Value = "NA";

// add chart of type Pie.
var myChart = myWorksheet.Drawings.AddChart("chart", eChartType.Pie);

// Define series for the chart
var series = myChart.Series.Add("C2: C4", "B2: B4");
myChart.Border.Fill.Color = System.Drawing.Color.Green;
myChart.Title.Text = "My Chart";
myChart.SetSize(400, 400);

// Add to 6th row and to the 6th column
myChart.SetPosition(6, 0, 6, 0);

p.Save();

}

The output –

If we want to do it using Open XML SDK without using EPPlus Library we can refer to the below article.

https://msdn.microsoft.com/en-us/library/office/cc820055.aspx

Hope it helps..