c# and java diffs

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Daniel

    c# and java diffs

    Hi all,

    I have some questions regarding C#. I come from a Java background. I am
    using .NET 1.1. Please limit your answers to v 1.1, otherwise please state
    the version to which it applies to.

    1) I have seen code like this:

    public void someMethod() {
    // code
    MyObject otherVar;
    using (SomeClass var = DifferentClass. method()) {
    otherVar = someOtherObject .method();
    }
    }

    What is happening here? What is the part that begins with "using"? And what
    is the purpose of putting that whole thing inside the "using"? Why not just:

    MyObject otherVar = someOtherObject .method();

    2) What are delegates, how are they used, and why would you use them? Can
    you provide a simple example?

    3) Are there inner classes? In Java, it's a class defined within another
    class, which may or may not be accessible outside of the class.

    For example:

    public class A {
    public static class B {
    public void methodInB() { ... }
    }
    }

    B can be instantiated by : A.B b = new A.B();

    4) Are there anonymous inner classes?

    For example:

    public class A {
    public void methodInA() {
    var.addActionLi stener() { new ActionListener( ) {
    public void actionPerformed (Object a) {
    }
    }
    }
    }

    The inner anonymous class is the new ActionListener( ).

    5) Is there a "javadoc" like thing for methods in C#?
    I see stuff like:

    /// <summary>
    /// text...
    /// </summary>
    /// <param name...

    I am guessing that is the C# javadoc equivalent. If so, how are the docs
    generated, and does it create an HTML output?

    6) VS.NET 2003 question
    Is there a way to point at a class or variable and ask the IDE all the
    places that it's being used in? For example, if I have class A, then give me
    all the places class A is being used (for example of all the class A
    instantions or all the places static methods of class A are being used).
    Same with variables, if I have an instance variable in class A, show me
    everywhere this var is being used.

    7) Does 1.1 provide generics? Even if it does not, can you please provide a
    simple example?

    The java equivalent would be:
    List<Stringlist String = new ArrayList<Strin g>();
    listString.add( "my string...");
    listString.add( "2nd string...");
    // to iterate
    for(String s : listString) {
    System.out.prin tln(s);
    }

    I'd like to see some of the generic creation syntax of C#.

    8) Is version .NET 2 backwards compatible with 1.1? Can I use VS C# Express
    2005 on a VS.NET 2003 project? Can I specify in 2005 Express that I want to
    compile for .NET 1.1?


    Thanks in advance.

  • =?ISO-8859-1?Q?Arne_Vajh=F8j?=

    #2
    Re: c# and java diffs

    Daniel wrote:
    Hi all,
    >
    I have some questions regarding C#. I come from a Java background. I am
    using .NET 1.1. Please limit your answers to v 1.1, otherwise please
    state the version to which it applies to.
    >
    1) I have seen code like this:
    >
    public void someMethod() {
    // code
    MyObject otherVar;
    using (SomeClass var = DifferentClass. method()) {
    otherVar = someOtherObject .method();
    }
    }
    >
    What is happening here? What is the part that begins with "using"? And
    what is the purpose of putting that whole thing inside the "using"? Why
    not just:
    >
    MyObject otherVar = someOtherObject .method();
    It means:

    public void someMethod() {
    // code
    MyObject otherVar;
    SomeClass var;
    try
    {
    var = DifferentClass. method())
    otherVar = someOtherObject .method();
    }
    finally
    {
    if(var != null)
    {
    var.Dispose(); // usually equivalent to var.Close()
    }
    }
    }
    2) What are delegates, how are they used, and why would you use them?
    Can you provide a simple example?
    Delegate are method pointers.

    Java often uses anonymous classes to achieve the same.

    Example snippet (you should be able to figure it out):

    public delegate void Processor(strin g s);
    public static void Permute(List<Li st<String>data, Processor
    p)
    {
    ...
    p(prefix + s);
    ...
    }
    public static void Print(string s)
    {
    Console.WriteLi ne(s);
    }
    public static void Main(string[] args)
    {
    ...
    Permute(data, Print);
    }
    3) Are there inner classes? In Java, it's a class defined within another
    class, which may or may not be accessible outside of the class.
    >
    For example:
    >
    public class A {
    public static class B {
    public void methodInB() { ... }
    }
    }
    >
    B can be instantiated by : A.B b = new A.B();
    Yes.
    4) Are there anonymous inner classes?
    >
    For example:
    >
    public class A {
    public void methodInA() {
    var.addActionLi stener() { new ActionListener( ) {
    public void actionPerformed (Object a) {
    }
    }
    }
    }
    >
    The inner anonymous class is the new ActionListener( ).
    No. You would use delegates and in higher versions anonymous
    methods or lambda expressions.
    5) Is there a "javadoc" like thing for methods in C#?
    I see stuff like:
    >
    /// <summary>
    /// text...
    /// </summary>
    /// <param name...
    >
    I am guessing that is the C# javadoc equivalent. If so, how are the docs
    generated, and does it create an HTML output?

    6) VS.NET 2003 question
    Is there a way to point at a class or variable and ask the IDE all the
    places that it's being used in? For example, if I have class A, then
    give me all the places class A is being used (for example of all the
    class A instantions or all the places static methods of class A are
    being used). Same with variables, if I have an instance variable in
    class A, show me everywhere this var is being used.
    No idea.
    7) Does 1.1 provide generics? Even if it does not, can you please
    provide a simple example?
    >
    The java equivalent would be:
    List<Stringlist String = new ArrayList<Strin g>();
    listString.add( "my string...");
    listString.add( "2nd string...");
    // to iterate
    for(String s : listString) {
    System.out.prin tln(s);
    }
    >
    I'd like to see some of the generic creation syntax of C#.
    No. That requires .NET 2.0+ and VS 2005+.
    8) Is version .NET 2 backwards compatible with 1.1? Can I use VS C#
    Express 2005 on a VS.NET 2003 project? Can I specify in 2005 Express
    that I want to compile for .NET 1.1?
    No.

    But in 2008 you can choose between 3.5 and 2.0.

    Arne

    Comment

    • Peter Duniho

      #3
      Re: c# and java diffs

      On Wed, 14 May 2008 18:11:15 -0700, Daniel <dhw4095@gmail. com.nospam
      wrote:
      Hi all,
      >
      I have some questions regarding C#. I come from a Java background. I am
      using .NET 1.1. Please limit your answers to v 1.1, otherwise please
      state the version to which it applies to.
      Here is a web site you may find useful:

      1) I have seen code like this:
      >
      public void someMethod() {
      // code
      MyObject otherVar;
      using (SomeClass var = DifferentClass. method()) {
      otherVar = someOtherObject .method();
      }
      }
      >
      What is happening here? What is the part that begins with "using"?
      See:

      [...]
      2) What are delegates, how are they used, and why would you use them?
      Can you provide a simple example?
      See:
      Learn about reference types that have C# keywords you can use to declare them.

      Explore delegate types in C#. A delegate is a date type that refers to a method with a defined parameter list and return type. You use delegates to pass methods as arguments to other methods.

      3) Are there inner classes? In Java, it's a class defined within another
      class, which may or may not be accessible outside of the class.
      See:
      A type defined within a class, struct, or interface is called a nested type in C#.


      The short answer is "no". C# has nested classes (equivalent to Java
      "static nested classes"), but not inner classes. Of course, you can
      always pass a reference to the outer class when constructing the nested
      class to accomplish the same thing explicitly. As in Java, C# nested
      classes have access to private members of the outer class.
      [...]
      4) Are there anonymous inner classes?
      Sort of.

      C# 3.0 introduced anonymous classes, if I understand the "var" type
      correctly. However, as far as I know, this doesn't provide the full
      degree of functionality that Java's anonymous classes would. In
      particular, an anonymous type can contain only properties, and not
      methods, events, etc.

      However, note that in Java one of the most common reasons for using an
      anonymous class is addressed in .NET/C# using delegates and events instead.

      See:
      Learn about C# anonymous types, which provide a way to create objects with read-only properties without defining a named type first.

      The var keyword in C# instructs the compiler to infer the type of the variable from the expression on the right side of the initialization statement.

      Declaration statements introduce a new local variable, local constant, or local reference variable (ref local). Local variables can be explicitly or implicitly typed. A declaration statement can also include initialization of a variable's value.

      [...]
      5) Is there a "javadoc" like thing for methods in C#?
      I see stuff like:
      >
      /// <summary>
      /// text...
      /// </summary>
      /// <param name...
      >
      I am guessing that is the C# javadoc equivalent. If so, how are the docs
      generated, and does it create an HTML output?
      Those are XML comments, and the compiler uses them to generate a separate
      XML document that goes with the output. The IDE then can use that for
      context-sensitive help. I don't know if it can be used to generate HTML
      output.

      See:

      6) VS.NET 2003 question
      Is there a way to point at a class or variable and ask the IDE all the
      places that it's being used in?
      I'm not sure. For sure, 2005 and 2008 versions support this.

      You should really consider upgrading to 2008. Even if you can only use
      the free Express version, that's likely to be a significant improvement
      over being stuck with 2003 and especially .NET 1.1
      [...]
      7) Does 1.1 provide generics? Even if it does not, can you please
      provide a simple example?
      No. Generics were introduced in C# 2.0. I assume by "simple example" you
      mean a C# 2.0 example, not a 1.0 (.NET 1.1) example.
      The java equivalent would be:
      List<Stringlist String = new ArrayList<Strin g>();
      listString.add( "my string...");
      listString.add( "2nd string...");
      // to iterate
      for(String s : listString) {
      System.out.prin tln(s);
      }
      >
      I'd like to see some of the generic creation syntax of C#.
      Well, your example is using generics, not defining one. And I don't think
      it's correct, as the type is ArrayList, not List as you've declared the
      variable.

      Anyway, the equivalent is practically the same:

      List<Stringlist String = new List<String>();

      listString.Add( "my string...");
      listString.Add( "2nd string...");

      foreach (String s in listString)
      {
      Console.WriteLi ne(s);
      }
      8) Is version .NET 2 backwards compatible with 1.1?
      Mostly, yes. You may want to read MSDN's documentation on specific
      changes. In a very few places, new versions of .NET significantly change
      behavior. This happened much more with the 1.1 to 2.0 transition than
      with later ones. Things like how unhandled exceptions are dealt with,
      thread pool size, and some other were changed.
      Can I use VS C# Express 2005 on a VS.NET 2003 project?
      You can import it, yes. But it won't be a 2003 project any longer.
      Can I specify in 2005 Express that I want to compile for .NET 1.1?
      No. 2008 provides for specifying a specific target version of .NET, but
      only down to 2.0.

      Pete

      Comment

      • Peter Duniho

        #4
        Re: c# and java diffs

        On Wed, 14 May 2008 18:29:22 -0700, Arne Vajhøj <arne@vajhoej.d kwrote:
        [...]
        > What is happening here? What is the part that begins with "using"? And
        >what is the purpose of putting that whole thing inside the "using"? Why
        >not just:
        > MyObject otherVar = someOtherObject .method();
        >
        It means:
        >
        public void someMethod() {
        // code
        MyObject otherVar;
        SomeClass var;
        try
        {
        var = DifferentClass. method())
        otherVar = someOtherObject .method();
        }
        finally
        {
        if(var != null)
        {
        var.Dispose(); // usually equivalent to var.Close()
        }
        }
        }
        Almost. The variable used in the "finally" clause is actually a hidden,
        private variable known only to the compiler. Changes to the variable
        "var" will not affect the disposal when using a "using" statement, while
        it would in the above code. Also, the initialization of the "var"
        variable is outside the try/finally block, not inside.

        It also implies a cast to IDisposable before calling Dispose() (since not
        all classes implement IDisposable implicitly).

        And one final note: it's true that many classes that define both Close()
        and Dispose() will make both functionally equivalent to each other.
        However, this is far from guaranteed, and many classes implementing
        IDisposable don't have a Close() method at all.
        [...]
        But in 2008 you can choose between 3.5 and 2.0.
        And .NET 3.0 as well, in case that wasn't clear.

        Pete

        Comment

        • Tim Jarvis

          #5
          Re: c# and java diffs

          Arne Vajhøj wrote:
          6) VS.NET 2003 question
          Is there a way to point at a class or variable and ask the IDE all
          the places that it's being used in? For example, if I have class
          A, then give me all the places class A is being used (for example
          of all the class A instantions or all the places static methods of
          class A are being used). Same with variables, if I have an
          instance variable in class A, show me everywhere this var is being
          used.
          >
          No idea.
          I don't think so in VS2003 (I'm using 2008 so can't check), however you
          can get this functionality from Lutz Roeders excellent tool "Reflector"
          using the Analyze function if you don't already have reflector, you
          really should go get it (http://www.aisto.com/roeder/dotnet/), it's a
          must have tool for any .NET developer.

          Regards Tim.

          Comment

          • Chris Shepherd

            #6
            Re: c# and java diffs

            Peter Duniho wrote:
            >I am guessing that is the C# javadoc equivalent. If so, how are the
            >docs generated, and does it create an HTML output?
            >
            Those are XML comments, and the compiler uses them to generate a
            separate XML document that goes with the output. The IDE then can use
            that for context-sensitive help. I don't know if it can be used to
            generate HTML output.
            Sandcastle (http://www.codeplex.com/Sandcastle) has a "website" build mode which
            generates HTML.

            Chris.

            Comment

            Working...