471,306 Members | 851 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,306 software developers and data experts.

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.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<StringlistString = 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.

Jun 27 '08 #1
5 1597
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(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/libr...f7(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<StringlistString = 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
Jun 27 '08 #2
On Wed, 14 May 2008 18:11:15 -0700, Daniel <dh*****@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:
http://msdn.microsoft.com/en-us/library/default.aspx
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:
http://msdn.microsoft.com/en-us/library/yh598w02.aspx
[...]
2) What are delegates, how are they used, and why would you use them?
Can you provide a simple example?
See:
http://msdn.microsoft.com/en-us/library/900fyy8e.aspx
http://msdn.microsoft.com/en-us/library/ms173171.aspx
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:
http://msdn.microsoft.com/en-us/library/ms173120.aspx

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:
http://msdn.microsoft.com/en-us/library/bb397696.aspx
http://msdn.microsoft.com/en-us/library/bb384061.aspx
http://msdn.microsoft.com/en-us/library/bb383973.aspx
[...]
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:
http://msdn.microsoft.com/en-us/library/b2s063f7.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?
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<StringlistString = 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#.
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<StringlistString = new List<String>();

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

foreach (String s in listString)
{
Console.WriteLine(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
Jun 27 '08 #3
On Wed, 14 May 2008 18:29:22 -0700, Arne Vajhøj <ar**@vajhoej.dkwrote:
[...]
> 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
Jun 27 '08 #4
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.
Jun 27 '08 #5
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.
Jun 27 '08 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Michael | last post: by
reply views Thread by Ravi Tallury | last post: by
1 post views Thread by ptaz | last post: by
1 post views Thread by bezeee | last post: by
reply views Thread by Markus Wollny | last post: by
oll3i
reply views Thread by oll3i | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.