Going to C# from Java?

I’ve been learning Java for a while, and I want to get into Unity. I’ve been told that they’re quite similar, (being object-oriented) and that a lot of things are the same. However, I’m more worried about what’s different. To those who have experience in Java and C#, what should I watch out for in C# that doesn’t work the same in Java? I’ve heard that casting is different/not present in C#. Are there fundamental things, things I’m used to in Java, that work differently in C#?

Hope someone has insight on this stuff.

Java is totally different than anything used in Unity. I’m assuming you mean JavaScript which is yet another language. If that is what you mean, UnityScript uses some of the same code as JavaScript but there are enough differences to make it a pain.

I’m not sure of the differences since I skipped past the UnityScript and went straight to C#. I’m still learning, but then again I guess we all are at different levels. In my experience though (at least for me) once you build an understanding of a programming language and development concepts, applying what you know to learn other languages isn’t bad at all. I’d recommend C# over UnityScript though since it is ACTUALLY C# that you are using.

If your coming from real Java not JavaScript, it will feel very familiar to C#, syntax wise. Just jump into it with something from the Unity Learn Section and keep both the Unity Scripting Reference and the MSDN sites handy.

1 Like

Yeah, I’ve been using actual Java. I was looking more for specific Java habits that might trip me up transitioning to C#, but I guess boldly going forward is really the only way to learn. Thanks for the links though.

Yeah, actual Java is pretty similar to C#.

Here are a couple of resources that might help.

1 Like

lol, “Comparison of C Sharp and Java” I didn’t think wikipedia would have such an obvious answer…

Thanks for pointing me towards these resources.

Honestly, C# is a LOT like Java… but with extras added on.

Ignore the library aspect, they obvsly will have different frameworks of code to work with.

Syntactically though C# was developed out of Visual J++ (an old discontinued dialect of Java implemented by Microsoft). In the early years it was criticized as being a Java rip off even.

So really… so much is the same. There’s code you could directly copy paste from Java to C# and barely have to change a thing.

Like packages are namespace in C#. You don’t spell out ‘extends’ but rather just use ‘:’.

But you can’t go back the other way. C# just adds a LOT more syntax because it doesn’t strive to be pure object oriented like Java does. So delegates, anonymous functions, extension functions, events, the fact that all types (including prim types like int) are objects, and so many other things that replace certain design patterns you may be used to using in Java.

Of course you can still use those design patterns you may use in Java, can still be done in C#. Like you can create callbacks the same way… but why would you when C#'s delegates and events are so easy to use.

also notice a few defaults have changed, method default to being what would be called final in Java, and require the virtual keyword if you want to override them in a subclass. Also the default protection level is private and not protected like Java

Personally, I think of C# as a friendlier Java. I’d much rather go from Java to C# than vice versa.

One of the biggest things to remember is that, in C#, basically everything is an object. In Java, primitive types weren’t objects, while types like Strings were, but in C#, an int, a string, a bool, etc. are all objects with helper methods built in, so there’s no more encapsulating primitives in class wrappers like was necessary with Java.

C# also introduces properties, which I love and find very useful. Essentially, they encapsulate the functionality of a Java getter/setter into what appears to be a normal public member.

So while in Java you’d have something like this:

private int myInt = 0;
public int getMyInt() {
return myInt;
}
protected void setMyInt(int value) {
myInt = value;
}
public void foo() {
setMyInt(20);
int myIntValue = getMyInt();
}

… in C#, you’d do something like:

public int MyInt { get; protected set; }
public void foo() {
MyInt = 20;
int myIntValue = MyInt;
}

Much easier to use, in my opinion. :slight_smile: C# is just full of convenient little features like that.

Wow, thanks for all the extra advice people, it’s good to know that C# is pretty similar to Java. I’ve started on a tutorial, and it’s helpful just to interpret what the scripts are doing.