/*
Learning C#
by Jesse Liberty
Publisher: O'Reilly
ISBN: 0596003765
*/
using System;
public class Functions
{
static void Main()
{
Console.WriteLine("In Main! Calling SomeMethod()...");
SomeMethod();
Console.WriteLine("Back in Main().");
}
static void SomeMethod()
{
Console.WriteLine("Greetings from SomeMethod!");
}
}
ASP.NET & Csharp (C#)
ASP.NET & Csharp (C#) , Here you can find my posts about .NET c# .
Recursive function in action
using System;
public class FactorialTest
{
public static void Main( string[] args )
{
for ( long counter = 0; counter <= 10; counter++ )
Console.WriteLine( "{0}! = {1}",
counter, Factorial( counter ) );
}
public static long Factorial( long number )
{
if ( number <= 1 )
return 1;
else
return number * Factorial( number - 1 );
}
}
[/csharp]
Recursive Factorial method.
using System;
public class FactorialTest
{
public static void Main( string[] args )
{
for ( long counter = 0; counter <= 10; counter++ )
Console.WriteLine( "{0}! = {1}", counter, Factorial( counter ) );
}
public static long Factorial( long number )
{
if ( number <= 1 )
return 1;
else
return number * Factorial( number - 1 );
}
}
[/csharp]
Use a recursive method, travel, to journey from start to finish
using System;
public class Journey {
private static String indent = “”;
public static void TakeOneStep(int step) {
Console.WriteLine(“{0}Taking step {1}”, indent, step);
}
public static void Move(int start, int finish) {
string oldIndent = indent;
Console.WriteLine(“{0}Starting move from {1} to {2}”, indent, start, finish);
if (start < finish) {
TakeOneStep(start);
indent += " ";
Move(start+1, finish);
indent = oldIndent;
}
Console.WriteLine("{0}Finishing move from {1} to {2}",indent, start, finish);
}
public static void Main(String [] args) {
Move(1, 10);
}
}
[/csharp]
Recursive sum method
public class SumPrices {
public static double Sum(double[] p, int start, int end) {
if (start < end)
return p[start] + Sum(p,start+1,end);
else return 0;
}
public static void Main() {
double[] prices = {1.3, 13.68, 314.919, 82.827, 363.949};
System.Console.WriteLine("The sum is {0:C}", Sum(prices,0,prices.Length));
}
}
[/csharp]
Foreach for arraylist
/*
* C# Programmers Pocket Consultant
* Author: Gregory S. MacBeth
* Email: gmacbeth@comporium.net
* Create Date: June 27, 2003
* Last Modified Date:
*/
using System;
using System.Collections;
namespace Client.Chapter_3___Structs__Enums__Arrays_and_Classes
{
public class MyArrayList
{
static void Main(string[] args)
{
ArrayList a = new ArrayList(10);
ArrayList.Synchronized(a);
int x = 0;
a.Add(x);
a.Insert(1, ++x);
foreach (int y in a)
{
Console.WriteLine(y);
}
a.Remove(x);
a.RemoveAt(0);
a.Clear();
}
}
}
Stack push and foreach
/*
* C# Programmers Pocket Consultant
* Author: Gregory S. MacBeth
* Email: gmacbeth@comporium.net
* Create Date: June 27, 2003
* Last Modified Date:
*/
using System;
using System.Collections;
namespace Client.Chapter_3___Structs__Enums__Arrays_and_Classes
{
public class StacksChapter_3___Structs__Enums__Arrays_and_Classes
{
static void Main(string[] args)
{
Stack a = new Stack(10);
int x = 0;
a.Push(x);
x++;
a.Push(x);
foreach (int y in a)
{
Console.WriteLine(y);
}
a.Pop();
a.Clear();
}
}
}