472,958 Members | 2,143 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,958 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 1707
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 Vajhj 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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Michael | last post by:
Hello I am trying to write a Java-Program which converts a XML-file in a HTML. It should take the Transformation-file from the XML-file itself. Below find a possible XML-file: <?xml...
0
by: Ravi Tallury | last post by:
Hi We are having issues with our application, certain portions of it stop responding while the rest of the application is fine. I am attaching the Java Core dump. If someone can let me know what...
1
by: ptaz | last post by:
Hi I'm trying to run a web page but I get the following error. Ca anyone please tell me a solution to this. Thanks Ptaz HTTP Status 500 - type Exception report
1
by: bezeee | last post by:
At my work we are in the process of building a tool to test an XML based API. Basically, XML in and XML out over http. Currently, there are two engines that do all of the schema validations, xml...
11
by: DrUg13 | last post by:
In java, this seems so easy. You need a new object Object test = new Object() gives me exactly what I want. could someone please help me understand the different ways to do the same thing in...
0
by: Markus Wollny | last post by:
Hello! When I try to run ./configure --with-java, it complains that ant doesn't work. However ant is installed, as is the latest Java SDK 1.4.2 from sun, PATH and JAVA_HOME are set correctly; ...
1
by: David Van D | last post by:
Hi there, A few weeks until I begin my journey towards a degree in Computer Science at Canterbury University in New Zealand, Anyway the course tutors are going to be teaching us JAVA wth bluej...
0
oll3i
by: oll3i | last post by:
package library.common; import java.sql.ResultSet; public interface LibraryInterface { public ResultSet getBookByAuthor(String author); public ResultSet getBookByName(String name);
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
2
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

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.