dotgnu-general
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[DotGNU]Microsofts plans for the future of C#


From: Adam Treat
Subject: [DotGNU]Microsofts plans for the future of C#
Date: Sat, 9 Nov 2002 12:35:00 -0500
User-agent: KMail/1.4.7

Hello All,

I just thought this should be on the radar for everyone to discuss.  Microsoft 
is planning some new features for C#.  The new features are:

* Generics
* Iterators
* Anonymous methods
* Partial types

*** Example of generics:
public class List
{
   private object[] elements;
   private int count;

   public void Add(object element) {
      if (count == elements.Length) Resize(count * 2);
      elements[count++] = element;
   }

   public object this[int index] {
      get { return elements[index]; }
      set { elements[index] = value; }
   }

   public int Count {
      get { return count; }
   }
}

List<int> intList = new List<int>();

intList.Add(1);           // No boxing
intList.Add(2);           // No boxing
intList.Add("Three");     // Compile-time error

int i = intList[0];       // No cast required

*** Example of Iterators:
public class List
{
   internal object[] elements;
   internal int count;

   public object foreach() {
      for (int i = 0; i < count; i++) yield elements[i];
   }
}

public IEnumerator<T> GetEnumerator() {
   return new __Enumerator(this);
}

private class __Enumerator: IEnumerator<T>
{
   public bool MoveNext() {
      switch (state) {
         case 0: ...;
         case 1: ...;
         case 2: ...;
         ...
      }
   }

   public T Current {...}

*** Example of Anonymous methods:
class MyForm: Form
{
   ListBox listBox;
   TextBox textBox;
   Button addButton;

   public MyForm() {
      listBox = new ListBox(...);
      textBox = new TextBox(...);
      addButton = new Button(...);
      addButton.Click += new EventHandler(sender, e) {
         listBox.Items.Add(textBox.Text);
      };
   }
}

public class Bank
{
   ArrayList accounts;

   ArrayList GetLargeAccounts(double minBalance) {
      return accounts.Select(new Filter(
         new MinBalanceSelector(minBalance).Matches));
   }

   class MinBalanceSelector
   {
      double minBalance;

      public MinBalanceSelector(double minBalance) {
         this.minBalance = minBalance;
      }

      public bool Matches(object obj) {
         return ((Account)obj).Balance >= minBalance;
      }
   }
}

*** Example of Partial Types:
public partial class Customer
{
   private int id;
   private string name;
   private string address;
   private List<Orders> orders;
}
public partial class Customer
{
   public void SubmitOrder(Order order) {
      orders.Add(order);
   }

   public bool HasOutstandingOrders() {
      return orders.Count > 0;
   }
}
public class Customer
{
   private int id;
   private string name;
   private string address;
   private List<Orders> orders;

   public void SubmitOrder(Order order) {
      orders.Add(order);
   }

   public bool HasOutstandingOrders() {
      return orders.Count > 0;
   }
}

*** Example of the using statement:
Has anyone seen this before?  I'm not sure if this is an addition to the 
language or what?

using (Resource res = new Resource()) {
   res.DoWork();
}

Resource res = new Resource(...);
try {
   res.DoWork();
}
finally {
   if (res != null) ((IDisposable)res).Dispose();
}





reply via email to

[Prev in Thread] Current Thread [Next in Thread]