My journey started with…
When I set out to create a project, my goal was to build a game that could potentially feature 3D elements, be interactive, and particularly played on mobile platforms. I also wanted to integrate AI into the game to add depth and complexity. Naturally, this led me to explore C#—a language commonly used in the Unity game engine. While I won’t dive into the specifics of Unity itself in this post, I do want to focus on why I chose C# and how it compares to JavaScript from my perspective as someone coming from a JavaScript background.
Why C#...
The primary reason I decided to use C# is that Unity, the game engine I chose, relies heavily on C# as its main programming language. While there are other languages you could potentially use in Unity, C# is by far the most widely supported and recommended. It's a modern, object-oriented programming language developed by Microsoft, and it's commonly used in a variety of applications, including desktop software, web applications (via ASP.NET), and—of course—games using Unity.
C# is part of the larger .NET ecosystem, which offers a huge library of resources and frameworks for building everything from small utilities to large-scale enterprise applications. The language itself was designed with simplicity, power, and type safety in mind. This makes it a versatile choice for many kinds of projects. But in this post, I want to focus particularly on the aspect of type safety and why it makes C# stand out, especially compared to JavaScript.
Why C# data type…
C# is a statically typed language, meaning that variable types (like int, bool, string) are defined at compile-time. Since C# is statically typed, the compiler can perform more optimization work ahead of time. Its type safety feature, with variable types being strictly defined, allows the compiler to catch many types of errors before the code is even run and helps the code be more maintainable. JavaScript, being dynamically typed, requires more runtime checks due to a process called type coercion, which adds overhead, especially in large or complex applications. This is part of the reason C# can be faster. However, the main performance edge of C# lies in compute-intensive and multithreaded tasks (like running different parts of the game in parallel).
JS type coercion:
let num = 5; let str = "10"; console.log(num + str); // Outputs "510" (string concatenation instead of numeric addition) str = 10; // Assign a string to a variable and later assign a number to the same variable
Common C# declaration of value data type:
int x = 10; // Whole integer number double pi = 3.14159; // Decimals 64-bit floating point, end with d but not necessary float y = 10.2f; // Decimals 32-bit floating point, have to end with f decimal price = 19.99m; // 128-bit decimal, have to end with m, for precise monetary calculations or any financial stuff bool isValid = true; //Boolean true or false char grade = 'A'; //A single character, of 16-bit Unicode character string name = "John"; //special case, is actually a reference type
As we know, in JavaScript, variables are declared with var, const, and let. Also, JavaScript has no distinction between int and float; it's just number. Therefore, C# doesn't have NaN. Additionally, in JavaScript, string is a primitive type (similar to a value type in C#), but in C#, it is a reference type.
In C#, value types cannot be null by default, but you can use nullable types to allow value types to be assigned null. null is allowed only for reference types (like string, object, and custom classes). By default, reference types are initialized to null. To enable value types to be nullable, you use the ? syntax (like int?, double?, bool?).
string name = null; // This is valid. Person person = null; // This is also valid if 'Person' is a class. int? number = null; // Explicitly allows null since nullable type is enabled int number = null; // Error: Cannot assign null to a non-nullable value type
JavaScript does not have a concept of nullable types like C#; everything can technically be null or undefined.
In both languages, when dealing with reference types, any changes made to the data through one reference will affect all other references pointing to the same data in memory. However, C# is more strongly typed, and arrays are of fixed size unless you use collections like List
C# reference types: object, class, delegate, array.
JavaScript reference types: object, array, function.
C# Array (fixed size):
int[] numbers = new int[] { 1, 2, 3 }; numbers[3] = 4; // Error: Index out of bounds because the array size is fixed
JS Array (dynamic size):
let numbers = [1, 2, 3]; numbers.push(4); // Adds 4 to the end of the array, dynamically resizing it console.log(numbers); // [1, 2, 3, 4]
C# data conversion…
In C#, data conversion refers to the process of converting one data type to another, and it can be done in two main ways: implicit and explicit conversions.
- Implicit conversions are automatic when converting from smaller to larger types, or when there's no risk of data loss.
- Explicit conversions are done manually when there’s a risk of data loss (usually via casting).
C# also provides built-in Method for safe type conversions, especially when converting between types that might not automatically cast. Additionally, nullable types require extra handling to deal with null values. Proper data conversion is crucial to ensure data integrity, avoid runtime errors, and maintain performance when working with different data types in C#.
All in all, while JavaScript’s flexibility and dynamic nature make it great for quick prototyping and web development, C# has a clear performance edge when it comes to more complex or compute-intensive tasks. This is especially true in game development, where you may need to handle 3D graphics, AI, physics simulations, and other high-performance operations.
Remember to add your semicolons(;) to end every line of code in C#! I keep forgetting, but they’re strictly required in C#.
Here are a quick link to official documentation on Reserved keywords are words that the language uses, so they already have specific definitions that shouldn’t be rewritten
Up next: Guide to breaking down and understanding C# errors(coming soon…)
The above is the detailed content of First glance at C# from JS perspective. For more information, please follow other related articles on the PHP Chinese website!

Detailed explanation of JavaScript string replacement method and FAQ This article will explore two ways to replace string characters in JavaScript: internal JavaScript code and internal HTML for web pages. Replace string inside JavaScript code The most direct way is to use the replace() method: str = str.replace("find","replace"); This method replaces only the first match. To replace all matches, use a regular expression and add the global flag g: str = str.replace(/fi

So here you are, ready to learn all about this thing called AJAX. But, what exactly is it? The term AJAX refers to a loose grouping of technologies that are used to create dynamic, interactive web content. The term AJAX, originally coined by Jesse J

10 fun jQuery game plugins to make your website more attractive and enhance user stickiness! While Flash is still the best software for developing casual web games, jQuery can also create surprising effects, and while not comparable to pure action Flash games, in some cases you can also have unexpected fun in your browser. jQuery tic toe game The "Hello world" of game programming now has a jQuery version. Source code jQuery Crazy Word Composition Game This is a fill-in-the-blank game, and it can produce some weird results due to not knowing the context of the word. Source code jQuery mine sweeping game

Article discusses creating, publishing, and maintaining JavaScript libraries, focusing on planning, development, testing, documentation, and promotion strategies.

This tutorial demonstrates how to create a captivating parallax background effect using jQuery. We'll build a header banner with layered images that create a stunning visual depth. The updated plugin works with jQuery 1.6.4 and later. Download the

The article discusses strategies for optimizing JavaScript performance in browsers, focusing on reducing execution time and minimizing impact on page load speed.

This article demonstrates how to automatically refresh a div's content every 5 seconds using jQuery and AJAX. The example fetches and displays the latest blog posts from an RSS feed, along with the last refresh timestamp. A loading image is optiona

Matter.js is a 2D rigid body physics engine written in JavaScript. This library can help you easily simulate 2D physics in your browser. It provides many features, such as the ability to create rigid bodies and assign physical properties such as mass, area, or density. You can also simulate different types of collisions and forces, such as gravity friction. Matter.js supports all mainstream browsers. Additionally, it is suitable for mobile devices as it detects touches and is responsive. All of these features make it worth your time to learn how to use the engine, as this makes it easy to create a physics-based 2D game or simulation. In this tutorial, I will cover the basics of this library, including its installation and usage, and provide a


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

SublimeText3 Mac version
God-level code editing software (SublimeText3)

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),
