C#
vs Java
Methods
At the basic level there are no differences between
C# and Java methods; each method takes parameters,
and has a return type.
class A {
public void MethodA1() { }
public int MethodA2() { }
public void MethodA3( int i ) { }
public int MethodA4{ int i ) { }
}
This creates a class A that has one method named MethodA1
that does not take any parameters and does not return
any value. MethodA2 returns an integer, MethodA3 takes
an integer as a parameter.
However, C# can do things with methods that could
not be done before with Java.
Method signature and params
When calling a method, the Java compiler checks
that there is a method that matches its signature.
For example, in Java, if there is a method defined
as
public void methodCaller( int a, int b, int c );
then a call of that method
methodCaller( 3, 4, 5, 6 );
will generate a compiler error as the Java compiler
cannot find a method named methodCaller that takes
four ints. We cannot create a method signature that
allows us to take a variable number of ints as parameters.
Why should this even matter? If a programmer wants
to take a variable number of ints, he can define a
method signature that takes an array of integers and
then construct the array when calling the method.
In Java, this will take at least three lines of code
just to call the method the first to create the array,
one or more lines to assign values into the array,
and the third to call the method.
C# eases this issue by allowing programmers to place
a params modifer on the last array parameter of the
method, so that a variable number of parameters can
be passed into it. Using the above example, the method
signature can be defined as
public void methodCaller( params int[] a );
and the method can be called with any of
methodCaller( 1 );
methodCaller( 1, 2, 3, 4, 5 );
Inside methodCaller, the parameters can be accessed
through the "a" array defined.
Passing by reference and out
C# can also force a parameter to be passed in by reference
as opposed to being passed by value. Let's consider
a primitive type,
public void increment( int a ) {
a++;
}
int a = 3;
increment( a );
// a still == 3
will not cause the variable passed into increment
to actually be incremented because "a" is
being passed in by value; the variable a in the increment
block is in a different environment, and therefore
changes in that scope do not propagate back out.
However, changing the method to
public void increment( ref int a ) {
a++;
}
int a = 3;
increment( ref a );
// a now == 4
will cause the int "a" to be passed in by
reference (just as classes are passed in) and therefore
modifying it will cause the original value to be changed.
Using ref with a class allows a function to reassign
the class pointer inside the function and have that
change propagate outside the method.
A natural partner to ref is the out keyword. While
ref does not guarantee that any operations will actually
be performed on the variable, using out will cause
the compiler to make sure that a value has been set
to the variable. Code such as
public void DoNothing( out int a ) { }
will cause the compiler to complain as "a"
is not assigned to anything.
Properties
Properties is a C# construct that formalizes
the get/set pattern seen in many Java classes. The
Java paradigm of having a get method that takes one
parameter and a set method that returns the said parameter
is compacted. So the following Java code:
private int name;
public int getName()
{
return this.name;
}
public void setName( int name )
{
this.name = name;
}
can be written in C# as
private int property;
public int Property {
get {
return this.property;
}
set {
// value is an implicit variable generated by the
// compiler to represent the parameter
this.property = value;
}
}
Behind the scenes, C# actually compiles the property
to two methods in the .NET intermediate language framework
named get_Property and set_Property. These methods
cannot be called directly from C#, but other languages
using the MSIL should be able to access these getters/setters.
Accessibility Modifiers
Access modifiers restrict the ability for only
certain code to change a field. The ones that a Java
programmer is used to are private, protected, default,
and public.
C#, in turn, has five modifiers:
-
Public:
means the same thing that it means in Java. As
long as you can get to the enclosing object, any
thing has free access to this member.
-
Protected: yet again, the same thing as
Java. Free reign over the member from classes
that extend the containing classes.
-
Internal: this is a new one to Java programmers,
as this means that a member is accessible from
the entire assembly. All the objects you define
inside a .cs file (you can define more than one
object inside the .cs file, unlike Java, where
you can usually define only one object) have a
handle to the internal member.
-
Protected internal: think of this one as
the union of protected and internal, as the item
it is modifying is either protected or internal.
This item can be accessed from the entire assembly,
or within objects which derive from this class.
-
Private: just as in Java. Nothing except
the enclosing class may access this.
These modifiers can be applied to the same structures
and modify the accessibility to objects, methods,
and variables.
Source Files
In Java, each class needs to exist in a like-named
file (with the like-named extension .java) there are
exceptions with class visible and inner classes, as
those are defined within another class's .java file.
C# does not have this or any restriction when it comes
to defining classes, even when they are in different
namespaces any part of a C# program may exist in the
same .cs file, so as to form a self-contained unit.
File Naming Convention
C# follows mixed-caps style, except that method
names typically also capitalize their first letter.
It is also common to see that variables in the BCL
(Base Class Library) have capital letters as the first
character, although most user-defined variables are
defined with a lowercase first letter.
There is no distinction made between the naming convention
of constants and assignable variables, as both use
mixed caps.
Page
1 2