background preloader

C#

Facebook Twitter

How to mock HttpClient in your .NET / C# unit tests - Gingter Ale. You have a class that depends on HttpClient (or IHttpClientFactory for that matter), and want to unit test that.

How to mock HttpClient in your .NET / C# unit tests - Gingter Ale

Since you want a unit test and not an integration test, it should not call a real http endpoint. You decide to mock out your dependency on HttpClient, and soon thereafter you got stuck. After googling ‘How to mock HttpClient’ you landed on this post. So, how would you mock HttpClient for your unit tests? I give you the short answer right away: You don’t. Why did you get stuck in the first place? Mock HttpClient – and run into problems While trying to replace your HttpClient with a mock of it, you will have noticed the following facts.

First, the HttpClient does not have an interface which you could use for mocking.Secondly, it doesn’t even have an abstract base class you could use.Even worse, the methods that you call on the HttpClient are not virtual, so you don’t have a chance to override them. First (not so good) idea: Wrap the HttpClient. Why is string.GetHashCode() different each time I run my program in .NET Core? This post is part of the second annual C# Advent.

Why is string.GetHashCode() different each time I run my program in .NET Core?

Check out the home page for up to 50 C# blog posts in December 2018! In this post I describe a characteristic about GetHashCode() that was new to me until I was bitten by it recently - that calling GetHashCode() on a string gives a different value each time you run the program in .NET Core! In this post I show the problem in action, and how the .NET Core GetHashCode() implementation differs from .NET Framework. I then look at why that's the case and why it's a good thing in general. Finally, if you need to ensure GetHashCode() gives the same value every time you run your program (spoiler: generally you don't), then I provide an alternative implementation you can use in place of the built-in GetHashCode(). tl;dr; I strongly suggest reading the whole post, but if you're just here for the deterministic GetHashCode(), then see below. Zugriffsmodifizierer – C#-Programmierhandbuch. {.NET » C#} C#: Override, virtual, new, abstract: ich verstehe nicht, was wann zu verwenden ist. @CODEKICKER.

Oop - What is the difference between an abstract function and a virtual function? C# - Why would you use Expression<Func<T>> rather than Func<T>? CallerMemberNameAttribute Class (System.Runtime.CompilerServices) Abstract (C#-Referenz) Der abstract-Modifizierer gibt an, dass dem modifizierten Objekt eine Implementierung fehlt oder dass diese unvollständig ist.

abstract (C#-Referenz)

Der abstract-Modifizierer kann für Klassen, Methoden, Eigenschaften, Indexer und Ereignisse verwendet werden. Verwenden Sie den abstract-Modifizierer in einer Klassendeklaration, um anzugeben, dass die Klasse nur die Basisklasse für eine andere Klasse sein soll. Als abstrakt markierte Member oder Member in einer abstrakten Klasse müssen von Klassen, die von der abstrakten Klasse abgeleitet wurden, implementiert werden.

Beispiel In diesem Beispiel muss die Klasse Square eine Implementierung von Area bereitstellen, da sie von ShapesClass abgeleitet ist: Abstrakte Klassen weisen die folgenden Funktionen auf: Eine abstrakte Klasse darf nicht instanziiert werden. Verwenden Sie den abstract-Modifizierer in einer Methoden- oder Eigenschaftendeklaration, um anzugeben, dass die Methode oder Eigenschaft keine Implementierung enthalten. BaseClass bc = new BaseClass(); // Error. Predicate(T)-Delegat (System) Diese Dokumentation wurde archiviert und wird nicht länger gepflegt. .NET Framework (current version) Veröffentlicht: Oktober 2016 public delegate bool Predicate<in T>( T obj ) Parameter obj Type: T Rückgabewert Typparameter.

Predicate(T)-Delegat (System)

What is the difference between Delegate & Predicate. Action-Delegat (System) Diese Dokumentation wurde archiviert und wird nicht länger gepflegt. .NET Framework (current version) Veröffentlicht: Oktober 2016 public delegate void Action() using System; using System.Windows.Forms; public delegate void ShowValue(); public class Name { private string instanceName; public Name(string name) { this.instanceName = name; } public void DisplayToConsole() { Console.WriteLine(this.instanceName); } public void DisplayToWindow() { MessageBox.Show(this.instanceName); } } public class testTestDelegate { public static void Main() { Name testName = new Name("Koani"); ShowValue showMethod = testName.DisplayToWindow; showMethod(); } }

Action-Delegat (System)

Zugriffsebenen (C#-Referenz) Verwenden Sie die Zugriffsmodifizierer öffentlich, geschützt, intern oder privat, um eine der folgenden deklarierten Zugriffsebenen für Member anzugeben.

Zugriffsebenen (C#-Referenz)

Es ist nur ein Zugriffsmodifizierer für einen Member oder Typ zulässig, außer wenn Sie die protected internal-Kombination verwenden. Zugriffsmodifizierer sind bei Namespaces nicht zulässig. Namespaces haben uneingeschränkten Zugriff. Abhängig vom Kontext einer Memberdeklaration sind nur bestimmte deklarierte Zugriffe zulässig. Wenn in einer Memberdeklaration kein Zugriffsmodifizierer angegeben ist, wird ein Standardzugriff verwendet. Typen der obersten Ebene, die nicht in anderen Typen geschachtelt sind, können nur Zugriff der Art internal oder public haben. Geschachtelte Typen, die Member von anderen Typen sind, können deklarierte Zugriffe haben, wie in der folgenden Tabelle angegeben.

Strings in .NET and C# This article is now hosted on csharpindepth.com I'm gradually moving all my C# articles to the web site for my book, C# in Depth.

Strings in .NET and C#

This article has already moved. I'm leaving the content here so that existing links still work, but I won't be updating this page.