Getting Started with C#

C# is a modern, object-oriented programming language developed by Microsoft. Itโ€™s widely used for building desktop applications, web apps, games, and software using the .NET framework.

As a software engineering student, I started learning C# and .NET through Microsoft learn and other resources.

This blog is my personal learning journal where I document what I study, practice, and build.

1) Write the first C# code ๐Ÿš€ :

Today, I wrote my very first C# program, and I noticed a few important things that might seem small, but they really matter!

Console.WriteLine("Hello World!");
  • C# is case-sensitive, which means Console.WriteLine it must be written exactly with the correct casing. Writing console.writeline will result in an error.

  • A semicolon (;) is required at the end of each statement.

  • Console.WriteLine() prints the output with a line break. To print without a line break, use Console.Write() instead.

2) Data Types๐Ÿ—’๏ธ:

  • char โ†’ Stores a single character, e.g., 'A'

  • int โ†’ Stores whole numbers, e.g., 123

  • string โ†’ Stores text, e.g., "Hello World"

  • float โ†’ Stores decimal numbers (less precision), e.g., 0.2f

  • double โ†’ Stores decimal numbers (more precision), e.g., 3.1457

  • decimal โ†’ Used for high-precision calculations, e.g., 12.345m

  • bool โ†’ Stores true or false values

Console.WriteLine('A');  //Char
Console.WriteLine(123);  //Int
Console.WriteLine("Hello World!");  //String
Console.WriteLine(0.2f);  //float
Console.WriteLine(3.1457); //double
Console.WriteLine(12.345m);  //Decimal
Console.WriteLine(true); //bool

3) Variables๐Ÿšฉ:

Here are a few important things about C# variables:

  • Can contain alphanumeric characters and the underscore(โ€˜_โ€˜) character

  • Special characters are not allowed(โ€˜$โ€˜, โ€˜#โ€˜)

  • Must begin with a letter or underscore. Canโ€™t begin with a Number

  • Case sensitive. (โ€œNameโ€ and โ€œnameโ€œ are two different variables.)

  • Names must not be keywords

String name = "Chathush";
Console.WriteLine(name);

Implicit type of local variable(Var):

An implicit type of variable is created using var Keyword. The var keyword tells the C# compiler that the assigned value implies the data type.

We can change the assigned value after this type of variable.

var name = "Chathush";
Console.WriteLine(name);

You can also print variables in this way,

var name = "Chathush";
var count = 3;

Console.WriteLine($"Hello, {name}! You have {count} cars");
// Output -> Hello, Chathush! You have 3 cars

4) Type Casting ๐ŸŽ—๏ธ :

Explicit casting:

double myDl = 3.14;
int myInt = (int) myDl; 

Console.WriteLine(myInt);
// output -> 3

Type conversion methods:

There are some built-in methods in C# for type casting. Such as,

  • ToBoolean()

  • ToDouble()

  • ToString()

  • ToInt32()

  • ToInt64()

double myNum = 3.14;

Console.WriteLine(Convert.ToInt32(myNum));
// output -> 3

5) Get User Inputs ๐Ÿ”ค :

We use Console.ReadLine() for getting input from the user. It always returns a String type of value. Therefore for if we need any calculation from inputs, we must typecast the input to the appropriate data type.

Console.WriteLine("Enter your name: ");
String name = Console.ReadLine();

5) C# Math๐Ÿ”ข :

C# has many built-in methods for doing mathematical calculations.

  • Math.max(x,y) โ†’ Find the maximum value

  • Math.min(x,y) โ†’ Find the minimum value

  • Math.sqrt(x) โ†’ Return the square root of โ€˜xโ€˜

  • Math.abs(-3.14) โ†’ Return absolute value(Postive value)

  • Math.round(9.56) โ†’ Round the number to the nearest whole number

6) Loops โžฟ :

If you used loops in other languages, C# loops are the same as those.

Type of loops:

  • for loop

      for (int i = 0; i < 5; i++)
      {
          Console.WriteLine("Count: " + i);
      }
    
  • while loop

      int i = 0;
      while (i < 5)
      {
          Console.WriteLine("While Loop: " + i);
          i++;
      }
    
  • do-while loop

      int i = 0;
      do
      {
          Console.WriteLine("Do While Loop: " + i);
          i++;
      }
      while (i < 5);
    
  • foreach loop

      string[] fruits = { "Apple", "Banana", "Cherry" };
      foreach (string fruit in fruits)
      {
          Console.WriteLine(fruit);
      }
    

My thoughts ๐Ÿง :

Learning C# has been exciting and a bit challenging, but Iโ€™m enjoying every part of it.

If youโ€™re also learning C# or just starting out, feel free to follow my blog for regular updates.

Thanks for reading ๐Ÿ‘‹ !

Iโ€™m Chathush, a Software Engineering student sharing my journey of learning .NET from scratch.

0
Subscribe to my newsletter

Read articles from Chathush Vishmika directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Chathush Vishmika
Chathush Vishmika

Sharing my journey as I learn web development from scratch. Writing to learn, grow, and help other beginners along the way. ๐Ÿ‘จโ€๐Ÿ’ป๐Ÿ“˜