public void Method(out int i) { } //Error
public void Method(ref int i) { }//Error
We Cannot Overload Method with only difference as out and ref. It will
throw Compiler Error.(Member with Same Signature Exist).
Category: Basics
#5 Ref and Out Parameters
int NormalVaribale = 1;
int referencevariable = 1;
int outvaraibale = 1;
Normalmethod(NormalVaribale);
referencemethod(ref referencevariable);
outmethod(out outvaraibale);
Console.WriteLine("Normal Variable"+NormalVaribale);
Console.WriteLine("(REF)Reference Variable"+referencevariable);
Console.WriteLine("(out) Out Varaible"+outvaraibale);
ref :Value must be Initialized before it is passed to the method. Reference is being passed to the method,so the value changed will get reflected./*It has got Nothing to do with value type and reference type both of these can use ref*/
out:Value need not be Assigned,but has to be defined before the method exists.
Note:Even though you have initialized before passing to method,you got to assign value before the method exists.
#4 Static Constructors
1.Static Constructors cannot have Access Modifiers or have parameters.
2.They are initialized before any static members are referenced.They are the First One to get Initialized.
3.A non static class can have static constructor.
class Test
{
static Test() //cannont have access modifier
{
Console.WriteLine("Hello World");
}
}
#2:Enum With Flags
Enum can take Flags as attribute and perform bit operations over the numerics(Values must be power of two).
[Flags]
public enum Color
{
Red=1,-->0001
Yellow=2,--->0010
Green=4--->0100
}
var combination = Color.Red | Color.Green;
Console.WriteLine(combination); /* Red,Green*/
They are helpful when you need to hold multiple values/Combination of values defined in an Enum.
#1:Enum Basic
public enum Color
{
Red,
Yellow,
Green
}
Enum are Stored as Constants Internally.i.e each name gets mapped to corresponding value.