5 C# Tips that you MUST know NOW!!! ⚡

If you have taken a call to grow your career in the information technology sector, knowledge of coding is essential. It is the most in-demand skill in the industry. Thus, the programming knowledge you gain and practice, in the beginning, is priceless.


This content originally appeared on DEV Community and was authored by Dotnetsafer

If you have taken a call to grow your career in the information technology sector, knowledge of coding is essential. It is the most in-demand skill in the industry. Thus, the programming knowledge you gain and practice, in the beginning, is priceless.

Here are 5 good C# tips that will be of great help to you🤗

1. Nullable number

This tip is based on knowing that numbers CAN accept nulls. This tip is strange for many developers of other languages because we will use the symbol ? after the data type.

In this case we will use int but first let’s see what it would look like without the symbol ? 👇

using System;
namespace dotnetsafer
{
  class Program
  {
    static void Main(string[] args)
    {
      int number = null;
      Console.WriteLine(number);
    }
  }
}

If we simply copy and paste this code, when executed it generates an error as you can see in the following image 👇

csharp

Now we will simply add the symbol ? to int to look like int? .

Let’s see how the code would look like 👇

using System;
namespace dotnetsafer
{
  class Program
  {
    static void Main(string[] args)
    {
      int? number = null;
      Console.WriteLine(number);
    }
  }
}

Now let’s run it again and see what happens 👇

csharp

Good! It has worked, it has not returned any error and if we check the value of number we can indeed see that yes, it is null.

2. Readonly value

In a field statement, readonly indicates that the assignment to a field can only occur as part of the declaration or in a constructor of the same class. That is, READ ONLY.

Based on this, let’s see how it works with this example 👇

using System;
namespace dotnetsafer
{
  class Program
  {
    public static readonly string Url = "dotnetsafer.com";
  }
}

Here we can simply see that the Url value is “dotnetsafer.com”.
But… what if we try to change it later, is it possible? Let’s find out 👇

using System;
namespace dotnetsafer
{
  class Program
  {
    public static readonly string Url = "dotnetsafer.com";
    static void Main(string[] args)
    {
      Url = "";
    }
  }
}

We tried to change the Url value to null and no, it didn’t let us 👇

csharp

An example of use of readonly is in the connection to a database since it is always the same and we are not interested that nobody can change it.

Readonly is curious because if it is supposed to be a variable that can only be read and not modified… Could the definition of constant be applied to it?

So Readonly and Const are the same thing?

You can find the answer in this article 👇

👉👉👉👉 Const vs Readonly Explanation 👈👈👈👈

3. Detect null strings

In this tip we are going to see how we can detect if a string is null or not null. For this we are going to reuse the example of the previous tip 👇

using System;
namespace dotnetsafer
{
  class Program
  {
    public static readonly string Url = "dotnetsafer.com";
    static void Main(string[] args)
  }
}

Now we are going to use sting.IsNullOrEmpty. This checks strings for references to null or empty strings. We simply return in console if it is null or not null 👇

using System;
namespace dotnetsafer
{
  class Program
  {
    public static readonly string Url = "dotnetsafer.com";
    static void Main(string[] args)
    {
      if (string.IsNullOrEmpty(Url))
        Console.WriteLine ("This string is null or empty.");
      else
        Console.WriteLine("This string is not null or empty.");
    }
  }
}

And if we execute we can see that it returns that it is not empty (as its value is “dotnetsafer.com”) 👇

csharp

4. Terminate application

Here we are going to see a very simple way for the application to exit its execution when a condition is met. Let’s see the example that we are going to use 👇

using System;
namespace dotnetsafer
{
  class Program
  {
    static void Main(string[] args)
    {
      int number = 1;
      if (number == 1)
      {
        Console.WriteLine("Exit");
      }
      Console.WriteLine("No exit");
    }
  }
}

At this point I want that when the condition is met (which is met), the application exits its execution. So we will use Environment.FailFast and exit with the indicated argument: “Successfully exited” 👇

using System;
namespace dotnetsafer
{
  class Program
  {
    static void Main(string[] args)
    {
      int number = 1;
      if (number == 1)
      {
        Console.WriteLine("Exit");
        Environment.FailFast("Successfully exited");
      }
      Console.WriteLine("No exit");
    }
  }
}

When running it, we can indeed see that the application exits its execution correctly.

csharp

5. Line break

Who is not accustomed to making line breaks with /n ? 🤔
Well, in C# there is a special way to make them. For it, inside the environment class we have NewLine 👇

using System;
namespace dotnetsafer
{
  class Program
  {
    static void Main(string[] args)
    {
      Console.WriteLine($"First{Environment.NewLine}Second");
    }
  }
}

With this we will have a line break. Let’s run to check it 👇

csharp

These have been 5 basic tips. I NEED YOU to comment me WHAT LEVEL you want the following (COMMENT me with EMOJI):

🟢 Basic level
🟠 Intermediate Level
🔴 Advanced Level

The emoji that is repeated the most, wins!


This content originally appeared on DEV Community and was authored by Dotnetsafer


Print Share Comment Cite Upload Translate Updates
APA

Dotnetsafer | Sciencx (2021-10-07T17:20:23+00:00) 5 C# Tips that you MUST know NOW!!! ⚡. Retrieved from https://www.scien.cx/2021/10/07/5-c-tips-that-you-must-know-now-%e2%9a%a1/

MLA
" » 5 C# Tips that you MUST know NOW!!! ⚡." Dotnetsafer | Sciencx - Thursday October 7, 2021, https://www.scien.cx/2021/10/07/5-c-tips-that-you-must-know-now-%e2%9a%a1/
HARVARD
Dotnetsafer | Sciencx Thursday October 7, 2021 » 5 C# Tips that you MUST know NOW!!! ⚡., viewed ,<https://www.scien.cx/2021/10/07/5-c-tips-that-you-must-know-now-%e2%9a%a1/>
VANCOUVER
Dotnetsafer | Sciencx - » 5 C# Tips that you MUST know NOW!!! ⚡. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/10/07/5-c-tips-that-you-must-know-now-%e2%9a%a1/
CHICAGO
" » 5 C# Tips that you MUST know NOW!!! ⚡." Dotnetsafer | Sciencx - Accessed . https://www.scien.cx/2021/10/07/5-c-tips-that-you-must-know-now-%e2%9a%a1/
IEEE
" » 5 C# Tips that you MUST know NOW!!! ⚡." Dotnetsafer | Sciencx [Online]. Available: https://www.scien.cx/2021/10/07/5-c-tips-that-you-must-know-now-%e2%9a%a1/. [Accessed: ]
rf:citation
» 5 C# Tips that you MUST know NOW!!! ⚡ | Dotnetsafer | Sciencx | https://www.scien.cx/2021/10/07/5-c-tips-that-you-must-know-now-%e2%9a%a1/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.