Showing posts with label c#. Show all posts
Showing posts with label c#. Show all posts

Thursday, 25 June 2026

The Powerful System.Environment

System.Environment is an all-powerful class.  

It allows interaction with the current environment and platform. It is something to revere when developing intelligent agents that need to navigate the user's environment.

System.Environment.SpecialFolder is a well-known enum.  

It contains the CSIDLs (these are not .NET "things", they are Windows "things"). 

CSIDL = Constant Special Item ID List.  Here are some of the popular values.

Desktop 0  (logical desktop rather than physical file system location)
ApplicationData 26
LocalApplicationData 28
System 37
ProgamFiles 38

A full rundown of CSIDLs is here.

Friday, 24 April 2026

Why does TypeScript feel a bit C-Sharpy?

TypeScript was created by Anders Hejlsberg, a Danish software engineer, in 2012. He formerly created C# around the year 2000. He is also known for Turbo Pascal and Delphi, both extraordinary products in their time. Deservedly he is a Microsoft Technical Fellow (a list of whom appear here).

Saturday, 7 February 2026

Keep up to Speed with C# Language Design Discussions

The content of language meetings are detailed here.

Thursday, 20 November 2025

The Concept of C# Scripting (.csx files)

The concept of C# scripting bears semblance to Jython in Java (more so than JavaScript to Java).

Scripting commands will not be allowed in regular programs.

Here's an example:

#r "nuget: Microsoft.SuperAdvancedKernel, 1.23.0"

It can be used in environments like .NET Interactive and Jupyter Notebooks with .NET. It came into being in Visual Studio 2015.

The file needs to be .csx file.

#r is the reference directive, to reference an assembly or package.

Sunday, 7 September 2025

The Jungle of Text Encoding

Dealing with textual data on the Internet is like navigating a jungle. 

Without some normalisation, you need to get adept at handling multiple encodings.

System.Text is your partner here.

This holds the Encoding class, which has various useful properties.

  • Encoding.ASCII 
  • Encoding.Default (default encoding for current .NET implementation)
  • Encoding.Latin1 (Latin 1 character set, ISO-8859-1)
  • Encoding.Unicode (encoding for UTF16 in little endian byte order)
  • Encoding.UTF32 (little endian)
  • Encoding.UTF7 (obsolete)
  • Encoding.UTF8
Recall that Windows is little-endian by default, running primarily on x86 and x86-64 architectures which are little endian.  Even Windows on ARM uses little endian mode (ARM is bi-endian which means it can be used in little or big endian mode).

Thursday, 1 May 2025

Dot net (.NET) MAUI for Dummies

Dot net (.NET) MAUI (Multi-platform App UI) is a cross-platform framework for creating native mobile and desktop applications with C# (and XAML optionally).

The upside is you just have one codebase which can be used to render UI on Windows, Android, iOS, macOS and Samsung Tizen. 

For Windows, WinUI 3 is used as the native platform (this means it will work on Windows 10 version 1809 or later, and Windows 11).

Mermaid has evolved from multifarious UI technologies.



Tuesday, 13 August 2024

gRPC for .NET

The recommended library for using gRPC in .NET is grpc/grpc-dotnet under the Apache 2.0 license (The original implementation is now deprecated). 

gRPC is a (language agnostic) remote procedure call framework designed for high performance.

gRPC is often used with protobuf with coding structured around .proto files explained here.  One of the reasons for high performance is compact binary serialization of these so-called protocol buffers.

Common use cases include gRPC authentication (gRPC can converse with a variety of authentication systems), compression and other distributed communication scenarios. 

It's first commit was made internal to Google in December 2013 and was made public on Github in 2015. Google was doing RPC with protocol buffers for years and years,  but needed to expose APIs for public consumption. In 2017, gRPC was donated to the CNCF.  More complex features were incorporated like Flatbuffer, which is for serialization - where you can use the data without full deserialization (originally used for games). Observability features have also been built into gRPC since its initial release.

gRPC is used by Netflix and Facebook and Google of course.

Sunday, 27 June 2021

ASP.NET Razor - What is it?

ASP.NET Razor is a (server-side) markup language, that allows you to embed server-based code into web pages (Visual Basic and C#).

Razor syntax is similar to PHP and Classic ASP.

The following Razor code outputs a series of list items.

<ul>

@for (int i=0; i<5; i++) {

<li>@i</li>

}

</ul>

The foreach loop is of course also supported in Razor.

Saturday, 8 September 2018

Using C# to Process Open XML Format Documents

To open up and process DOCX format Word documents you need the Open XML SDK, currently on Version 2.0. It is built on System.IO.Packaging. The SDK uses LINQ technology to provide strongly typed object access to the XML content in the Word doc.

Thursday, 3 May 2018

Object.GetHashCode

GetHashCode is a virtual (overridable) function that returns an int (System.Int32). It is used to identify an object in a hash-based collection (Dictionary). A hash code is not a permanent value; it should not be serialized or stored in databases. They should not be sent across app domains or processes. A hash code can be negative.

Tuesday, 24 April 2018

Calling a Base Class Constructor in C#

To call a base class constructor in a derived class in C#, define the constructor in the subclass and use the syntax :base after the signature to invoke the base class constructor.

Naming Conventions for Abstract Classes in C#

Unlike with interfaces which tend to begin with a capital I, abstract classes have no such convention in C#.

(A famous example of prefix I usage "in the field" is the ubiquitous IEnumerable in System.Collections).

Some possible conventions that can be used (though some people oppose any prefix or suffix) -
  • Prefix : Abstract
  • Suffix:  Base
On a side note, there are some facts about abstract classes in C#.

Abstract classes cannot be sealed and they cannot be private, for obvious reasons. Abstract members cannot be labelled virtual as they are implicitly virtual. Also an abstract member cannot be static - perhaps less obvious.

The internal keyword in C#

Internal types are only available in files in the same assembly (they have "assembly scope"). An example, would be an internal interface used to template for some specific classes used in the assembly.

Monday, 26 February 2018

PDFSharp is a .NET library for Processing PDF Files

More details here. Current nuget packages are detailed here.

Sunday, 3 July 2016

.NET Basics - What does void mean in C#? Is it the same meaning as in C++ or Java?

The void keyword means pretty much the same thing in C# as it means in Java or C++.  But there are few things to be aware of.

  • void used as a method return type means that the method does not return a value. (Implementation-wise it is an alias for System.Void in mscorlib - which has no members except what it inherits from System.Object)
  • void is also used to declare a pointer to an unknown type, when used in an unsafe context

Sunday, 22 May 2016

Roll your own MD5 implementation - here's how!

Roll your own MD5 implementation - here's how!

If you wish to implement an MD5 algorithm (commonly used to verify data integrity) in C# the "right way" to go about it is to inherit from class MD5.

public (abstract) class MD5: HashAlgorithm  [From mscorlib and in System.Security.Cryptography]

public (abstract) class HashAlgorithm: IDisposable, ICryptoTransform

Brainchild of Yale Mathematician Ron Rivest!

The MD5 ("message digest") algorithm was designed in 1991 by Ronald Rivest (whose academic journey began with a Bachelors in Mathematics from Yale) as the successor to MD4, although execution speed is slower. It is described in RFC1321. It produces a 128 bit digest or "fingerprint" of the input data. That "fingerprint" can then be used to validate the integrity of downloaded data files.

The Avalanche Effect

A concept that comes up in discussions of MD5 and other related algorithms is the notion of the avalanche effect. This is a scenario where an input bit is allowed to impact two or more output bits. Berlin-born cryptographer Horst Feistel, whose research led to the formation of the Data Encryption Standard, or DES, in the 1970s, and gave his name to the Feistel cipher, coined the term.

The Idea of a One Way Hash Function or Cryptographic Checksum

To understand MD5 as a cryptographic checksum or one-way hash function, we should understand the basics of hash functions.  A hash function converts a variable length input into a fixed length output (the former is known as the "pre-image" and the latter the "hash value"). The application is verification - to validate if a given "pre-image" is "likely" to be the same as the real "pre-image" the user is trying to validate.  The key to a cryptographic hash, or one-way hash, is that it is difficult to create a "pre-image" that hashes to a particular value. In short, it is hard to spoof the true "pre-image".

Thursday, 12 November 2015

What Types of Thread Priority are there in .NET?

ThreadPriority is specified by the ThreadPriority enumeration. It's an "old-school" enum having originated in .NET 1.1. BelowNormal means that the thread can be scheduled after threads with Normal priority. To make it above normal priority, use AboveNormal or Highest. There is also a ThreadPriority.Lowest for anything non-urgent. The algorithm to select which thread runs next is operating system dependent. The thread state must be Running before the OS can schedule it.

Monday, 21 September 2015

Find the Path for Embedded Resources using Reflection

This is a useful technique to find out the path of embedded resources (you need the path to access the resources).

System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceNames()

Sunday, 9 August 2015

What causes "PInvokeStackImbalance was detected"?

You are happily debugging your C# code, perhaps forgetting it is interfacing with C libraries, when suddenly a most perplexing "StackImbalance" exception is detected. This is often due to signature mismatch between the DLL code and how it is being interpreted in the managed world. A good tip is to review the CallingConvention used. Common conventions include Cdecl and StdCall depending on whether the caller or callee cleans the stack.

Tuesday, 18 November 2014

Why is there no const keyword for methods in C# like there is for C++?

The key difficulty is verifiability at compile-time. An implementation using the [Pure] attribute has been discussed meaning the function has no side-effects; or a "getter" can be used which is effectively the same thing.