Pages

19 Mar 2016

C# 6.0 Language Features

 C# 6.0 Language Features

1) Expression Bodied Methods

How many times have you had to write a method just for one line of code? Now, with C# 6 you can simply create an expression bodied member with only the expression and without the curly braces or explicit returns.
class Employee
{
   // Method with only the expression
   public static int
      CalculateMonthlyPay(int dailyWage)
      => dailyWage * 30;
}
2) ?—Conditional Access Operator
In earlier versions of the C# language, you always had to write the explicit if condition NULL checks before using an object or its property, as shown below.
private void GetMiddleName(Employee employee)
{
   string employeeMiddleName = "N/A";
 
   if (employee != null && employee.EmployeeProfile
      != null)
      employeeMiddleName =
         employee.EmployeeProfile.MiddleName;
}
The same can be converted into a one-liner by using the Conditional Access Operator in C# 6.
private void GetMiddleName(Employee employee)
{
   string employeeMiddleName =
      employee?.EmployeeProfile?.MiddleName ?? "N/A";
}

3) Auto-Property Initializers

With the Auto-Property initialization feature, the developer can initialize properties without using a private set or the need for a local variable. Following is the sample source code.
class PeopleManager
{
   public List Roles { get; } =
      new List() { "Employee", "Managerial"};
}

4) Primary Constructor

Primary Constructor is a feature in which you are allowed to pass the constructor parameters at the class declaration level instead of writing a separate constructor. The scope of the primary constructor parameters values is class level and will be available only at the time of class initialization. It comes to good use when it is used with the Auto-Property initializers.
// Primary constructor
class Basket(string item, int price)
{
   // Using primary constructor parameter values
   // to do auto property initialization.
   public string Item { get; } = item;
   public int Price { get; } = price;
}

5) OUT Parameter Declaration During Method Call

This is one of my favorites because I was feeling something not good about the separate declaration of the OUT parameter before the method call. This feature allows you to declare the OUT parameter during the method call, as shown below.
public bool ConvertToIntegerAndCheckForGreaterThan10
   (string value)
{
   if (int.TryParse(value, out int convertedValue)
      && convertedValue > 10)
   {
      return true;
   }
 
   return false;
}

6) Await in the Catch Block

This is an important non-syntactic enhancement that will be available in C# 6. The await keyword can be called inside the catch and finally blocks. This opens up the way to perform an async exception handling or fallback process in case an exception happened during an async process call.
public async void Process()
{
   try
   {
      Processor processor = new Processor();
      await processor.ProccessAsync();
   }
   catch (Exception exception)
   {
      ExceptionLogger logger = new ExceptionLogger();
      // Catch operation also can be aync now!!
      await logger.HandleExceptionAsync(exception);
   }
}

7) Exception Filters

Exceptions can be filtered in the catch blocks with ease and cleanly with C# 6. Following is a sample source code where the intention is to handle all Exceptions except the SqlException type.
public async void Process()
{
   try
   {
      DataProcessor processor = ne
   }
   // Catches and handles only non sql exceptions
   catch (Exception exception) if(exception.GetType()
      != typeof(SqlException))
   {
      ExceptionLogger logger = new ExceptionLogger();
      logger.HandleException(exception);
   }
}

8) Using Is Allowed

This feature is something to make your code less cluttered and will reduce duplications. As with the namespaces, you can include a static class in the using statement similar to a namespace.
using System;
// A static class inclusion
using System.Console;
 
namespace CSharp6Demo
{
   class Program
   {
      static void Main(string[] args)
      {
         WriteLine("Console. is not required
            as it is included in the usings!");
      }
   }
}

9) String Interpolation

It can be looked at as an improvement to the String.Format functionality where, instead of the place holders, you can directly mention the string expressions or variables.
static void Main(string[] args)
{
   string name = "Robert";
   string car = "Audi";
   WriteLine("\{name}'s favourite car is
      {car}!");
}



UWP New features for developer

Here is the  list of what's new in Windows 10.
  • Application model
    • File Explorer
    • Shared Storage
    • Settings
  • Controls
    • WebView updates
    • Client-side data validation for user input
    • Windows core text APIs
    • Input updates
    • Maps
  • Devices
    • New Location method
    • AllJoyn support
    • Battery APIs
    • Support for MIDI devices
    • Custom sensor support
  • Graphics and games
    • Support for DirectX 12
  • Media
    • HTTP live streaming
    • Media Foundation Transcode Video Processor (XVP) support for Media Foundation Transforms (MFTs)
    • New Transcoding API
    • Updated MediaElement
    • Media transport controls for desktop apps
      Random access JPEF encoding and decoding
    • Support for Overlays for media compositions
    • New effects framework
  • Networking
    • Socket updates
    • New APIs for Background transfer post-processing tasks
    • Bluetooth support for ads
    • Wifi Direct API upcate
    • Improvements in JSON support
  • Security
    • ECC encryption
  • System services
    • Power change notifications
    • Version Helper functions
  • Storage
    • File-search APIs for Windows Phone
  • Tools and performance
    • Property-change notifications
    • Trace logging
  • User Experience
    • List scrolling virtualization
    • Drag-and-drop capabilities between different application platforms
    • Keyboard acceleration support for keystroke navigation
  • Internet Explorer
    • Support for Edge mode (living document mode) for maximum interoperability with other modern browsers

8 Mar 2016

Freeing Disk Space on C:\ Windows Server 2008

Freeing Disk Space on C:\ Windows Server 2008


I just spent the last little while trying to clear space on our servers in order to install .NET 4.5. Decided to post so my future self can find the information when I next have to do this.
I performed all the usual tasks:

  • Deleting any files/folders from C:\windows\temp and C:\Users\%UserName%\AppData\Local\Temp
  • Delete all EventViewer logs
    • Save to another Disk if you want to keep them
  • Remove any unused programs, e.g. Firefox
  • Remove anything in C:\inetpub\logs
  • Remove any file/folders C:\Windows\System32\LogFiles
  • Remove any file/folders from C:\Users\%UserName%\Downloads
  • Remove any file/folders able to be removed from C:\Users\%UserName%\Desktop
  • Remove any file/folders able to be removed from C:\Users\%UserName%\My Documents
  • Stop Windows Update service and remove all files/folders from C:\Windows\SoftwareDistribution
  • Deleting an Event Logs
  • Run COMPCLN.exe
  • Move the Virtual Memory file to another disk