Enums in C#
Enums are basically a set of named constants. i.e. these are basically a group of named constants defined under a common name.
Enums are value types and are created on the stack and not on the heap.
Eg:
using System;
public enum Colors
{
red,
green,
blue,
}
public class Mainclass
{
public static void Main()
{
string color = Colors.red.ToString();
Console.WriteLine("Color is:" + color);
int id = (int)Colors.blue;
Console.WriteLine("Color value:" + id);
}
}
Each enum type has a corresponding value type called underlying type of the enum type.
Friday, August 20, 2010
Unsafe Code ( Using Pointer) in C#
Unsafe Code ( Using Pointer in C#)
C#.NET hides most of memory management, but in some cases we need direct access to the memory, then unsafe code is used.
Eg:
using System;
class Mainclass
{
public static void Main()
{
int a = 5;
unsafe
{
int* ptr;
ptr = &a;
*ptr = 20;
}
Console.WriteLine("a = " + a);
}
}
Eg:
using System;
unsafe class Mainclass
{
public static void Main()
{
int a = 5;
int* ptr;
ptr = &a;
*ptr = 20;
Console.WriteLine("a = " + a);
}
}
C#.NET hides most of memory management, but in some cases we need direct access to the memory, then unsafe code is used.
Eg:
using System;
class Mainclass
{
public static void Main()
{
int a = 5;
unsafe
{
int* ptr;
ptr = &a;
*ptr = 20;
}
Console.WriteLine("a = " + a);
}
}
Eg:
using System;
unsafe class Mainclass
{
public static void Main()
{
int a = 5;
int* ptr;
ptr = &a;
*ptr = 20;
Console.WriteLine("a = " + a);
}
}
Thursday, August 19, 2010
Parameter Arrays in C#
Parameter array.
Parameter arrays allows us to pass a variant number of arguments into a method. All incoming arguments will consider as a single dimensional array. Have a look on the following example.
using System;
class Mainclass
{
public static void Main()
{
Test obj = new Test();
obj.Add(10, 20);
obj.Add(10, 20, 30);
obj.Add(1, 2, 3, 4, 5, 6);
}
}
class Test
{
public void Add(params int[] ar)
{
int sum = 0;
foreach (int i in ar)
{
sum = sum + i;
}
Console.WriteLine("Sum : " + sum);
}
}
Parameter arrays allows us to pass a variant number of arguments into a method. All incoming arguments will consider as a single dimensional array. Have a look on the following example.
using System;
class Mainclass
{
public static void Main()
{
Test obj = new Test();
obj.Add(10, 20);
obj.Add(10, 20, 30);
obj.Add(1, 2, 3, 4, 5, 6);
}
}
class Test
{
public void Add(params int[] ar)
{
int sum = 0;
foreach (int i in ar)
{
sum = sum + i;
}
Console.WriteLine("Sum : " + sum);
}
}
Reference Parameters in C#
Reference Parameter
These are input/output parameters; they can be used for passing value into a function as well as to get back value from a function.
using System;
class Test
{
public void Inc(ref int a) // here a is a reference parameter. It refer the same memory address of passing arguement.
{
a = a + 10;
Console.WriteLine(a);
}
}
class Mainclass
{
public static void Main()
{
int x = 10;
Test obj = new Test();
obj.Inc(ref x); // arguement x and parameter a will be using the same memory reference.
Console.WriteLine("From Main " + x);
}
}
These are input/output parameters; they can be used for passing value into a function as well as to get back value from a function.
using System;
class Test
{
public void Inc(ref int a) // here a is a reference parameter. It refer the same memory address of passing arguement.
{
a = a + 10;
Console.WriteLine(a);
}
}
class Mainclass
{
public static void Main()
{
int x = 10;
Test obj = new Test();
obj.Inc(ref x); // arguement x and parameter a will be using the same memory reference.
Console.WriteLine("From Main " + x);
}
}
Output Parameters in C#
Output Parameter
A function can return only a single value. But when we using output parameters it is possible to return multiple values.
Note: output parameters must be assigned with value before leaving method, otherwise it will results in a compilation error.
Eg
using System;
class Test
{
public void Cal(int a, int b, out int c, out int d) // a and b are input parameters. c and d are output parameters.
{
c = a + b; // set value for output parameter c.
d = a - b; // set value for output parameter d.}
}
class Mainclass
{
public static void Main()
{
int n1 = 10, n2 = 30, sum, dif;
Test obj = new Test();
obj.Cal(n1, n2, out sum, out dif);
Console.WriteLine("Sum is {0} and Difference is {1} ", sum, dif);
}
}
A function can return only a single value. But when we using output parameters it is possible to return multiple values.
Note: output parameters must be assigned with value before leaving method, otherwise it will results in a compilation error.
Eg
using System;
class Test
{
public void Cal(int a, int b, out int c, out int d) // a and b are input parameters. c and d are output parameters.
{
c = a + b; // set value for output parameter c.
d = a - b; // set value for output parameter d.}
}
class Mainclass
{
public static void Main()
{
int n1 = 10, n2 = 30, sum, dif;
Test obj = new Test();
obj.Cal(n1, n2, out sum, out dif);
Console.WriteLine("Sum is {0} and Difference is {1} ", sum, dif);
}
}
Input Parameters in C#
Input Parameter
It is used to receive values, means it just pass values.
e.g
using System;
class Test
{
public void Increment(int a) // here a is an input parameter it just takes value from the Main method.
{
a = a + 10;
Console.WriteLine("a=" + a);
}
}
class MainClass
{
public static vod Main()
{
int x = 10;
Test obj = new Test();
obj.Increment(x); // passing value into the method.
Console.WriteLine("x=" + x);
}
}
It is used to receive values, means it just pass values.
e.g
using System;
class Test
{
public void Increment(int a) // here a is an input parameter it just takes value from the Main method.
{
a = a + 10;
Console.WriteLine("a=" + a);
}
}
class MainClass
{
public static vod Main()
{
int x = 10;
Test obj = new Test();
obj.Increment(x); // passing value into the method.
Console.WriteLine("x=" + x);
}
}
Subscribe to:
Posts (Atom)