c# and java diffs

D

Daniel

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.addActionListener() { 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<String> listString = new ArrayList<String>();
listString.add("my string...");
listString.add("2nd string...");
// to iterate
for(String s : listString) {
System.out.println(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.
 
A

Arne Vajhøj

Daniel said:
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(string s);
public static void Permute(List<List<String>> data, Processor
p)
{
...
p(prefix + s);
...
}
public static void Print(string s)
{
Console.WriteLine(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.addActionListener() { 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?
http://msdn.microsoft.com/en-us/library/b2s063f7(VS.71).aspx

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<String> listString = new ArrayList<String>();
listString.add("my string...");
listString.add("2nd string...");
// to iterate
for(String s : listString) {
System.out.println(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
 
C

Chris Shepherd

Peter said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top