Introduction to C#
Akin to introducing JavaScript for a web development enthusiast.
C#! OOP is more rigid and statically-typed compared to the dynamic, prototype-based OOP in JavaScript.
Let's write a 'Hello, World!' application in C#:
using System;
namespace HelloWorld
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello, World!");
}
}
}
using System
: Imports the `System` namespace
namespace HelloWorld
: Organizes code, similar to modules/packages in JavaScript
class Program
: Declares a class named `Program`
static void Main(string[] args)
: Entry point of the program
console.log('Hello, World!');
Console.WriteLine
in C# serves a similar purpose to
console.log
in JavaScript, outputting information to the console.
using System;
namespace HelloWorldApp {
class Program {
static void Main(string[] args) {
Console.WriteLine("Hello World!");
}
}
}
using System;
: Imports modules or libraries.
namespace HelloWorldApp
: Organizational container to avoid naming clashes.
class Program
: Encapsulates data and methods.
static void Main(string[] args)
: Entry point for a C# application.
Console.WriteLine("Hello World!");
: Prints text to console.
;
).
{}
).
//
.
class Greeter {
constructor() {
}
greet() {
console.log("Hello World!");
}
}
const greeter = new Greeter();
greeter.greet();
// Declaring a variable
int age;
// Assigning value to the variable
age = 25;
Here, `int` is a data type representing a 32-bit signed integer, and `age` is a variable of type `int`.
byte num1 = 255; // 8-bit unsigned integer
double price = 99.95; // 64-bit double-precision floating point
bool isCSharpFun = true; // Boolean
string name = "John"; // String type
object obj = name; // Object type, can hold any data type
In JavaScript, no specific data type declaration is needed when declaring variables using `let` or `const`.
let age = 25; // No need to specify the 'int' data type explicitly
let name = "John"; // Variables can hold any type and can be reassigned to different types
let isLearningCSharp = true; // No data type declaration required
+
), Subtraction (
-
), Multiplication (
*
), Division (
/
), and Modulus (
%
)
int a = 10;
int b = 3;
int addition = a + b; // 13
int subtraction = a - b; // 7
int multiplication = a * b; // 30
int division = a / b; // 3
int modulus = a % b; // 1
==
), Inequality (
!=
), Greater than (
>
), Less than (
<
), Greater than or equal to (
>=
), Less than or equal to (
<=
)
bool isEqual = (a == b); // false
bool isNotEqual = (a != b); // true
bool isGreaterThan = (a > b); // true
=
+=
,
-=
,
*=
,
/=
,
%=
a += b; // equivalent to a = a + b; a becomes 13
&&
), OR (
||
), NOT (
!
)
bool isTrue = true;
bool isFalse = false;
bool resultAND = isTrue && isFalse; // false
bool resultOR = isTrue || isFalse; // true
bool resultNOT = !isTrue; // false
&
), OR (
|
), XOR (
^
), NOT (
~
), Left Shift (
<<
), Right Shift (
>>
)
int c = a & b; // Bitwise AND
int d = a | b; // Bitwise OR
int e = a ^ b; // Bitwise XOR
?:
.
sizeof
and others
int max = (a > b) ? a : b; // If a > b, max = a; otherwise, max = b
let x = 5;
let y = 2;
// Arithmetic operations
let sum = x + y; // 7
// Comparison operations
let isLess = x < y; // false
// Logical operations
let orResult = (x > 0) || (y < 0); // true
The concepts of how operators work don't change across these languages — they are essential for performing calculations and logic in both JavaScript
if
,
else
,
else if
,
switch
,
? :
if
statement
Executes code block only if condition is true .
int number = 10;
if (number > 0)
{
Console.WriteLine("The number is positive.");
}
else
clause
Executes code when
if
condition is false.
if (number > 0)
{
Console.WriteLine("The number is positive.");
}
else
{
Console.WriteLine("The number is non-positive.");
}
else if
construct
Handles multiple conditions; only one block executes.
if (number > 0)
{
Console.WriteLine("The number is positive.");
}
else if (number < 0)
{
Console.WriteLine("The número is negative.");
}
else
{
Console.WriteLine("The número is zero.");
}
switch
statement
Tests a variable against multiple case values; runs matching code block.
switch (number)
{
case 1:
Console.WriteLine("The number is one.");
break;
case 2:
Console.WriteLine("The number is two.");
break;
default:
Console.WriteLine("The number is neither one nor two.");
break;
}
?:
conditional operator
Evaluates a boolean expression and returns one of two values.
string result = (number > 0) ? "positive" : "non-positive";
Console.WriteLine("The number is " + result);
Similar constructs in JavaScript:
if
,
else if
,
else
,
switch
,
? :
let number = 10;
if (number > 0) {
console.log("The number is positive.");
} else if (number < 0) {
console.log("The number is negative.");
} else {
console.log("The number is zero.");
}
switch
in JavaScript
switch (number) {
case 1:
console.log("The number is one.");
break;
case 2:
console.log("The number is two.");
break;
default:
console.log("The number is neither one nor two.");
break;
}
?:
operator in JavaScript
let result = (number > 0) ? "positive" : "non-positive";
console.log("The number is " + result);
Comparison with JavaScript eases the learning curve for C#.
for
foreach
while
do-while
for
Loop
Example:
for (int i = 0; i < 10; i++)
{
Console.WriteLine("Iteration " + i);
}
foreach
Loop
Example:
string[] names = { "Adam", "Bruce", "Charlie" };
foreach (string name in names)
{
Console.WriteLine(name);
}
while
Loop
Example:
int i = 0;
while (i < 3)
{
Console.WriteLine("Iteration " + i);
i++;
}
do-while
Loop
while
loop.
Example:
int i = 0;
do
{
Console.WriteLine("Iteration " + i);
i++;
} while (i < 3);
Example in JavaScript:
for (let i = 0; i < 10; i++) {
console.log("Iteration " + i);
}
Who designed the C# programming language?
Which of the following lines in the C# example provided is equivalent to the `console.log()` function in JavaScript?
Which operator would you use in C# to combine multiple boolean expressions ensuring all conditions must be true?
Which of the following statements correctly explains the behavior of the switch statement in C#?
Which looping construct in C# ensures that the block of code is executed at least once, regardless of the condition?