Understanding ref and out + C#7.0 features
ref and out - these are used in situations where some methods required to return multiple values from the method parameter list itself and not using 'return' keyword, where method does not have 'return' keyword.
Difference is -
1:ref variables must and should initialize or assigned the values before calling the method
2:out variables must be initialize or assigned the values inside the method. That is before leaving the method. In C#6.0, the out variables must and should be declared before calling the method. But in c#7.0, the out variables can directly declared inside the method parameters
c# 6.0
-------
int x , int y;
obj.Math1(out x , out y);
Here before using the variable it must be declared.
c# 7.0
-------
obj.Math(out int x , out int y);
Here we no need to declared it before using it in method parameters
Tuple c# 6
=======
Tuple - these are used in situations where some methods required to return multiple values from the method, where method does have 'return' keyword. Here method's return type will be of two types. Ex: if you want to return sum and product two values in a single method then below is the example below. Here there is no string naming convention when accessing object.members
In c#7.0 - Tuple class object is implicitly created so we no need to use Tuple keyword or create explicitly tuple object any where in the method. Here there is strong naming convention when access the object.members
strong naming convention in c#7.0
Local functions - This is same as normal methods. Main purpose or usage is mostly using inside method itself and if there is no requirement to be called outside of the method then Local functions are useful. We all know that class can have normal methods so if you write Local functions then you wont see any changes or same as old way of defining method.
This is mainly use full when you want to write method inside method. Example static void main() is method. Now inside static void main(), if you want write method then first you need to create class and define method. Inside Main() create object of that class and then only access its method. This can be avoided by using Local function
This type of functions we can write it using ananymous methods,delegates,lambda expressions,Fucn,Action and Predicate. Then what is use of Local functions. The main disadvantage in ananymous methods,delegates,lambda expressions,Fucn,Action and Predicate : is that we cannot use below things
we cannot use Ref, out, params, generics [ i mean use T ]
But these Ref, out, params, generics [ i mean use T ] are possible in Local functions
If you want to know c# version then type like "#error version" anywhere in project and try to run. It will show the version.
ref local and ref return - Earlier ref was used only on method parameters. But in c# 7.0, ref can be used on local variables also.ref local can be used only on arrays. It is used to modify the array contents. earlier if we want to modify array values then we need to access thru index position and then modify the contents of the array.
ref return- Earlier ref was used only on method parameters. But in c# 7.0, ref can be used on local variables also.ref return can be used only on arrays. It is used to modify the array contents. earlier if we want to modify array values then we need to access thru index position and then modify the contents of the array.
pattern-Matching
Digit separator and Binary literals
Comments
Post a Comment