<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Zhenni Wu</title>
    <description>The latest articles on Forem by Zhenni Wu (@zhenni_wu_28d825b0254dcde).</description>
    <link>https://forem.com/zhenni_wu_28d825b0254dcde</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F2518777%2F41e86e4d-050e-4e07-9c45-7e733a01d71e.png</url>
      <title>Forem: Zhenni Wu</title>
      <link>https://forem.com/zhenni_wu_28d825b0254dcde</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/zhenni_wu_28d825b0254dcde"/>
    <language>en</language>
    <item>
      <title>Guide to breaking down and understanding C# errors</title>
      <dc:creator>Zhenni Wu</dc:creator>
      <pubDate>Mon, 16 Dec 2024 19:45:58 +0000</pubDate>
      <link>https://forem.com/zhenni_wu_28d825b0254dcde/guide-to-breaking-down-and-understanding-c-errors-2472</link>
      <guid>https://forem.com/zhenni_wu_28d825b0254dcde/guide-to-breaking-down-and-understanding-c-errors-2472</guid>
      <description>&lt;h4&gt;
  
  
  Intro
&lt;/h4&gt;

&lt;p&gt;In this blog, I’m going to mainly focus on deciphering C# errors and potential steps to solve them. As I mentioned in my last blog, C# is statically typed, which allows the compiler to perform more optimization work ahead of time. One of the important features of C# is how it’s compiled.&lt;/p&gt;

&lt;h4&gt;
  
  
  Compile Time is…
&lt;/h4&gt;

&lt;p&gt;Compile time refers to the period when the source code is being translated into machine code or an intermediate language by a compiler before the program actually runs (or even gets executed). In C#, this process happens when you build (or compile) your application (like all the code). The compiler checks your code for syntax errors, type mismatches, and other issues that would prevent the program from running correctly.&lt;/p&gt;

&lt;h4&gt;
  
  
  Compile time versus runtime…
&lt;/h4&gt;

&lt;p&gt;I also mentioned in my last blog that JavaScript does runtime checks. I want to highlight the biggest difference between these two: C# detects errors at compile time, before the program runs, while JavaScript detects errors at runtime, when the program is already running. In JavaScript, the code continues to execute until it reaches an error.&lt;/p&gt;

&lt;h5&gt;
  
  
  Example compile-time errors:
&lt;/h5&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;randomData = "abc abc";
Console.WriteLine(randomData); 
// Error: all variables must be declared before they are assigned a value. 

int a = 10; 
string b = "Hello"; 

a = b;
 // Error: types are strictly enforced, meaning you cannot assign a string directly to an int without explicitly converting it.

string result = a + b;  
// Concatenation of an integer with a string requires explicit conversion before the concatenation can happen.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The program won't compile due to the errors. The compiler will produce error messages indicating the type mismatch, likely along the lines of:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;/home/ccuser/workspace/csharp-data-types-variables-handling-errors-csharp_v2/Program.cs(9,7): error CS0103: The name 'randomData' does not exist in the current context [/home/ccuser/workspace/csharp-data-types-variables-handling-errors-csharp_v2/e7-workspace.csproj] 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We can break it down into 3 parts to read.&lt;br&gt;
First, we have the full local path of the file where the error occurred, Program.cs. The numbers that follow in parentheses denote the line and character of the file responsible for the error, respectively. So, (9,7) indicates that the error is on line 9, starting at the 7th character (including spaces).&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;/home/ccuser/workspace/csharp-data-types-variables-handling-errors-csharp_v2/Program.cs(9,7): 
// Path to exact location separated from the next part by :
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Next is the actual error, which will inform us what in our code is incorrect. Every kind of error in C# has a unique identifier, like “CS0103”, so that they can easily be found within the &lt;a href="https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/compiler-messages/cs0103" rel="noopener noreferrer"&gt;Microsoft documentation&lt;/a&gt;. This error indicates that a variable name was used without ever being defined:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;error CS0103: The name 'randomData' does not exist in the current context 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Finally, a C# program requires a special type of file using the &lt;code&gt;.csproj&lt;/code&gt; extension. The &lt;code&gt;.csproj&lt;/code&gt; file itself does not generate error messages directly, but it dictates the build and deployment of metadata for the project. Checking the &lt;code&gt;.csproj&lt;/code&gt; file is a good step to diagnose and resolve any issues related to missing dependencies, incorrect settings, or build misconfiguration.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[/home/ccuser/workspace/csharp-data-types-variables-handling-errors-csharp_v2/e7-workspace.csproj] 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Based on that, you can infer what the other two error messages look like:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Error CS0103: The randomData variable is not declared before use.&lt;/li&gt;
&lt;li&gt;Error CS0029: You cannot assign a string (b) to an int (a) without explicit conversion.&lt;/li&gt;
&lt;li&gt;Error CS0019: You cannot use the + operator to concatenate an int with a string without converting the int to a string first.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;These messages will explain why the code can't compile and point to the specific lines where the errors occur. You will need to fix these issues to make the code work, but now you know what issues your code is facing.&lt;/p&gt;

&lt;p&gt;Sometimes, we don’t even need to find a solution—the error message will tell us exactly what the problem is.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;int score = 45 // error CS1002: ; expected 

/home/ccuser/workspace/csharp-data-types-variables-handling-errors-csharp_v2/Program.cs(9,21): error CS1002: ; expected [/home/ccuser/workspace/csharp-data-types-variables-handling-errors-csharp_v2/e7-workspace.csproj]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The procedure to resolve a C# error...&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Receive the error and break it down

&lt;ul&gt;
&lt;li&gt;First, identify the location of the error. Look for the file name, line number, and character position in the error message.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Read through the error message carefully

&lt;ul&gt;
&lt;li&gt;Understand what the error is telling you. This will often include information about what’s wrong (e.g., undeclared variables, type mismatches, etc.).&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Search for the error code in &lt;a href="https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/compiler-messages/cs0001" rel="noopener noreferrer"&gt;Microsoft documentation&lt;/a&gt;

&lt;ul&gt;
&lt;li&gt;Go to the Microsoft documentation website, and use the error code (e.g., "CS0103") in the filter by title search bar to find the related documentation for that specific error.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Review the documentation and examples

&lt;ul&gt;
&lt;li&gt;Carefully read the explanation and solution examples provided in the documentation to understand how to resolve the issue.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Apply the solution in your code

&lt;ul&gt;
&lt;li&gt;Try the suggested solution or fix it in your own code. Make sure to test it and check if the error is resolved.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;I hope this was helpful! Here’s a quick tip: Like JavaScript, C# variable names can contain letters, digits, and underscores, but not the dollar sign (&lt;code&gt;$&lt;/code&gt;). While the compiler doesn’t enforce it, it’s also good practice to use camelCase, which is the standard convention in JavaScript.&lt;/p&gt;

&lt;p&gt;Previous: &lt;a href="https://dev.to/zhenni_wu_28d825b0254dcde/first-glance-at-c-from-js-perspective-516o"&gt;First glance at C# from JS perspective&lt;/a&gt;&lt;br&gt;
Up next: Coming soon…&lt;/p&gt;

</description>
      <category>beginners</category>
      <category>csharp</category>
    </item>
    <item>
      <title>First glance at C# from JS perspective</title>
      <dc:creator>Zhenni Wu</dc:creator>
      <pubDate>Tue, 03 Dec 2024 16:30:36 +0000</pubDate>
      <link>https://forem.com/zhenni_wu_28d825b0254dcde/first-glance-at-c-from-js-perspective-516o</link>
      <guid>https://forem.com/zhenni_wu_28d825b0254dcde/first-glance-at-c-from-js-perspective-516o</guid>
      <description>&lt;h4&gt;
  
  
  My journey started with…
&lt;/h4&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;h4&gt;
  
  
  Why C#...
&lt;/h4&gt;

&lt;p&gt;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, &lt;strong&gt;object-oriented programming&lt;/strong&gt; language developed by &lt;strong&gt;Microsoft&lt;/strong&gt;, and it's commonly used in a variety of applications, including desktop software, web applications (via ASP.NET), and—of course—games using Unity.&lt;br&gt;
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 &lt;strong&gt;type safety&lt;/strong&gt; 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 &lt;strong&gt;type safety&lt;/strong&gt; and why it makes C# stand out, especially compared to JavaScript.&lt;/p&gt;
&lt;h4&gt;
  
  
  Why C# data type…
&lt;/h4&gt;

&lt;p&gt;C# is a &lt;strong&gt;statically typed&lt;/strong&gt; language, meaning that variable types (like &lt;code&gt;int&lt;/code&gt;, &lt;code&gt;bool&lt;/code&gt;, &lt;code&gt;string&lt;/code&gt;) are defined at compile-time. Since C# is statically typed, the compiler can perform more optimization work ahead of time. Its &lt;strong&gt;type safety&lt;/strong&gt; 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 &lt;strong&gt;dynamically typed&lt;/strong&gt;, requires more runtime checks due to a process called &lt;strong&gt;type coercion&lt;/strong&gt;, 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 &lt;strong&gt;compute-intensive&lt;/strong&gt; and &lt;strong&gt;multithreaded&lt;/strong&gt; tasks (like running different parts of the game in parallel).&lt;/p&gt;
&lt;h5&gt;
  
  
  JS type coercion:
&lt;/h5&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;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
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;h5&gt;
  
  
  Common C# declaration of value data type:
&lt;/h5&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;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
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;As we know, in JavaScript, variables are declared with &lt;code&gt;var&lt;/code&gt;, &lt;code&gt;const&lt;/code&gt;, and &lt;code&gt;let&lt;/code&gt;. Also, JavaScript has no distinction between &lt;code&gt;int&lt;/code&gt; and &lt;code&gt;float&lt;/code&gt;; it's just &lt;code&gt;number&lt;/code&gt;. Therefore, C# doesn't have &lt;code&gt;NaN&lt;/code&gt;. Additionally, in JavaScript, &lt;code&gt;string&lt;/code&gt; is a primitive type (similar to a value type in C#), but in C#, it is a reference type.&lt;/p&gt;

&lt;p&gt;In C#, value types cannot be &lt;code&gt;null&lt;/code&gt; by default, but you can use nullable types to allow value types to be assigned &lt;code&gt;null&lt;/code&gt;. &lt;code&gt;null&lt;/code&gt; is allowed only for reference types (like string, object, and custom classes). By default, reference types are initialized to &lt;code&gt;null&lt;/code&gt;. To enable value types to be nullable, you use the &lt;code&gt;?&lt;/code&gt; syntax (like &lt;code&gt;int?&lt;/code&gt;, &lt;code&gt;double?&lt;/code&gt;, &lt;code&gt;bool?&lt;/code&gt;).&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;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
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;JavaScript does not have a concept of nullable types like C#; everything can technically be &lt;code&gt;null&lt;/code&gt; or &lt;code&gt;undefined&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;In both languages, when dealing with &lt;strong&gt;reference types&lt;/strong&gt;, 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 &lt;strong&gt;strongly typed&lt;/strong&gt;, and arrays are of fixed size unless you use collections like &lt;code&gt;List&amp;lt;T&amp;gt;&lt;/code&gt;, while JavaScript arrays are more flexible and dynamic. &lt;br&gt;
C# reference types: object, class, delegate, array.&lt;br&gt;
JavaScript reference types: object, array, function.&lt;/p&gt;

&lt;h5&gt;
  
  
  C# Array (fixed size):
&lt;/h5&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;int[] numbers = new int[] { 1, 2, 3 };
numbers[3] = 4;  // Error: Index out of bounds because the array size is fixed
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h5&gt;
  
  
  JS Array (dynamic size):
&lt;/h5&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;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]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  C# data conversion…
&lt;/h4&gt;

&lt;p&gt;In C#, &lt;strong&gt;data conversion&lt;/strong&gt; refers to the process of converting one data type to another, and it can be done in two main ways: &lt;strong&gt;implicit&lt;/strong&gt; and &lt;strong&gt;explicit&lt;/strong&gt; conversions.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Implicit conversions&lt;/strong&gt; are automatic when converting from smaller to larger types, or when there's no risk of data loss.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Explicit conversions&lt;/strong&gt; are done manually when there’s a risk of data loss (usually via casting).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;C# also provides built-in &lt;a href="https://learn.microsoft.com/en-us/dotnet/csharp/fundamentals/types/" rel="noopener noreferrer"&gt;Method&lt;/a&gt; for safe type conversions, especially when converting between types that might not automatically cast. Additionally, nullable types require extra handling to deal with &lt;code&gt;null&lt;/code&gt; values. Proper data conversion is crucial to ensure data integrity, avoid runtime errors, and maintain performance when working with different data types in C#.&lt;/p&gt;

&lt;p&gt;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.&lt;br&gt;
Remember to add your semicolons(&lt;code&gt;;&lt;/code&gt;) to end every line of code in C#! I keep forgetting, but they’re strictly required in C#.&lt;br&gt;
Here are a quick link to official documentation on &lt;a href="https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/" rel="noopener noreferrer"&gt;Reserved&lt;/a&gt; keywords are words that the language uses, so they already have specific definitions that shouldn’t be rewritten&lt;/p&gt;

&lt;p&gt;Up next: &lt;a href="https://dev.to/zhenni_wu_28d825b0254dcde/guide-to-breaking-down-and-understanding-c-errors-2472"&gt;Guide to breaking down and understanding C# errors&lt;/a&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>csharp</category>
    </item>
  </channel>
</rss>
