I am Alex and I work as a Java Dev for 2+ years. Nowadays, I have an opportunity to have fun with Unity and create something special for me and hope for people =)
My target now is to learn C# to be able to understand deeper and even to be able to develop plugins for Unity (I have heard it’s a good practice to optimize time to development) without the usage of bad code and bad practices.
However, I have no someone experienced enough to ask what scope of C# I need to know to freely work with Unity.
Could you please advise which chapters I must know?
Please, see chapters below from a book:
-Part I: Introducing C# and the .NET Platform 1
-Chapter 1: The Philosophy of .NET 3
-Chapter 2: Building C# Applications 35
-Part II: Core C# Programming 59
-Chapter 3: Core C# Programming Constructs, Part I 61
-Chapter 4: Core C# Programming Constructs, Part II 109
-Part III: Object-Oriented Programming with C# 149
-Chapter 5: Understanding Encapsulation 151
-Chapter 6: Understanding Inheritance and Polymorphism 205
-Chapter 7: Understanding Structured Exception Handling 247
-Chapter 8: Working with Interfaces 275
-Part IV: Advanced C# Programming 313
-Chapter 9: Collections and Generics 315
-Chapter 10: Delegates, Events, and Lambda Expressions 355
-Chapter 11: Advanced C# Language Features 399
-Chapter 12: LINQ to Objects 439
-Chapter 13: Understanding Object Lifetime 473
-Part V: Programming with .NET Assemblies 501
-Chapter 14: Building and Configuring Class Libraries 503
-Chapter 15: Type Reflection, Late Binding, and Attribute-Based Programming555
-Chapter 16: Dynamic Types and the Dynamic Language Runtime 599
-Chapter 17: Processes, AppDomains, and Object Contexts 623
-Chapter 18: Understanding CIL and the Role of Dynamic Assemblies 651
-Part VI: Introducing the .NET Base Class Libraries 693
-Chapter 19: Multithreaded, Parallel, and Async Programming 695
-Chapter 20: File I/O and Object Serialization 749
-Chapter 21: ADO.NET Part I: The Connected Layer 799
-Chapter 22: ADO.NET Part II: The Disconnected Layer 859
-Chapter 23: ADO.NET Part III: Entity Framework 929
-Chapter 24: Introducing LINQ to XML 1001
-Chapter 25: Introducing Windows Communication Foundation 1021
Well, since you pointed out to the book, you should start from Chapter 1 and practice through.
However, I suggest start with tutorials and very basics. Rest will come along the way, as your requirement and new solutions arise.
You need to learn as well Unity editor and other technical aspects. Not only language itself.
Since you know Java, learning new language will be piece of cake.
More challenging will be learning Unity APIs.
You have an option to put the book away and start coding right now. Simply pick information as you go.
Standard C# has paradigms in unity may cause performance issues. Immutability for example (as immutable objects generate garbage, and garbage may result in performance spikes due to garbage collector).
If you dont’ want to “learn as you go”, then reading through the whole book is probably a good thing. So either work without hte book, by drawin parallels with your previous language, or read the whole thing. I don’t think picking individual chapters will do you any good.
Honestly none of them. You have the knowledge of programming from Java. Java and C# are similar enough that you dot need any C# specific training.
Then Unity has it’s own design philosophy which works differently to plain .NET. So you don’t want to go to far down the rabbit hole without a unity context.
I would not claim to be an expert, however from my perspective quite a lot of “best practice in unity” stems from the real time nature of the end product, in particular avoiding generating memory allocations leading to garbage collection. Sometimes this means avoiding “cleaner” code constructions, correct me if i am wrong but i think use of foreach is a common gc creating syntax to be avoided?
Textbooks are a better source of information than tutorials. If you’re talking about language, that is, and not the engine. After the textbooks the second best source is referenes and documentation.
That’s not correct, because in this case it is tradeoff between performance and sanity coupled with maintenance cost.
Avoiding foreach and common gc creating patterns increases performance, but reduces your sanity and drives maintenance cost up, because non-gc code often is harder to read. The language wasn’t made for non-gc code, which is quite obvious, as it even uses immutable strings. You’re supposed to spanw things and “let it go” without any worry in the world. Doesn’t quite work this way in games though.
Additionally, you need to profile the code and see which parts cause problems. There was an optimization principle (forgot to whom it was attributed) where you need to consider whether a piece of program is worth optimizing by calculating how much overall performance would increase if you reduce execution time of code fragment in question to zero. So, profile and look for actual bottlenecks.
A lot of this trouble could’ve been avoided, if unity added C++ API, of course (mostly because C++ allows deterministic lifetimes). But they seem to be hell bent on not doing that.
Badly performing code that I know I’ll have to fix later also has a sanity cost. The catch is the “know” part.
It used to be, but I don’t know if it’s true any more. In any case, the idea isn’t to know a bunch of tricks like that. As @neginfinity says, get familiar with the profiler and just write code that does what it needs to do. Sometimes that will involve optimisation, other times it won’t.
Consider that as well as developer sanity, developer time is a limited resource, too. Spending your time optimising stuff that doesn’t make a difference is a waste of resources like any other. I’m not saying “avoid optimisations”, I’m saying “pick your battles”.
Also be aware of the difference between macro- and micro-optimisations. Broadly speaking:
Macro-optimisations are when you find ways to get the same result with less work, such as using a different algorithm.
Micro-optimisations are when you find ways to do the same work more efficiently, such as shaving milliseconds off iterations of a for loop.
Both of these are pretty important, but I’d encourage people to generally favor macro- over micro-optimisation where possible. Also note that micro-optimisations are typically only beneficial in code that runs a lot, for the reasons that @neginfinity pointed out. My general approach is to macro-optimise things as I’m designing my code and then micro-optimise them only where needed.
(Aside: I do avoid GC by habit these days, as I’ve been doing so since early XNA days when you absolutely had to because the Xbox 360 was easily crippled by uncontrolled allocations. In comparison to that I find the situation with Unity to be a walk in the park.)
There’s a time and place for textbooks, as a reference when you need to polish a particular thing. But the OP asks:
To me this sounds like someone trying to learn Unity. And I say, forget about C# textbooks if you are trying to learn Unity. Concentrate on making games.
The concept of reading textbook chapters with such ethereal titles as ‘The Philosophy Of .NET 3’ sounds like a good way to bore oneself to death (unless you really just have a passion for programming, in which case Unity is probably not for you).
I’m not a fan at all of rote learning, and I especially dislike the idea of learning in abstractions, so my perspective on textbooks and theoretical material is that they belong as a way to make sense of a specific problem at a specific time.
If you are doing a course where the teacher is giving you practical game development problems to solve along with the theory to explain them, so much the better, but if that’s not the case and you are learning on your own, it’s far better to consume practical experience than theoretical knowledge.
There was discussion year or two ago as I recall correctly, here on forum, regarding foreach and for loops. Apparently foreach has improved. And I think it may be indeed GC free. However, I generally try to avoid it myself, when I can.
I don’t avoid it any more, per se, but I find myself just not using it very often because I prefer the additional control you get from knowing what index you’re up to.
However, op only has 2 years of experience with java and does not list other languages.
So, a textbook definitely won’t hurt. At least skimming through it and see if something catches you eye.
The thing is I started with C++, and while I got a hang of the engine in a few months, I did hit a bunch of hidden headscratchers a year later in the language itself. Java is closer to C# than C++, though.
The important thing with a textbook, however is not to get religious with any of the concepts/techniques.
I remember an amazing blog post of someone who wrote something along the lines of “extension methods allowed me to write cleaner and more readable code, but I won’t be using them because they break OOP paradigm”. This is, in my opinion, not the way to do it.
One important thing to understand about foreach is that the garbage amount is different for each collection. Starting with Unity 5.6 arrays and List create no garbage. For Dictionary, HashSet, LinkedList, Queue, and Stack there is only garbage on the first loop with no additional garbage for additional loops.
If the OP has 2 years experience in Java I imagine they know what fundamentals of C# they should learn. Unity itself seems to be the unknown variable here, and cannot be learned without, well, doing Unity projects.
They can’t know what fundamentals of c# they need to learn after java, if they do not know c#. You acquire this knowledge after experiencing both languages.
That’s why a book won’t hurt, as long as there is no fanatism.
The way I see it, the fundamentals you need to know are sort of the same across different languages (at least for two fairly similar level languages such as Java and C#). They aren’t implemented the same, but they are the same sort of thing. Then you’ve got what you need to learn for a specific application, which should be learned through using that application.
Anyway, I’ll agree with you that the book won’t hurt, but frankly an experienced programmer wanting to get up to speed with Unity should focus on using Unity, in my opinion. The switch from Java to C#, at this point in time, is pretty much only a question of syntax. The rest can come when you need to use it.
If the OP really wants to become comprehensively qualified in using C# and Unity, then I think they should take a course.
What they need to lean is a bit above the level of those fundamentals.
Fundamentals are loops, flow control, oop, polymorphism and so on. This is not worth mentioning, as it is too basic.
The parts that needs learning are things that set the language apart from the other, and those are subtle differences that can have profound consequences.
For example (as I don’t use java), coming from C++ to C# that would be differences in abilities of generics vs templates, concept of reference vs value types, boxing, lack of references and const correctness and non-deterministic lifetimes.
This is a big deal, and none of this is basics. Coming from Java to C# there will be something else.
That level of stuff is absolutely going to surface in a well-written book. It will not show up in tutorials, and might hit programmer in the face few months later.