Start C# here

Study C#.. Go ahead ! !

Monday, June 29, 2009

C# string handling - Part 1

We have lots of string handling functions in C#.net. We are just going to see some basic

functions.

C# string Split() - Splitting a string to an Array

The split() can be used to Split an entire string in to an string array. This will be

done using this split method. This can be used when you need long strings to be splitted

to seperate string objects.

---------------------------------------------

string mString = "This is to show Splitting in strings";
string[] mArr = mString.Split(' ');

---------------------------------------------

Here in the above code we have a string named mString and we have assigned a string to

it. Now check the second line of it. We have created an String Array to store the values

that gets Splitted.
mString.Split(' ') method splits the Entire string into an array. We have given a white

space as the argument for the Split method for the string. This will allow it to split

the string whenever it sees a white space inside the string.

Now, if you need to get the Splitted values of the string you must have to fetch the

entire items of the Array mArr using some loops.


C# string.Join() - Joining an Array as a String

The Join() method can be used to join an Array to a single String. If we have an array

to be used as a single string you can use this string.Join() method.
Let's see an example for it

---------------------------------------------

string joinedString = string.Join("-", mArr);

//Here the JoinedString variables value will be
// "This-is-to-show-Splitting-in-strings"

---------------------------------------------

In the above code in the Join function we have two overloads mainly
string.Join("String Separator","The array to Join");
Here in the above we have set the Separator as "-"(hyphen). So it will attach each Array

element with this "-". Like wise we can join the Strings in any way we like.

Wednesday, June 17, 2009

Write a Program to show numbers in Fibonacci Series in C#??

Fibonacci series is a pattern in which digits are shown in a manner that it adds the last two digit from the series ie..
0 1 1 2 3 5 8......... n..

Here, the first no is 0 and the second no is 1 so the third number must be 0+1, and the fourth number must be Sum of Second and Third.. ie 1+1, like wise we must create it for the Entire Series....

Here is the Program to show the first ten digits in Fibonacci Series in C#...
----------------------------------------------
using System;

namespace MyFibonacciApp
{
class _FibonacciSeries
{
static void Main(string[] args)
{
int j = 0;
int k = 0;
int l = 1;
for(int i = 0; i <10;i++)
{
Console.Write("{0} ", k);
j = l;
l = k;
k = l + j;
}
}
}
}
----------------------------------


Saturday, June 13, 2009

Destructor in C#

C# Destructors are used to destroy an object of a Class. Destructors are automatically invoked when the Scope of the Object goes out. We represent a Destructor with a tilde(~) symbol. Destructor will also be having the Same name of the class.
If there is a class with the name "Hello" we can represent its destructor as
~Hello()

Destructors cannot be overloaded, Inhertied or have Modifiers defined to it.
we can see a Simple program..

-------------------------------------------------
using System;

namespace Destructorss
{
class One
{
//Constructor
public One()
{
Console.WriteLine("First Constructor Called");
}

//Destructor
~One()
{
Console.WriteLine("First destructor is called.");
}
}

class Two : One
{
//Constructor
public Two()
{
Console.WriteLine("Second Constructor Called");
}
//Destructor
~Two()
{
Console.WriteLine("Second destructor is called.");
}
}

class Three : Two
{
//Constructor
public Three()
{
Console.WriteLine("Third Constructor Called");
}
//Destructor
~Three()
{
Console.WriteLine("Third destructor is called.");
}
}

class MyDestructor
{
static void Main()
{
Three t = new Three();
}

}
}

/*The result of this Program will be:-
First Constructor Called
Second Constructor Called
Third Constructor Called

Third destructor is called.
Second destructor is called.
First destructor is called.
*/

-------------------------------------------------

In the above program we have constructors and Destructors. Constuctors invoke from the First to the Third while Destructors go just in the Opposite way ie it starts from Third destrucor and goes to First....

Monday, November 24, 2008

C# Constructor(s)

what are Constructors?

Constructors are methods that gets automatically invoked when an object of a class is created. Constructors are used to Initialize values to variables inside the class automatically at the time of object creation.
Constructors will be having the same name of the class itself. Constructor doesn't return any value.

Let's see an example for Constructor..

-------------------------------------
using System;

class HelloWorld
{
int iNum1,iNum2;
string sName,sAddress;

//Creating a Constructor
public HeloWorld() //Line1
{
iNum1=iNum2=0;
sName="";
sAddress="";
}
}
-------------------------------------

Here, in the above code we have created a default constructor with the name of the class. Constructors will not be having a return type.

The Line1 represents the constructor for the class HelloWorld. The same name of the class itself will be given to constructor also.

We can have multiple constructors inside a class but should be overloaded. We use overloaded constructors for initializing values by passing it to the object of the class.

Eg
-------------------------------------
using System;

class HelloWorld
{
int iNum1,iNum2;
string sName,sAddress;

//Creating a Constructor
public HeloWorld() //Line1
{
iNum1=iNum2=0;
sName="";
sAddress="";
}
//Overloaded constructor
public HelloWorld(int _iNum,string _sName,string _sAddress)
{
iNum1=_iNum; //Line1
sName=_sName;
sAddress=_sAddress;
iNum2=0;
}

}
-------------------------------------

Here, in the above code we have two constructors but our function signatures are different in both Constructors. In the first constructor the Function signature or parameter is void and in the second one we have three parameters one Integer and two strings. So for invoking the second constructor we must have to pass values to the object.

Eg
--------------------------
//Invokes the Default Constructor
HelloWorld hwObj=new HelloWorld();

//This invokes the Second constructor
HelloWorld hwObj1=new HelloWorld(20,"SMILU","TPRA");

--------------------------

Like this we can have many constructors inside a Class.

Now, if in the case we have Static class and variables and have to initialize those variables we need another type of constructors called Static constructors.
Static constructors are used for initializing values inside Static classes. We cannot have instance constructors inside the static classes.

For Creating a static constructor we can use the static keyword.
-----------------------------------------
static class HelloWorld
{
static int i;
static HelloWorld()
{
i=10;
}
}
------------------------------------------

These are the types of constructors in C#.

Thursday, November 6, 2008

What is an Object?

What is an object?
In simple words we can say an Object as an "Instance of a Class." Without Objects we cannot say a Class is Existing.
An object is a combination of messages and data. Objects can receive and send messages and use messages to interact with each other. The messages contain information that is to be passed to the recipient object.
Look at the program given below:
using System;

---------------------------------------
namespace prime_no
{
class prime
{
int num;
public void acceptNo()
{
Console.WriteLine("Enter a number:");
num = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("\n");
}
public void primeNosTill()
{
int i, j;
bool answer = false;
Console.WriteLine("The prime numbers till entered number are: ");
for (i = 2; i <= num; i++)
{
for (j = 2; j <>
{
if (i % j == 0)
{
answer = true;
break;
}
}
if (answer == false)
Console.WriteLine(i);

else
answer = false;
}
}
class execute
{
public static void Main(string[] a)
{
prime primeNo = new prime();
primeNo.acceptNo();
primeNo. primeNosTill();
Console.Read();
}
}
}
}
---------------------------------------

The prerequisite of object creation is a class i.e. you can only create an object of a class. For the declaration of a class, the class keyword is used. In the above code, class keyword defines the class prime. The braces know as delimiters, are used to indicate the start and end of a class.
The functions of a class are called member functions. A function is a set of statements that performs a specific task in response to a message. Member functions are declared inside the class. The function declaration introduces the function in the class and function definition contains the function code. The two member functions in the above code are acceptNo and primeNosTill.
The execute class is declared with the Main() method. The execute is used as a class from where the prime class can be instantiated. To use the members of the class, the object of that must be created.
The above code contain class prime and the object has been named primeno.
prime primeno = new prime();
The member functions (acceptNo and primeNosTill), can be accessed through the object (primeno) of the class prime using “.” operator.
primeno.acceptNo();
primeno. primeNosTill();

The acceptNo function accepts the number from the user and primeNosTill function displays all the prime numbers till the number entered by the user.