Friday, November 20, 2009

C# Dynamic array

Sample code to test a dynamic array in C#
using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication4
{
class Program
{
static void Main(string[] args)
{
DynamicArray da = new DynamicArray();
for (int i = 0; i < 60; i++)
{
da.AddObject(i);
}
da.readArray();
Console.ReadLine();
}
}
class DynamicArray
{
private object [] _objArray ;
private const int ARRAY_INITIAL_SIZE = 10;
private const int NEXT_INCREMENT_SIZE = 10;
private int _currentIndex = 0;
public DynamicArray()
{
_objArray = new object[ARRAY_INITIAL_SIZE];
}
public void AddObject(object o)
{
if (_currentIndex < _objArray.Length)
{
_objArray[_currentIndex] = o;
_currentIndex++;
}
else
{
growArray();
_objArray[_currentIndex] = 0;
_currentIndex++;
}
}
void growArray()
{
object[] tempArray = new object[_objArray.Length + NEXT_INCREMENT_SIZE];
_objArray.CopyTo (tempArray ,0);
_objArray = tempArray;
}
public void readArray()
{
for (int i = 0; i < _objArray.Length ; i++)
{
Console.WriteLine(i.ToString());
}
}
}
}

Wednesday, November 18, 2009

C#. Method arguments with and wothout ref keyword

Here is a sample code that explains the difference between method arguments with and without key word ref.

Copy and past the code in visual studio and run the code to examine the difference.

using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{

Person p = new Person("Ram","Gopal", "Verma","male",new DateTime(1979,2,28));
int a = 10;
ParameterWithoutRef(a, p);
Console.WriteLine("Method call ParameterWithoutRef starts");
Console.WriteLine("FirstName" + p.FirstName ) ;
Console.WriteLine(" value of a" + a.ToString());
Console.WriteLine("Method call ParameterWithoutRef ends");
Console.WriteLine("--------------------------------------------");
Console.WriteLine("--------------------------------------------");
Console.WriteLine("Method call ParameterWithRef starts");
ParemeterWithRef(ref a, ref p);
Console.WriteLine("FirstName" + p.FirstName);
Console.WriteLine(" value of a" + a.ToString());
Console.WriteLine("Method call ParameterWithRef ends");
Console.ReadLine();
}
static void ParameterWithoutRef(int integer , Person p)
{
p.FirstName = "Scott";
integer = 20;
}
static void ParemeterWithRef(ref int integer, ref Person p)
{
p.FirstName = "Scott";
integer = 50;
}
}
class Person
{
public Person()
{
}
public Person( string firstName, string middleName, string lastName, string gender, DateTime birthDay )
{
this._firstName = firstName;
this._lastName = lastName;
this._middleName = middleName;
this._gender = gender;
this._birthday = birthDay;
}
#region PersonClassPrivateDetails
private String _firstName;
private String _middleName;
public String MiddleName
{
get { return _middleName; }
set { _middleName = value; }
}
private String _lastName;
public String LastName
{
get { return _lastName; }
set { _lastName = value; }
}
private String _gender;
public String Gender
{
get { return _gender; }
set { _gender = value; }
}
private DateTime _birthday;
public DateTime Birthday
{
get { return _birthday; }
set { _birthday = value; }
}
public string FirstName
{
set
{
_firstName = value;
}
get
{
return _firstName;
}
}
#endregion
public string getFullName ()
{
return _firstName + _lastName;
}
}
}

Friday, November 13, 2009

C# Reverse a string

If somebody ask you to write a simple method that reverse an input string, here is the code.

using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
string result = ReverseString("i am fine");
}
public static string ReverseString(string input)
{
char[] output = input.ToCharArray();
char[] result = new char [output.Length ];
int j = 0;
for (int i = output.Length - 1 ; i > 0; i--)
{
result[j] = (char)output[i];
j++;
}
return new string(result);
}
}
}
,
Another simple way

public static string Reverse( string s ){ char[] charArray = s.ToCharArray(); Array.Reverse( charArray ); return new string( charArray );}