Introduction
It’s like an old tradition to introduce new programming language to the readers using a ‘Hello World!’ program, so keeping that in mind here are the steps to create your first Hello World program in C# and a step by step explanation of each line and keyword used in the program. We also have some examples where we would be consuming C# code in PowerShell and executing it. This approach makes it easy for the reader to understand both these languages and also use them together.
If you like reading this article follow me on Twitter @singhprateik, for more such content!
Table of Contents
- C# Types
- Value Types
- Reference Types
- Value vs Reference Types
- Type Conversions
- C# Variables
- C# Operators
- Unary operator
- Binary operator
- Ternary operator
- Video Tutorial
[Announcement] PowerShell to C# and back Book
This book bridges concept and knowledge the gap between a scripting language like PowerShell and modern programming language like C#, which is a natural language of choice for People who know PowerShell.
An increase in adoption of software development inside Infrastructure teams makes this the perfect time to get started with a programming language and C# is a natural choice for people using PowerShell today. As it fits best with the basket of matching skillsets like Azure, Azure DevOps which will add value in your career.
Download the FREE Book Sample from the book web page which covers all the basics of C# within 50 pages – https://leanpub.com/powershell-to-csharp 👇 👇 👇
C# Types
-
A Data type is an attribute that tells the compiler how to use and represent the data in memory.
-
C# is a strongly-typed language
-
That means we must declare the type of a variable that indicates the kind of values it is going to store, such as
integer
,float
,decimal
,text
, etc -
This is also known as Type Declaration
PowerShell Example:
$str = "hello world!" $str.GetType()
C# Example:
string str = "hello world!"; str.GetType()
All C# data types or simply ‘types’ can be classified into two broad categories:
- Value types
- Reference types
-
Values Types
-
- Holds a value within its own designated memory space
- Can not contain
null
values - Derived from base class
[System.ValueType]
Following is a list of value data types you can use in your C# programs.
.Net Type Type Alias Represents Range Default Value System.Byte byte 8-bit unsigned integer 0 to 255 0 System.Boolean bool Boolean value True or False False System.Char char 16-bit Unicode character U+0000 to U+ffff ‘\0’ System.Decimal decimal 128-bit precise decimal values with 28-29 significant digits (+/-)1.0 x 10^-28 to (+/-)7.9 x 10^28 0.0M System.Double double 64-bit double-precision floating point type (+/-)5.0 x 10^-324 to (+/-)1.7 x 10^308 0.0D System.Single float 32-bit single-precision floating point type (+/-)1.5 x 10^-45 to (+/-)3.4 x 1038 0.0F System.Int32 int 32-bit signed integer type -2,147,483,648 to 2,147,483,647 0 System.Int64 long 64-bit signed integer type -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 0L System.SByte sbyte 8-bit signed integer type -128 to 127 0 System.Int16 short 16-bit signed integer type -32,768 to 32,767 0 System.UInt32 uint 32-bit unsigned integer type 0 to 4,294,967,295 0 System.UInt64 ulong 64-bit unsigned integer type 0 to 18,446,744,073,709,551,615 0 System.UInt16 ushort 16-bit unsigned integer type 0 to 65,535 0 -
-
Reference Types
-
-
-
Contain references to other objects or memory locations
-
Actual data is not stored in a variable.
-
One or more variables can reference a single object, and any action performed by anyone variable changes the referenced object.
-
-
PowerShell Example:
$str = "This is a string"
$str.GetType() # reference type
$str[1].GetType() # value type
C# Example:
string str = "This is a string";
str.GetType() // reference type
str[1].GetType() // value type
-
- Derived from base class
[System.Object]
- Derived from base class
-
-
C# provides some built-in reference types such as:
dynamic
,object
,string
. -
You can create custom reference types using keywords:
class
interface
delegate
string
-
add-type @"
public class TestDataType {
public string First;
public string Last;
public string Phone;
}
"@
[TestDataType] @{First="Prateek";Last="Singh"}
$Object = [TestDataType] @{First="Prateek";Last="Singh"}
$Object.GetType()
Value vs Reference Types
Value type | Reference type | |
---|---|---|
Stores | Actual value | Memory location \ Reference to a value |
Allocation | Stack | Heap |
Lifetime | Lifetime of variable | Managed by .Net |
Nullable | Always has value | Maybe null |
Default | 0 | null |
Assignment | Copying actual data | Copying reference |
Type Conversions
PowerShell Example:
# float to int
[int] 3.14159
# string to char array
[char[]] 'prateek'
# int to char
[char] 97
[char] 98
# bool to int
[int] $true
[int] $false
# int to bool
[bool] 1
[bool] 0
# char to int
[int] [char]'Z'
# int to hex
"{0:x}" -f 397312
C# Examples:
// float to int
(int) 3.14159
// string to char array
"prateek".ToCharArray()
// int to char
(char) 97
(char) 98
// bool to int
(int) true // throws error - Cannot convert type 'bool' to 'int
true ? 1 : 0 // alternatively, use ternary operator
// char to int
(int) 'Z'
// int to hex
Convert.ToString(397312,16)
Convert.ToInt32(Convert.ToString(397312,16))
C# Variables
-
A variable is the name of the storage location to store values of data types supported by the programming language.
-
In C# depending upon the data type of the variable, a memory location with a specific size and layout is assigned to the variable.
To define and initialize a variable we follow the below-mentioned syntax:
// variable definition
<data-type> <variable-name>;
// multiple variable definition
<data-type> <variable-name1>, <variable-name2>, <variable-name3>;
// variable definition and initialization
<data-type> <variable-name> = value;
Here, <data-type>
can be one of the data types that we discussed in the previous subsection such as value types: char
,int
,float
and reference types or in other words a user-defined data types: Employee
, Car
etc. Following are some examples to demonstrate this:
public class example
{
static void Main(string[] args)
{
// single variable declaration only
bool flag;
// multiple variable declaration only
int a, b, c;
// variable declaration and initialization
double pi = 3.14;
char chr = 'a';
int num, count = 5;
int[] list = { 2, 4, 6, 8, 10 };
}
}
When declaring a variable in your C# program, you must explicitly specify the data type, otherwise, you can also use the ’var
’ keyword to let the compiler implicitly decide the type of a variable at compilation time.
Var
datatype was introduced in C# 3.0. var is used to declare implicitly typed local variable means it tells the compiler to figure out the type of the variable at compilation time.
var i = 0;
C# Operators, Operands and Expressions
Expressions are combinations or sequences of operands and operators and once an expression is evaluated, it returns a value.
The operators of expression represent operations to apply to the operands. For example: (x + y) * z
is an expression in which x
, y
, z
are operands and +
, *
are operators.
Operators
The operators in the C# Language can be categorized into, following 3 broad categories:
-
Unary operator
Unary operators take one operand to perform the operation and either prefixed or postfixed to the operand. Some common use cases can be increment (
++
), decrement (--
) and negative boolean (!
) operators, below are some examples that will help you understand.PowerShell Example:
# increment and decrement operators $x = 5; (++$x) # pre increment operator ($x++) # post increment operator (--$x) # pre decrement operator ($x--) # post decrement operator # negative boolean operator [bool] $flag = $true; !$flag # alternatively, -not $flag !$false !$true
C# Example:
// increment and decrement operators int x = 5; ++x // pre increment operator x++ // post increment operator --x // pre decrement operator x-- // post decrement operator // negative boolean operator bool flag = true; !flag !false !true
-
Binary operator
Binary operators take two operands to perform the operation and operator is in between the two operands,
- Arithmetic Operators: (
+
,-
,*
,/
,%
) - Logical Operators: OR (
||
) and logical AND (&&
) operators. - Relation Operators: (
>
,>=
,<
,<=
,==
,!=
) - Assignment Operators: (
=
)
PowerShell Example:
# arithmetic operators 1 + 2 13 - 12 5 * 5 15 / 3 29 % 4 # logical operators $x = $true; $y = $false; $x -or $y $x -and $y
C# Example:
// arithmetic operators 1 + 2 13 - 12 5 * 5 15 / 3 29 % 4 // logical operators var x = true; var y = false; x || y x && y
PowerShell Example:
# relational operators 1 -gt 0 "ONE" -eq "one" 5 -le 5 2 -ne 5 # assignment operators [int] $x, $y $x = 5 $y = $x + 3
C# Example:
// relational operators 1 > 0 "ONE" == "one" 5 <= 5 2 != 5 // assignment operators int x, y; x = 5 y = x + 3
- Arithmetic Operators: (
-
Ternary operator
A special operator that is used in decision making or to check conditions, this operator takes three operands.
Syntax:
<condition> ? <if true> : <if false>
PowerShell Example:
[int] $x = 5;
[int] $y = 7;
$x -gt $y ? "x greater-than y" : "x smaller-than y"
C# Example:
int x = 5;
int y = 7;
x > y ? "x greater-than y" : "x smaller-than y"
Video Tutorial
[02:22]PowerShell to C# and Back – Comments, Case sensitivity, Using directive
{ Presented by Deepak Dhami [Twitter] [Blog] }
[30:55] PowerShell to C# and Back – Types, Type Conversion, Variables and Operators
{ Presented by Prateek Singh [Twitter] [Blog] }
- C# Types
- Value Types
- Reference Types
- Value vs Reference Types
- Type Conversions
- C# Variables
- C# Operators
- Unary operator
- Binary operator
- Ternary operator
Author of “PowerShell Guide to Python“, “Windows Subsystem for Linux (WSL)” and currently writing the most awaited book: “PowerShell to C# and Back” !
Prateek Singh
Related posts
Categories
Author of Books
Awards
Open Sourced Projects
Author at
Blog Roll
Mike F RobbinsDamien Van Robaeys
Stéphane van Gulick
Kevin Marquette
Adam Bertram
Stephanos Constantinou
Francois-Xavier Cat
Ravikanth Chaganti
Roman Levchenko
Blog Stats
- 1,131,876 People Reached