A. What is new in c#6.0.
1. Null-Conditional Operatorexample :
public static string Truncate(string value, int length)
{
return value?.Substring(0, Math.Min(value.Length, length));
}
public void Truncate_WithNull_ReturnsNull()
{
Assert.AreEqual<string>(null, Truncate(null, 42));
}
2. Primary Constructor:primary constructors will help destroy the pain of capturing values of constructor parameters to fields in the class for further operations. This is really useful. The main purpose for it is using constructor parameters for initialization. When declaring a primary constructor all other constructors must call the primary constructor using :this().
class Person(string firstName, string lastName)
{
public string FirstName { get; set; } = firstName;
public string LastName { get; } = lastName;
}
3. Auto-Property Initializers
In C# 6 and later, you can initialize auto-implemented properties similarly to fields:
eg. public string FirstName { get; set; } = "Jane";
eg. public string FirstName { get; set; } = "Jane";
B. What is new in c#5.0.
C# 5.0 was released on August 15, 2012 .
1. Async Programming.
C# 5.0 Async feature introduces two keywords async and await which allows you to write asynchronous code more easily and intuitively like as synchronous code. Before C# 5.0, for writing an asynchronous code, you need to define callbacks (also known as continuations) to capture what happens after an asynchronous process finishes. This makes your code and other routine task such exception handling complicated.
Both the keywords are used in a combination of each other. Hence, an await operator is applied to a one or more than one expressions of an async method. An async method returns a Task or Task<TResult> that represents the ongoing work of the method. The task contains information that the caller of the asynchronous method can use, such as the status of the task, its unique ID, and the method's result.
2. Caller Information.
Caller Information can help you in tracing, debugging and creating diagnose tools. It will help you to avoid duplicate codes which are generally invoked in many methods for same purpose, such as logging and tracing.
You can get the following information of caller methods
1. CallerFilePathAttribute
Full path of the source file that contains the caller. This is the file path at compile time.
2. CallerLineNumberAttribute
Line number in the source file at which the method is called.
3. CallerMemberNameAttribute
Method or property name of the caller.
using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Main method Start");
MyMethodB();
MyMethodA();
Console.WriteLine("Main method End!");
Console.ReadLine();
}
static void MyMethodA()
{
MyMethodB();
}
static void MyMethodB([CallerMemberName] string memberName = "", [CallerFilePath] string sourceFilePath = "", [CallerLineNumber] int sourceLineNumber = 0)
{
InsertLog(memberName+"Line no:"+sourceLineNumber.ToString()+" file path:"+sourceFilePath.ToString());
}
static void InsertLog(string method)
{
Console.WriteLine("{0} called MyMethodB at {1}", method, DateTime.Now);
}
}
}
No comments:
Post a Comment