What is C#?
C# is a programming language that Microsoft cooked up for building a whole range of applications, from Windows systems (no surprise there) to games, virtual reality, augmented reality, backend services and web applications.
It's especially popular with banks and big corporations, who use it for their enterprise solutions. The main perks? A strong community, full support from Microsoft, and strong typing. It even borrows some handy features from both Java and C++. In a nutshell, C# is Microsoft's modern solution for a wide variety of software development needs.
Strong typing: Strict type system, enhances code reliability by ensuring only compatible types interact. Catching errors early simplifies debugging and maintenance. Slower to write, easier to debug and troubleshoot you will save time in the long run.
using System;
namespace HelloMars
{
class Program
{
static void Main()
{
Console.WriteLine("Hello Mars!");
}
}
}
This is how we would print our first message to the console. Console.WriteLine("")
will print whatever is between " "
. It's the console.log("")
equivalent of JavaScript.
using System
imports fundamental classes and base types used in C#. It's like telling our computer, "Hey, we need these to make our code work!"namespace HelloMars
, is where we get organized. We use namespaces to group related classes and types. In C#, it helps keep things well organized, especially when working on large applications.
class Program
classes are like building blocks in C#. They let us define methods and variables that give our program its behavior and structure.
static void Main()
It's the method that gets called when our program runs. We mark it as static because it belongs to the class, not an instance. The "void" part means our program won't be returning anything.
Method:Action performed by an object or class.
Instance: Object created from a class..
Guess what does this code do?
using System
namespace GettingInput
{
class Program
{
static void Main()
{
Console.WriteLine("Type something");
string input = Console.ReadLine();
Console.WriteLine($"you typed {input}");
}
}
}
Console.WriteLine
: It allows the user to enter text in the console.string
: It tells the program that the expected data is text (a string). You can use other data types for different kinds of information.Console.ReadLine()
: It captures the text entered by the user in the console.Console.WriteLine($"You typed {input}");
: It displays the entered text back to the console, using string interpolation. The$
symbol allows us to directly insert the value of theinput
variable into the string.String interpolation: It helps us include variables inside a string. We use
{}
with a variable name{input}
to insert its value into the string when usingConsole.WriteLine()
. Without$
we wouldn't be able to call{input}
directly.
Writing comments to your code is essential, because on large scale apps people will forget what that line of code do. Do not trust your memory for everything. Sometimes I forget the reason behind a line of code after a month or two. It's always great idea to document your work. Use //
for single line and /* */
for multiple line comments.
// this is a single line
// this is a single line too
/* multiple
lines
of
comments
*/
Good things about C#
When you first encounter C#, you might think it requires more typing compared to JavaScript, which can make it seem slower. But don't let that fool you! C# is actually blazingly fast when it comes to execution and performance.
Thanks to the versatile .NET Core framework, you can create applications for a wide range of platforms, including iOS, Android, Windows, and macOS. It opens up exciting possibilities for developing software that works seamlessly across different devices.
C# and the .NET framework are in high demand in the job market. Banks and Fortune 500 companies, rely heavily on C# and .NET for their large-scale applications. They're not going to abandon ship and switch to a different language easily, considering the high costs associated with rewriting existing systems.
- While C# and .NET may not be considered as "cool" as frameworks like React or Vue, this can actually work to your advantage when it comes to landing entry-level job positions. The popularity of trendy frameworks often means more competition for those roles. On the other hand, the demand for entry-level .NET developers remains high, as many large companies rely on C# and .NET for their critical applications.