C# remains a popular computer programming language in 2021, staying within the top five spots above the likes of R or Swift.

Based on the C family, C# offers an object-orientated and type-safe coding experience. It enables developers to build apps in the .NET ecosystem on platforms like Windows and the web.

However, C# isn’t the easiest of languages to learn. That’s why we’ve created this simple guide to introduce you to the top 10 C# basics.

Read on to learn about data types, classes, and collections. You’ll learn how to manage large chunks of text and how to print in C#. And even how to use it to convert to PDF.

1. C# Developer IDE

Before you write your first ‘Hello World’ program, you need to install an integrated development environment or IDE.

An IDE provides all the tools you require to write, test, debug, and run your C# applications. That includes a compiler to convert your code into a machine-readable language.

Microsoft recommends installing their Visual Studio IDE.

You’ll need at least 2GB of RAM, 1.8Ghz processor faster, Windows 10, and the latest .NET framework. It’s free for individual use and includes code refactoring and intelligent suggestions.

2. Data Types

Data types define the type of information that you work with. That includes:

• string – text data

• int – whole numbers

• float and double – fractional numbers

• bool – true/false values

• System.DateTime – date and time data

C# uses strict data types meaning you can’t easily edit a particular data type once it’s defined. This ensures there’s less confusion when working with lots of variables, especially in a large application.

3. Collections

Collections or arrays are powerful tools in the C# developer’s arsenal. They allow you to gather simple or complex types of data in a list. From there, you can loop or iterate over them to produce powerful results.

The ArrayList collection can store different types of objects dynamically by using the .Add() method. For example:

using System.Collections;

var alist = new ArrayList();
alist.Add(“Hello world”);

Other collection types include IEnumerable, Stack, and List. Learn what each one does, including their strengths and weaknesses, to unlock C#’s potential.

4. C# Classes

C# is an object-orientated language. This means it groups blocks of logic into objects or classes.

A simple way to visualize this is by picturing the blueprint of a car.

The Car blueprint or class contains all the functionality that any car has. It can drive forwards, reverse, turn, and brake. Yet not all cars are the same. Some offer larger engines or a better entertainment system or fuel injection.

C# allows you to extend classes by taking the base class and modifying it as you wish. Like the car scenario, you can alter or add new features but still have access to the core set.

5. Casting C# Basics

Although C# enforces strict typing sometimes you’ll need to convert data from one type to another. For example, converting a floating-point number to an integer.

This process is called casting and works by adding brackets around the type you want to convert to:

float floatNumber = 0.6;
int integerNumber = (int)floatNumber;

However, if you try to cast non-compatible data types you’ll get a System.InvalidCastException error. To avoid that and return a null instead, use ‘as a’:

string text = integerNumber as a string;

From there you can do a null check and not crash your app.

6. StringBuilder Object 

Joining or concatenating text in C# means using the + symbol. But working with strings of text can become unruly, especially if you manage large amounts.

The StringBuilder object is designed to make your life easier by offering several methods to add, remove, and replace text.

Use it alongside methods like string.Concat() and string.Join() to work with text more productively. Don’t try to do it all yourself with dozens of + symbols!

7. Convert to PDF

Converting content like text or HTML to the popular PDF format is an essential skill to learn. Thankfully, C# print to PDF third-party tools exist to make this process a snap.

The first step is to install the C# print library.

You can download the DLL or use NuGet. The library will then display directly within your Visual Studio project.

From there you include the library at the top of the code, instantiate it, and choose your conversion source.

The best C# print to PDF tools allow you to convert from a URL or by including HTML. You can also choose your printer settings to fine-tune the output.

8. How to Print in C#

The PDF tool mentioned above also lets you send formatted data directly to a printer.

You can select the printer name to use, change the resolution, and the number of pages to print. You can also bypass a physical printer and send the information directly to a file. For example:

var printDoc= pdfDoc.GetPrintDocument();
printDoc.PrintToFile(“<PathToFile>”, false);

Replace <PathToFile> to the path on your network and the file will save if you have the right permissions.

9. Interfaces

C# interfaces expand your classes by allowing other apps and objects to access their features.

It’s technically an abstract class in that its methods don’t contain any code. Other classes call these methods and overwrite them.

Interfaces are used to hide and secure details. Unlike ‘normal’ classes, they support multiple inheritance so you can combine classes together.

10. Shorthand Conditional Operators

Every computer language uses conditions to test code. The most popular is the if..else statement. However, it takes up several lines and looks rather bloated.

Instead, use the ?: conditional operators like the example below:

string result = 10 > 5 ? “it is bigger” : “it is smaller”;

The condition or the ‘if’ part starts on the left. In this case, is 10 larger than 5?

The question mark separates the condition from the ‘true’ answer. The colon separates the value that’s false.

All C# developers aim to keep their code concise and easy to read. Use shorthand operators like this to stay professional.

C# Programming and More at Computer Tech Reviews

This article has introduced you to Microsoft’s high-level language by sharing 10 C# basics to pique your interest.

C# developers can build any type of app, from Cloud-native apps to Windows libraries and client applications. C# print functions let you send the content directly to the printer or convert to PDF. The sky’s the limit!

Read more about C# programming and software tools in our Technology section.

Our articles also cover hardware, cybersecurity, gadgets, and new trends like AI. Bookmark our site and join us on social media to have your say on C#.