.NET 3.5: Quick look at what is in WCF

Windows Communication Foundation (WCF) is a platform for building service-oriented applications.  The following are some of the highlights in WCF:

  • Defines the following three core contracts with its consumers when creating WCF services: Service contract, Data contract, and Message contract.
  • Use of service endpoint to expose WCF services to consumers.
  • Availability of various service hosting applications: managed application such as Console application, Windows service, or Windows Forms application, Web server using IIS or Windows Process Activation Service (WAS)WAS also supports non-HTTP protocols such as TCP, MSMQ, and named pipes.  Also available is the WCF-provided host (wcfSvcHost.exe).
Advertisements

.NET 3.5: Quick look at what is in WPF

Windows Presentation Foundation (WPF) seems to be the successor to Windows Forms for desktop development.  The following are some of the highlights in WPF:

  • Separation of logic code from the presentation code where the presentation code is defined using a declarative syntax called Extensible Application Markup Language (XAML).
  • Three kinds of application available for creation: Windows applications, Navigation applications, and XAML Browser Applications (XBAPs)Windows applications are similar to Windows Forms applications.  Navigation applications are similar to web sites in that they provide a page-based user experience but they run on the desktop computer like Windows applications.  XBAPs are similar to Navigation applications except that they run in Windows Internet Explorer.
  • Unprecedented support for displaying graphics, images, and media files.
  • Binding of a property to any object or source.  Use of data templates to configure how your data is displayed in the presentation layer.
  • Use of Styles, Animations, and Triggers to provide consistent user interface appearance, add interactivity, and change the user interface appearance in response to different conditions.
  • New document and printing model where you can work with XML Paper Standard (XPS) Documents and Flow Documents.

General: Fibonacci sequence using a loop versus a recursive function

I recently had a phone interview and one of the technical questions they asked me is how to implement a Fibonacci sequence in code.  I started doing it in a loop but it got complicated so I changed to a recursive function.  Unfortunately, I got stuck with the recursive function and never actually finished it.  So after the phone interview I fired up my Visual Studio 2005 and started coding the Fibonacci sequence using a loop and using a recursive function and see which one is better.  The code below tells me recursive function is the way to go.

static int Fibonacci_Loop(int n)
{
  if (n == 0) return 0;
  if (n == 1) return 1;

  int nMinus2 = 0;
  int nMinus1 = 1;
  int nValue = 1;
  for (int i = 2; i <= n; i++)
  {
    nValue = nMinus1 + nMinus2;
    nMinus2 = nMinus1;
    nMinus1 = nValue;
  }

  return nValue;
}

static int Fibonacci_Recursive(int n)
{
  if (n == 1) return 1;
  if (n == 0) return 0;
  return Fibonacci_Recursive(n – 1) + Fibonacci_Recursive(n – 2);
}

.NET 2.0: Notes on Foundation Part 5 (C#)

Sequential Lists

If you need a collection that stores data in a sequential basis, you can use a Queue which is a first-in, first-out (FIFO) or Stack which is a last-in, first-out (LIFO).  Queue’s most important properties and methods are: Count, Dequeue(), Enqueue(), and Peek().  Stack’s most important properties and methods are: Count, Pop(), Push(), and Peek().

 

Dictionaries

Dictionaries are another type of collections that are used to store key/value pairs.  The most basic of all dictionary classes that are available in .NET Framework is the Hashtable class.

Hashtable emailLookup = new Hashtable();

// using Add() method
// where 1st parameter is the key and 2nd parameter is the value
emailLookup.Add("jdoe@company.com", "Doe, John");

// using indexer to add key/value pair
emailLookup["jdoe@company.com"] = "Doe, John";

// using indexer to access data
Console.WriteLine(emailLookup["jdoe@company.com"]);

// iterating through the entries in the dictionary
foreach (DictionaryEntry entry in emailLookup)
{
  Console.WriteLine(entry.Value);
}

All dictionary classes support the IDictionary interface which derives from ICollection interface.  IDictionary’s most important properties and methods are: Keys, Values, Add(), Clear(), Contains(), GetEnumerator(), and Remove()Hashtable also has additional methods for testing existence of keys and values: ContainsKey() and ContainsValue().

 

Hashtable and Equality

The Hashtable uses a hash value, an integer, to aid in the storage of its keys.  It uses the GetHash() method that the Object class supports to test for equality.  The Object.GetHash() method returns a hash value that is unique to the object.  For String class, this GetHash() method is overridden so that two strings with the same text will be equal even though they are different instances. 

Care should be taken that when creating your own class that will be stored in a Hashtable that you override the GetHash() method if the test for equality is wrong.  You also might need to override the Equals() method because if the Hashtable finds that the two objects have same hash value, it will call their Equals() method to see if they are in fact equal.  The Object.Equals() method returns false if the two objects are different instances of the same class.

public class Dog
{
  string _name;

  public Dog(string name)
  {
    _name = name;
  }

  public override int GetHashCode()
  {
    return name.GetHashCode();
  }

  public override bool Equals(object obj)
  {
    Dog otherDog = obj as Dog;
    if (otherDog == null) return false;
    return otherDog.name == name;
  }
}

Another way to specify your own equality comparison is through the IEqualityComparer interface and this is useful if you do not want to change your classes to override the GetHash() and Equals() methods.

public class InsensitiveComparer : IEqualityComparer
{
  CaseInsensitiveComparer _comparer = new CaseInsensitiveComparer();

  public int GetHashCode(object obj)
  {
    return obj.ToString().ToLowerInvariant().GetHashCode();
  }

  public new bool Equals(object x, object y)
  {
    if (_comparer.Compare(x, y) == 0)
    {
      return true;
    }
    else
    {
      return false;
    }
  }
}

// to use the InsensitiveComparer class
Hashtable dehash = new Hashtable(new InsensitiveComparer());
dehash["First"] = "1st";
dehash["Second"] = "2nd";
dehash["Third"] = "3rd";
dehash["Fourth"] = "4th";
dehash["fourth"] = "4th";
Console.WriteLine(dehash.Count); // 4

.NET 3.5: WPF, WCF, WWF, ASP.NET 3.5, ADO.NET 3.5

Now that .NET 3.5 is already out and I still have to get my certification on that .NET 2.0 Framework – Application Development Foundation, I decided to start ahead on learning these new technologies and not get left behind. 

The .NET Framework foundation is still important so I will still be continuing on that one except that I will start writing on .NET 3.5 in parallel.  I will probably start out with just a quick look at WPF, WCF, WWF, ASP.NET 3.5, and ADO.NET 3.5 just to get my feet wet.