Skip to content

Instantly share code, notes, and snippets.

@peinearydevelopment
Last active September 20, 2018 13:13
Show Gist options
  • Save peinearydevelopment/d11751f0742cdb6eed9847fce1b2f0a6 to your computer and use it in GitHub Desktop.
Save peinearydevelopment/d11751f0742cdb6eed9847fce1b2f0a6 to your computer and use it in GitHub Desktop.
C# Byte Sized: Variables
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.1</TargetFramework>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>
</Project>
using System;
using System.Runtime.InteropServices;
namespace CSharpByteSizedVariables
{
class Program
{
static void Main(string[] args)
{
/*
* Demonstrate program using different data types
* to display different outputs for the same 'in-memory' value
*/
byte b = 33;
char c = (char)33;
Console.WriteLine(b.ToString());
Console.WriteLine(c.ToString());
Console.WriteLine();
/*
* Shows different memory allocation sizes for different data types
*/
short s = short.MinValue;
int i = int.MinValue;
long l = long.MinValue;
double d = double.MinValue;
Console.WriteLine(Marshal.SizeOf(c));
Console.WriteLine(Marshal.SizeOf(b));
Console.WriteLine(Marshal.SizeOf(s));
Console.WriteLine(Marshal.SizeOf(i));
Console.WriteLine(Marshal.SizeOf(l));
Console.WriteLine(Marshal.SizeOf(d));
Console.WriteLine();
UnsafeCodeExamples();
}
/*
* Method marked as unsafe and 'unsafe' code needs to be allowed in csproj
* 'unsafe' is it is accessing 'unmanaged memory'
*/
static unsafe void UnsafeCodeExamples()
{
int i2;
int* p2 = &i2;
IntPtr ip2 = new IntPtr(p2);
Console.WriteLine(Marshal.ReadInt32(ip2));
int i3 = int.MaxValue;
int* p3 = &i3;
IntPtr ip3 = new IntPtr(p3);
Console.WriteLine(Marshal.ReadInt32(ip3));
}
}
}
// Program
using System;
using System.Runtime.InteropServices;
internal class Program
{
private static void Main(string[] args)
{
byte structure = 33;
char structure2 = '!';
Console.WriteLine(structure.ToString());
Console.WriteLine(structure2.ToString());
Console.WriteLine();
short structure3 = short.MinValue;
int structure4 = -2147483648;
long structure5 = -9223372036854775808L;
double structure6 = -1.7976931348623157E+308;
Console.WriteLine(Marshal.SizeOf(structure2));
Console.WriteLine(Marshal.SizeOf(structure));
Console.WriteLine(Marshal.SizeOf(structure3));
Console.WriteLine(Marshal.SizeOf(structure4));
Console.WriteLine(Marshal.SizeOf(structure5));
Console.WriteLine(Marshal.SizeOf(structure6));
Console.WriteLine();
UnsafeCodeExamples();
}
private unsafe static void UnsafeCodeExamples()
{
int num = default(int);
int* value = &num;
IntPtr ptr = new IntPtr(value);
Console.WriteLine(Marshal.ReadInt32(ptr));
int num2 = 2147483647;
int* value2 = &num2;
IntPtr ptr2 = new IntPtr(value2);
Console.WriteLine(Marshal.ReadInt32(ptr2));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment