Thursday, December 28, 2017

C# 6.0 features:
1. using static System.Console;
Example:
 using static System.Console;
 class Program
    {
        static void Main(string[] args)
        {
            WriteLine("Hello World!");
        }
    }


2. String interpolation.
Example:
WriteLine($"{firstName} {lastName} is my name!");

3. Null conditional operators.

4. Getter only auto properties.

5. Auto property initializers in C#

6. Parameterless constructor for structs

7. Dictionary Initialisation

8. await in catch and finally

9. Exception Filters
Example:
C#5.0:
catch (Exception ex)
{
if (ex.Message.Equals("500"))
Write("Bad Request");
}
C# 6.0
catch (Exception ex) when (ex.Message.Equals("400"))
{
Write("Bad Request");
ReadLine();
}

C# 7.0 features:
1. Local function / nested function
2. We can use out variable on the fly,
Example:
static void Main(string[] args) {
        string s = "26-Nov-2016";
        if (DateTime.TryParse(s, out DateTime date))
{
            WriteLine(date);
        }
        WriteLine(date);
    }
3. Tuples
4. Expression bodied methods / Constructors / getssers / setters
5. Throw expressions

Invoke Vs BeginInvoke:
Delegate.Invoke: Executes synchronously, on the same thread.
Delegate.BeginInvoke: Executes asynchronously, on a threadpool thread.
Control.Invoke: Executes on the UI thread, but calling thread waits for completion before continuing.
Control.BeginInvoke: Executes on the UI thread, and calling thread doesn't wait for completion.

Dependency Injection:
1.
Interface ILogger
{
OpenLog();
Log();
CloseLog();
}

2.
Class FileLogger() : ILogger
{
public string LogFileName { get; private set; }

public FileLogger(string logFileName)
{
LogFileName = logFileName;
}

public void OpenLog()
{
//
}

public void Log()
{
//
}

public void CloseLog()
{
//
}
}

3.
Class LoggerEngine()
{
ILogger _logger;

public LoggerEngine(ILogger logger)
{
_logger = logger;
}

public void log(string message)
{
_logger.OpenLog();
_logger.Log(message);
_logger.CloseLog();
}
}

4.
Create Client Class IoCThirdParty
{
public string FileName { get; set; }

public void Write(string message)
{
Logger ip = new Logger(FileName);
var obj = new LoggerEngine(ip);
obj.log(message);
}
}

5.
Use Client Class IoCThirdParty
class Use
{
static void main()
{
IoCThirdParty obj = new IoCThirdParty(){FileName="C:\log.txt"};
obj.Write("Hello man!");
}
}

No comments:

Post a Comment