Wednesday, 2 August 2017

Passing By Value Vs By Ref C#



Passing By Value Vs By Ref C#

 

A value-type variable contains its data directly as opposed to a reference-type variable, which contains a reference to its data. Passing a value-type variable to a method by value means passing a copy of the variable to the method. Any changes to the parameter that take place inside the method have no effect on the original data stored in the argument variable. If you want the called method to change the value of the parameter, you must pass it by reference, using the ref or out keyword. For simplicity, the following examples use ref.

Bool, byte, char, decimal, double, enum, float, int, long, sbyte, short, struct, uint, ulong, ushort


Class, delegate, dynamic, interface, object, string

C# Example
private void button1_Click(object sender, EventArgs e)
{
     int i = 100;

     PassByVal(i);   // Don't change original value
     button1.Text = i.ToString(); // OutPut is : 100

     PassByRef(ref i);   // Change original value
     button1.Text = i.ToString(); // OutPut is : 110
}

private void PassByRef(ref int i) { i += 10; }

private void PassByVal(int i) { i += 10; }

More Difference

Value Type
Reference Type
They are stored on stack
They are stored on heap
Contains actual value
Contains reference to a value
Cannot contain null values. However this can be achieved by nullable types
Can contain null values.
Value type is popped on its own from stack when they go out of scope.
Required garbage collector to free memory.
Memory is allocated at compile time
 Memory is allocated at run time

.NET Core or .NET Framework, What is right for your application?



.NET Core or .NET Framework, What is right for your application?

 

.NET Framework and .NET Core are two choices for building .NET server-side applications. Both share a lot of the same .NET platform components and you can share code across the two. However, there are fundamental differences between the two and based on what you want to accomplish, your requirement your choice will depend. 

You should use .NET Core for your server application when:
·  You have cross-platform needs.
·  You are targeting micro services.
·  You are using Dockers containers.
·  You need high performance and scalable systems.
·  You need side by side of .NET versions by application.
·   
You should use .NET Framework for your server application when:

·  Your application currently uses .NET Framework (recommendation is to extend instead of migrating)
·  You need to use third-party .NET libraries or Nuget packages not available for .NET Core.
·  You need to use .NET technologies that are not available for .NET Core.
·  You need to use a platform that doesn’t support .NET Core.
·   

Find 2nd highest salary using sql and LINQ


Find 2nd highest salary using sql
Select TOP 1 Salary
from (SELECT DISTINCT TOP 2 Salary from Employee ORDER BY Salary ASC)
a ORDER BY Salary DESC

Find 2nd highest salary using LINQ

var employee = Employees
                         .OrderByDescending(e => e.Salary)
                         .Skip(1)  //skip(N-1) N is 2
                         .First();

If multiple employees may have equal salary and you wish to return an IEnumerable of all the employees with the second-highest salary you could do

var employees = Employees
                           .GroupBy(e => e.Salary)
                          .OrderByDescending(g => g.Key)
                          .Skip(1)
                           .First();