Archive for the ‘C#’ Category

C# nested classes (vs Java)

Saturday, November 17th, 2007

C# does support nested classes, but have some different than Javas’ inner classes.

Java ’s nested classes have several types: static nested class, inner class, local inner class(declare an inner class within the body of a method), annonymous inner class (declare an inner class within the body of a method without naming it).

Java inner class:

http://java.sun.com/docs/books/tutorial/java/javaOO/nested.html

 http://java.sun.com/docs/books/tutorial/java/javaOO/innerclasses.html

http://java.sun.com/docs/books/tutorial/java/javaOO/summarynested.html

A class defined within another class is called a nested class. Like other members of a class, a nested class can be declared static or not. A nonstatic nested class is called an inner class. An instance of an inner class can exist only within an instance of its enclosing class and has access to its enclosing class’s members even if they are declared private.

C# nested classes are like C++ nested classes, not Java inner classes (http://blogs.msdn.com/oldnewthing/archive/2006/08/01/685248.aspx)

When you declare a class inside another class, the inner class still acts like a regular class. The nesting controls access and visibility, but not behavior. In other words, all the rules you learned about regular classes also apply to nested classes.

In C++ and C#, you will have to implement this effect manually. It’s not hard, though:

// Java
class OuterClass {
 string s;
 // ...
 class InnerClass {

  public InnerClass() { }
  public string GetOuterString() { return s; }
 }
 void SomeFunction() {
  InnerClass i = new this.InnerClass();
  i.GetOuterString();
 }
}
// C#
class OuterClass {
 string s;
 // ...
 class InnerClass {
  OuterClass o_;
  public InnerClass(OuterClass o) { o_ = o; }
  public string GetOuterString() { return o_.s; }
 }
 void SomeFunction() {
  InnerClass i = new InnerClass(this);
  i.GetOuterString();
 }
}
C# seemed don't have Java's anonymous inner class. The reason why it don't have seemed to because it doesn't need, here is from a comment: 
re: C# nested classes are like C++ nested classes, not Java inner classes

Tuesday, August 01, 2006 12:18 PM by JavaSharp

I suspect the reason this is in Java and not in C# is because C# has delegates, whereas Java must rely on inner classes (specifially, “anonymous inner classes”)

Here is a part from “C# Java differeces“:

3. Inner classes

In Java,

1. Private members of an outer class are accessible from the inner class.
2. There’s a concept of enclosing instance
3. Static inner classes were actually akin to top level classes

In C#,
1. Inner classes are called Nested Types
2. Private members of outer class are accessible from the nested class but 
the enclosing instance must be passed to the nested class.
3. In fact, C#’s Nested classes are akin to Static inner classes of Java
   (but even then you have to access members of the outer classes thru either an instance
   or class name in case of a static member)

4. Anonymous Classes

There are no anonymous classes in C#

5. Classes inside Methods

Not available in C#

C# does support anonymous delegates to archieve simpilar simplicity as Java’s anonymous class in many cases.

Codeproject: Inside C# 2.0 Anonymous Methods http://www.codeproject.com/csharp/InsideAnonymousMethods.asp

The implementation of anonymous methods in C# and its consequences (part 1)(http://blogs.msdn.com/oldnewthing/archive/2006/08/02/686456.aspx)

Visual Studio 2008 - Support for anonymous methods and lambda expressions (http://blogs.msdn.com/fxcop/archive/2007/09/21/new-for-visual-studio-2008-support-for-anonymous-methods-and-lambda-expressions.aspx)

The power of closures in C# 2.0 http://joe.truemesh.com/blog//000390.html :

Martin Fowler (obligitary Fowlbot namedrop) recently blogged about the power of closures in languages that support them. It’s worth remembering that C# 2.0 has true closure support in the form of anonymous delegates. This includes reading and modifying variables outside the context of the closure - unlike Java’s anonymous inner classes.

In this blog post he wrote some codes in Ruby and C#2.0, the C# code is simple enough however still not neat enough to beat Ruby, but I guess in C# 3.0, with the latest lambda expression, I think those C# code will be simpler than ruby.

BTW: C++ does support nested classes, and C++ classes can actually be defined inside a member function or whereever. C++ also have “anonymous class” concept, but means classes without a specific name, which is much different than what we discussed here.

Method overloading from a generic method

Tuesday, September 4th, 2007

Agata started a email discussion on "method overloading for/from a generic method, I searched and found this information:

http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=2053689&SiteID=1

I think the last 2 posts is interesting, last one should give a clear answer to why:

Generic method bodies are compiled without knowledge of the actual types to which type variables will be bound. The compiler can only use information from constraints on the type variables. The body of Func() is compiled in such a way that it works with any type arguments. That means in this case that the compiler must choose a Foo method to call that will work with any type. This is in contrast to C++ templates which are more like code generators.

ConfigurationManager does not exist?

Wednesday, June 27th, 2007

I opened up a project downloaded from codeproject, when I compile it, line like that show some waring:

System.Configuration.ConfigurationSettings.AppSettings["weburl"].ToString();

And the compiler show:

Warning 1 ’System.Configuration.ConfigurationSettings.AppSettings’ is obsolete: ‘This method is obsolete, it has been replaced by ConfigurationManager.AppSettings’

OK, I quickly change the line to:

System.Configuration.ConfigurationManager.AppSettings["weburl"].ToString();

This time, waring become error ! Compiler simply can’t find ConfigurationManager.

Fortunately, after waste no more than 30 min, I decided to search and found a post in Scott Cate’s blog. Which exactly talking about my problem, and I found a bunch of people (over 60!) left their comment to thank him saved their time. :)

The Original System.Configuration.ConfigurationSettings class is found in the System.dll assembly. There is a new assembly with all the new ConfigurationManager classes as System.Configuration.dll. Before using the ConfigurationManager class, you must first set a reference to the new System.Configuration.dll.

Well, how to do that? Following this steps:

Solution explorer -> project -> right-click on References -> add reference -> select System.Configuration in .NET tab