473,766 Members | 2,060 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.

Jun 27 '08 #1
5 1744
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?
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<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
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<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
Jun 27 '08 #3
On Wed, 14 May 2008 18:29:22 -0700, Arne Vajhøj <ar**@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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
4222
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 version="1.0" encoding="UTF-8"?> <!-- edited with XMLSPY v5 rel. 3 U (http://www.xmlspy.com) by Michael Herren (private) -->
0
6814
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 the issue is. Thanks Ravi
1
6918
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
3033
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 diffs, sending/receiving, etc. One of these engines in implemented in C# and the other in Java. Now the choice comes down to which scripting language we choose (Perl, Python or Jython) to tie into one of these engines. The scripting language...
11
9269
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 C++. I find my self sometimes, trying Object app = Object(); Object *app = Object(); Object app = new Object();
0
2854
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; helles:/ # /usr/java/apache-ant-1.5.4/bin/ant -version Apache Ant version 1.5.4 compiled on August 12 2003 It complains about some unsupported class-version; does it require an
1
9651
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 and I was wondering if anyone here would be able to give me some tips for young players such as myself, for learning the language. Is this the best Newsgroup for support with JAVA?
0
3286
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
9568
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9404
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10168
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10008
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9959
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8833
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5423
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3532
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2806
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.