Wednesday, November 28, 2018

ref Keyword Vs out Keyword


ref Keyword Vs out Keyword
The ref modifier means that:
  1. The value is already set and
  2. The method can read and modify it.
The out modifier means that:
  1. The Value isn't set and can't be read by the method until it is set.
  2. The method must set it before returning.

Ref

The ref keyword is used to pass an argument as a reference. This means that when value of that parameter is changed in the method, it gets reflected in the calling method. An argument that is passed using a ref keyword must be initialized in the calling method before it is passed to the called method.

Out

The out keyword is also used to pass an argument like ref keyword, but the argument can be passed without assigning any value to it. An argument that is passed using an out keyword must be initialized in the called method before it returns back to calling method.

Program with ref and out keyword

  1. public class Example
  2. {
  3. public static void Main() //calling method
  4. {
  5. int val1 = 0; //must be initialized
  6. int val2; //optional
  7.  
  8. Example1(ref val1);
  9. Console.WriteLine(val1); // val1=1
  10.  
  11. Example2(out val2);
  12. Console.WriteLine(val2); // val2=2
  13. }
  14.  
  15. static void Example1(ref int value) //called method
  16. {
  17. value = 1;
  18. }
  19. static void Example2(out int value) //called method
  20. {
  21. value = 2; //must be initialized
  22. }
  23. }
  24.  
  25. /* Output
  26. 1
  27. 2
  28. */

Note


  1. Do not be confused with the concept of passing by reference and the concept of reference type. These two concepts are not the same.
  2. A value type or a reference type can be passed to method parameter by using ref keyword. There is no boxing of a value type when it is passed by reference.
  3. Properties cannot be passed to ref or out parameters since internally they are functions and not members/variables.

is Keyword VS as Keyword

is Keyword VS as Keyword

is

The is operator checks if an object can be cast to a specific type.
Example:
if (someObject is StringBuilder) ...

as

The as operator attempts to cast an object to a specific type, and returns null if it fails.
Example:
StringBuilder b = someObject as StringBuilder;
if (b != null) ...
as Operator :
The AS operator also checks whether the type of an given object is compatible with the new object type. This keyword will checks whether the type of an given object is compatible with the new object type. If its not compatible with new one then it will return NULL.
object o = "somestring";
string str = o as string;
is Operator:

This Operator will checks weather type of an object is compatible with the new object. If its compatible it returns true otherwise false.

object ocust = new Customer();

if (ocust is Customer)
{ 

Keywords in C#



Keywords in C# 


Modifier Keywords

Modifier keywords are certain keywords that indicate who can modify types and type members. Modifiers allow or prevent certain parts of programs from being modified by other parts.
Modifier keywords
abstract
async
const
event
extern
new
override
partial
readonly
sealed
static
unsafe
virtual
volatile

Access Modifier Keywords:

Access modifiers are applied on the declaration of the class, method, properties, fields and other members. They define the accessibility of the class and its members.
Access ModifiersUsage
publicThe Public modifier allows any part of the program in the same assembly or another assembly to access the type and its members.
privateThe Private modifier restricts other parts of the program from accessing the type and its members. Only code in the same class or struct can access it.
internalThe Internal modifier allows other program code in the same assembly to access the type or its members. This is default access modifiers if no modifier is specified.
protectedThe Protected modifier allows codes in the same class or a class that derives from that class to access the type or its members.

Statement Keywords

Statement keywords are related to program flow.
Statement Keywords
if
else
switch
case
do
for
foreach
in
while
break
continue
default
goto
return
yield
throw
try
catch
finally
checked
unchecked
fixed
lock

Method Parameter Keywords

These keywords are applied on the parameters of a method.
Method Parameter Keywords
params
ref
out

Namespace Keywords

These keywords are applied with namespace and related operators.
Namespace Keywords
using
. operator
:: operator
extern alias

Operator Keywords

Operator keywords perform miscellaneous actions.
Operator Keywords
as
await
is
new
sizeof
typeof
stackalloc
checked
unchecked

Access Keywords

Access keywords are used to access the containing class or the base class of an object or class.
Access keywords
base
this

Literal Keywords

Literal keywords apply to the current instance or value of an object.
Literal Keywords
null
false
true
value
void

Type Keywords

Type keywords are used for data types.
Type keywords
bool
byte
char
class
decimal
double
enum
float
int
long
sbyte
short
string
struct
uint
ulong
ushort

Contextual Keywords

Contextual keywords are considered as keywords, only if used in certain contexts. They are not reserved and so can be used as names or identifiers.
Contextual Keywords
add
var
dynamic
global
set
value
Contextual keywords are not converted into blue color (default color for keywords in visual studio) when used as an identifier in Visual Studio. For example, var in the below figure is not in blue color whereas color of this is blue color. So var is a contextual keyword.




C# Keywords color in Visual Studio
C# Keywords

Query Keywords

Query keywords are contextual keywords used in LINQ queries.
Query Keywords
from
where
select
group
into
orderby
join
let
in
on
equals
by
ascending
descending

ref Keyword Vs out Keyword

ref Keyword Vs out Keyword The  ref  modifier means that: The value is already set and The method can read and modify it. The  ou...