Skip to content

Instantly share code, notes, and snippets.

@mezcel
Last active April 28, 2021 04:27
Show Gist options
  • Save mezcel/41a2e751c97eb0f6842e4fafe6e9ad63 to your computer and use it in GitHub Desktop.
Save mezcel/41a2e751c97eb0f6842e4fafe6e9ad63 to your computer and use it in GitHub Desktop.
Csharp notes

Csharp notes

.Net SDK

Download


.NET 5.0 (recommended)

.NET is a free, cross-platform, open-source developer platform for building many different types of applications.

.NET 5.0 (recommended) x64 .NET 5.0 (recommended) x86
dotnet-sdk-5.0.202-win-x64.exe dotnet-sdk-5.0.202-win-x86.exe
windowsdesktop-runtime-5.0.5-win-x64.exe windowsdesktop-runtime-5.0.5-win-x86.exe

.NET Core 3.1 (LTS)

.NET Core is a free, cross-platform, open-source developer platform for building many different types of applications.

.NET Core 3.1 (LTS) x64 .NET Core 3.1 (LTS) x86
dotnet-sdk-3.1.408-win-x64.exe dotnet-sdk-3.1.408-win-x86.exe
windowsdesktop-runtime-3.1.14-win-x64.exe windowsdesktop-runtime-3.1.14-win-x86.exe

Terminal Projects

## Init new project with auto generated hello-world demo
dotnet new console

## Add or modify *.cs files

## Run project
dotnet run

Export as a packaged executable

dotnet publish --output "c:/temp/myapp" --runtime win-x64 --configuration Release -p:PublishSingleFile=true -p:PublishTrimmed=true --self-contained true

Visual Studio

## build executable
csc -out:myProgram.exe Program.cs

Enable csc in terminal

Locate the path of csc.exe and add it your PATH environment variable: \

  • the path for 64-bit C# compiler is C:\Windows\Microsoft.NET\Framework64\v4.0.30319 \
  • 32-bit C# compiler in C:\Windows\Microsoft.NET\Framework
/*
csc BasicTest.cs
.\BasicTest.exe
*/
using System;
namespace BasicTest {
public class Program {
public static int myAdd(int a, int b) {
return (a + b);
}
public static int myMultiply(int a, int b) {
return (a * b);
}
public static void Main() {
Console.WriteLine("Hello world 2!");
//int num = BasicTest.Multiply(5, 2);
int num = BasicTest.Program.myAdd(5, 2);
Console.WriteLine( "BasicTest.Program.myAdd(5, 2)=" + num );
num = BasicTest.Program.myMultiply(5, 2);
Console.WriteLine( "BasicTest.Program.myMultiply(5, 2)=" + num );
}
}
}
#GARYTOWN.COM @gwblok
# https://garytown.com/edit-add-remove-system-path-variable-via-powershell
#Script to Add and/or remove items from the System Path
<#
Locate the path of csc.exe and add it your PATH environment variable:
the path for 64-bit C# compiler is C:\Windows\Microsoft.NET\Framework64\v4.0.30319
32-bit C# compiler in C:\Windows\Microsoft.NET\Framework
#>
#Get Current Path
$Environment = [System.Environment]::GetEnvironmentVariable("Path", "Machine")
#Remove "Junk" from Path
foreach ($path in ($Environment).Split(";")) {
if ($path -like "*SysWOW64\WIndowsPowerShell\v1.0*") {
$Environment = $Environment.Replace($Path ,"")
}
if ($path -like "c:\temp*") {
$Environment = $Environment.Replace($Path ,"")
}
}
#Add Items to Environment
$AddPathItems = "C:\Windows\Microsoft.NET\Framework64\v4.0.30319\;"
$Environment = $Environment.Insert($Environment.Length,$AddPathItems)
#Set Updated Path
[System.Environment]::SetEnvironmentVariable("Path", $Environment, "Machine")
:: dotnet-new-console-win10.bat
:: clear previous builds
rem bin
rem obj
rem *.csproj
:: init new dotnet project
dotnet clean
dotnet new console --force
:: import desired Program.cs
xcopy Program.txt Program.cs /Y
/*
Locate the path of csc.exe and add it your PATH environment variable:
the path for 64-bit C# compiler is C:\Windows\Microsoft.NET\Framework64\v4.0.30319
32-bit C# compiler in C:\Windows\Microsoft.NET\Framework
*/
using System;
namespace HelloWorld {
public class Program
{
public static void Main(){
Console.WriteLine("`nHello world!`n");
}
}
}
## Run CSharp through Powershell
## https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/add-type?view=powershell-7.1#:~:text=The%20Add-Type%20cmdlet%20lets%20you%20define%20a%20Microsoft,type%20by%20specifying%20an%20existing%20assembly%20or%20
Clear-Host
function hello1 {
Write-Host "`n--- CS script in PS1`n"
$hiWorld = @"
using System;
namespace HelloWorld
{
public class Program
{
public static void Main(){
Console.WriteLine("Hello world!");
}
}
}
"@
Add-Type -TypeDefinition $hiWorld -Language CSharp
Invoke-Expression "[HelloWorld.Program]::Main()"
}
function HiCSFile {
Write-Host "`n--- CS script from CS file`n"
[string] $myCSFile = Get-Content -Path .\HiCS.cs
Write-Host "file content:`n$myCSFile`n"
Add-Type -TypeDefinition $myCSFile -Language CSharp
$HiCSObject = New-Object HiCS
[string] $hi = $HiCSObject.hi();
Write-Host "HiCSObject.hi() = $hi"
}
function multiplyDemo {
Write-Host "`n--- PS1 multiply script`n"
$namespaceSource = @"
public class BasicTest
{
public static int myAdd(int a, int b)
{
return (a + b);
}
public int myMultiply(int a, int b)
{
return (a * b);
}
}
"@
Add-Type -TypeDefinition $namespaceSource
[int] $add1 = [BasicTest]::myAdd(4, 3)
Write-Host "add = $add1"
$BasicTestObject = New-Object BasicTest
$ans = $BasicTestObject.myMultiply(5, 2)
Write-Host "Multiply = $ans"
}
hello1
HiCSFile
multiplyDemo
using System;
namespace PartialClasses {
class Program {
static void Main(string[] args) {
PartialClass pc = new PartialClass();
pc.HelloWorld();
pc.HelloUniverse();
}
}
}
public class HiCS
{
public string hi() {
return "Hello CS world file!";
}
}
#/bin/bash
function InstallCsharp {
## Install the .NET SDK or the .NET Runtime on Debian
## https://docs.microsoft.com/en-us/dotnet/core/install/linux-debian
## Microsoft package signing key to your list of trusted keys and add the package repository.
wget https://packages.microsoft.com/config/debian/10/packages-microsoft-prod.deb -O packages-microsoft-prod.deb &&
sudo dpkg -i packages-microsoft-prod.deb &&
## Install the SDK
## The .NET SDK allows you to develop apps with .NET. If you install the .NET SDK, you don't need to install the corresponding runtime.
sudo apt-get update; \
sudo apt-get install -y apt-transport-https && \
sudo apt-get update && \
sudo apt-get install -y dotnet-sdk-5.0
## The ASP.NET Core Runtime allows you to run apps that were made with .NET that didn't provide the runtime. The following commands install the ASP.NET Core Runtime, which is the most compatible runtime for .NET.
sudo apt-get update; \
sudo apt-get install -y apt-transport-https && \
sudo apt-get update && \
sudo apt-get install -y dotnet-runtime-5.0
#sudo apt-get install -y aspnetcore-runtime-5.0
}
function DisableTelemetry {
## Disable telemetry
export DOTNET_CLI_TELEMETRY_OPTOUT=1
## Disable telemetry
echo "~/.bashrc" | grep "DOTNET_CLI_TELEMETRY_OPTOUT=1"
if [ $? -ne 0 ]; then
echo -e "\n## Disable telemetry\nexport DOTNET_CLI_TELEMETRY_OPTOUT=1" >> ~/.bashrc
fi
}
function main {
DisableTelemetry
InstallCsharp
DisableTelemetry
}
thisDir=$(pwd)
mkdir -p ~/Downloads/
cd ~/Downloads/
main
cd $thisDir
#!/bin/bash
## Installation via Direct Download - Debian 10
## Download the tar.gz package powershell-7.1.3-linux-x64.tar.gz from the releases page onto the Debian machine.
## https://docs.microsoft.com/en-us/powershell/scripting/install/installing-powershell-core-on-linux?view=powershell-7.1#debian-10
function direct_dl {
## https://github.com/PowerShell/PowerShell/releases/tag/v7.1.3
## https://github.com/PowerShell/PowerShell/releases/download/v7.1.3/powershell_7.1.3-1.debian.10_amd64.deb
sudo apt-get update
# install the requirements
sudo apt-get install -y \
less \
locales \
ca-certificates \
libicu63 \
libssl1.1 \
libc6 \
libgcc1 \
libgssapi-krb5-2 \
liblttng-ust0 \
libstdc++6 \
zlib1g \
curl
# Download the powershell '.tar.gz' archive
curl -L https://github.com/PowerShell/PowerShell/releases/download/v7.1.3/powershell-7.1.3-linux-x64.tar.gz -o /tmp/powershell.tar.gz
# Create the target folder where powershell will be placed
sudo mkdir -p /opt/microsoft/powershell/7
# Expand powershell to the target folder
sudo tar zxf /tmp/powershell.tar.gz -C /opt/microsoft/powershell/7
# Set execute permissions
sudo chmod +x /opt/microsoft/powershell/7/pwsh
# Create the symbolic link that points to pwsh
sudo ln -s /opt/microsoft/powershell/7/pwsh /usr/bin/pwsh
# Start PowerShell
pwsh
}
function aptPackage {
cd ~/Downloads/
## Download the Microsoft repository GPG keys
wget https://packages.microsoft.com/config/debian/10/packages-microsoft-prod.deb &&
## Register the Microsoft repository GPG keys
sudo dpkg -i packages-microsoft-prod.deb
## Update the list of products
sudo apt-get update
## Install PowerShell
sudo apt-get install -y powershell
## Start PowerShell
#pwsh
}
thisDir=$(pwd)
mkdir -p ~/Downloads/
cd ~/Downloads/
aptPackage
cd $thisDir
using System;
namespace PartialClasses {
public partial class PartialClass {
public void HelloWorld() {
Console.WriteLine("Hello, world!");
}
}
}
using System;
namespace PartialClasses {
public partial class PartialClass {
public void HelloUniverse() {
Console.WriteLine("Hello, universe!");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment