Use an iterator to return a sequence of Fibonacci numbers in C#

using System; using System;   class FibTest { static void Main () { foreach (int fib in Fibs (6)) Console.Write (fib + ""); }   static IEnumerable <int> Fibs (int fibCount) { for (int i = 0, prevFib = 1, curFib = 1; i <fibCount; i++) { yield return prevFib; int = newFib prevFib curFib […]
Continue reading…

Enjoyed this post? Share it!

 
 

Iterate through arraylist and use indexer

using System; using System.Collections;     class CollectionsDemo { public static void Main(String[] args) {     ArrayList PokerPlayers = new ArrayList(3); PokerPlayers.Add("J"); PokerPlayers.Add("M"); PokerPlayers.Add("A"); PokerPlayers.Add("J");     foreach (String Player in PokerPlayers) { Console.WriteLine(Player); }     PokerPlayers[0] = "A"; System.Console.WriteLine(PokerPlayers[0]); } }
Continue reading…

Enjoyed this post? Share it!

 
 

Example use of array.copy() method to copy part of an array

  /* C# Programming Tips & Techniques by Charles Wright, Kris Jamsa     Publisher: Osborne/McGraw-Hill (December 28, 2001) ISBN: 0072193794 */     // // CopyPart.cs — Uses the Array.Copy() method to copy part of an array // ints into a secton of an array of doubles. // // Compile this program with the […]
Continue reading…

Enjoyed this post? Share it!

 
 

Use array.copy() method to copy an array with type cast

  /* C# Programming Tips & Techniques by Charles Wright, Kris Jamsa     Publisher: Osborne/McGraw-Hill (December 28, 2001) ISBN: 0072193794 */ // // Int2Dbl.cs — Uses the Array.Copy() method to copy an array of ints // into an array of doubles. // // Compile this program with the following command line: // C:>csc Int2Dbl.cs […]
Continue reading…

Enjoyed this post? Share it!

 
 

Sorteddictionary to store instance of system.icomparable

using System; using System.Collections.ObjectModel; using System.Collections.Generic; using System.Text; using System.Runtime.Serialization;         public class Customer : System.IComparable { private int _id; private string _name; private string _rating; private static SortOrder _order;     public enum SortOrder { Ascending = 0, Descending = 1 }     public Customer(int id, string name) : this(id, […]
Continue reading…

Enjoyed this post? Share it!

 
 

Example use of a two-dimensional array

  /* C# Programming Tips & Techniques by Charles Wright, Kris Jamsa     Publisher: Osborne/McGraw-Hill (December 28, 2001) ISBN: 0072193794 */ // Grades.cs — Uses a two-dimensional array to store grades for students // // Compile this program with the following command line: // C:>csc Grades.cs namespace nsGrades { using System;   public class […]
Continue reading…

Enjoyed this post? Share it!

 
 

Recursive implementation of binary search in csharp

using System;     public class BinarySearch { public static int Search (int[] data, int key, int left, int right) { if (left <= right) { int middle = (left + right)/2; if (key == data[middle]) return middle; else if (key < data[middle]) return Search(data,key,left,middle-1); else return Search(data,key,middle+1,right); } return -1; } public static void […]
Continue reading…

Enjoyed this post? Share it!

 
 

Example use of a jagged array to store data

  /* C# Programming Tips & Techniques by Charles Wright, Kris Jamsa     Publisher: Osborne/McGraw-Hill (December 28, 2001) ISBN: 0072193794 */ // Sales.cs — Uses a jagged array to store sales figures, then writes report // for one month. Demonstrates that you do not have to worry about // looking for empty elements. // […]
Continue reading…

Enjoyed this post? Share it!

 
 

Example use of readonlycollection to wrap a collection

using System; using System.Collections.ObjectModel; using System.Collections.Generic; using System.Text; using System.Runtime.Serialization;         public class Customer : System.IComparable { private int _id; private string _name; private string _rating; private static SortOrder _order;     public enum SortOrder { Ascending = 0, Descending = 1 }     public Customer(int id, string name) : this(id, […]
Continue reading…

Enjoyed this post? Share it!

 
 

Example use of object to create a generic array

  /* C#: The Complete Reference by Herbert Schildt     Publisher: Osborne/McGraw-Hill (March 8, 2002) ISBN: 0072134852 */         // Use object to create a generic array.   using System;   public class GenericDemo { public static void Main() { object[] ga = new object[10];   // store ints for(int i=0; […]
Continue reading…

Enjoyed this post? Share it!

 
 

Example use of icomparer

  /* C#: The Complete Reference by Herbert Schildt     Publisher: Osborne/McGraw-Hill (March 8, 2002) ISBN: 0072134852 */     // Use IComparer.   using System; using System.Collections;   // Create an IComparer for Inventory objects. class CompInv : IComparer { // Implement the IComparable interface. public int Compare(object obj1, object obj2) { Inventory […]
Continue reading…

Enjoyed this post? Share it!

 
 

Example use of bitarray collection as flag

using System; using System.Collections;     public class Starter { public static void Main() { Hashtable employees = new Hashtable(); employees.Add("A100", new Employee("Ben", true, false, true)); employees.Add("V100", new Employee("Valerie", false, false, true)); Participation((Employee)employees["A100"]); Participation((Employee)employees["V100"]); }     public static void Participation(Employee person) { Console.WriteLine(person.Name + ":"); if (person.InProfitSharing) { Console.WriteLine(" Participating in Profit Sharing"); } […]
Continue reading…

Enjoyed this post? Share it!

 
 

Example of three-dimensional rectangular array

  /* Mastering Visual C# .NET by Jason Price, Mike Gunderloy     Publisher: Sybex; ISBN: 0782129110 */     /* Example10_8.cs illustrates the use of a three-dimensional rectangular array */     using System;     public class Example10_8 {     public static void Main() {     // create the galaxy array […]
Continue reading…

Enjoyed this post? Share it!