Csharp
65 TopicsTaming Mutable State: Applying Functional Programming in an Object-Oriented Language
🔥 .NET July at Microsoft Hero is on fire! 🚀 The last two sessions have blown us away with incredible speakers and fresh content, but the party isn’t even close to over. July is bursting with .NET energy, and next up, Rodney will join us to take us down a path less traveled with a topic that promises to shake up the way you think about C#. 🧠✨ What’s coming up? Imagine blending the strengths of object-oriented C# with some of the most intriguing secrets from the world of functional programming. This session teases the mysterious forces behind writing more resilient, maintainable apps, without giving it all away. Expect big “aha!” moments and insights you won’t see coming. 🕵️‍♂️💡 Curious? You should be! Make sure you’re registered, mark your calendar, and get ready to join us live for another game-changing session. Let’s unlock new perspectives together, the Microsoft Learn way! 🌟🤝 📅 July 19, 2025 06:00 PM CEST 🔗 https://streamyard.com/watch/CDGBWtmDTtjQ?wt.mc_id=MVP_350258111Views3likes0CommentsSimple Guide On Effective Use Of Parallel Programming For C# In A Managed Code Environment:
Parallel programming in C# uses a variation of Managed Threading, that relies on PLINQ, Expression Trees, The Barrier Class, Thread Class, BackgroundWorker Class, SpinWait Struct, SpinLock Struct, and Thread-Tracking Mode for SpinLock. The reason is that it's faster to use the environment to determine how many cores / threads are available, as to not starve the device of resources, which at that point, you have to use the BackgroundWorker Class to manage each task you've created. Often in this scenario, you can end up with a deadlock condition, because you have more than one task trying to access a shared resource. It's much easier and faster to split up each task, ONLY using byte arrays, assigning each task individual byte arrays to sort / parse, encrypted or not, with the Stream class, or a sub variation of that Class, and then when each task finishes, they all finish at different times, but the shared resource is divided up into portions, so that they will only be able to fill one area of that array. The reason why Barrier is used in this situation, is it forces each one to wait until all the tasks are finished, or until they all ARRIVE at the same place, which solves one timing issue, yet it might create another where there's a bit of a timing mismatch if your estimates are wrong, you overshoot or undershoot I mean. At the very end, you can use a separate process to just COPY from each individual array, without a deadlock scenario occurring. The only issue, is you have to estimate how many threads are available ahead of time, and you can't use every single thread, yet you're going to have to divide a single resource between all those threads. Beforehand, you have to verify if it's a waste of time to use more than one thread. The reason why I say this, is that a lot of people use reference types, not knowing they are immutable, and they take a huge / massive performance hit because of this. Often you have to convert strings into byte arrays, or use a pre-initialized character array at the start of the program, which contains the Unicode values that you want to recast individually as strings, and then use a byte array as an INDEX or a placeholder, of a Unicode character array. If you’re not encrypting the byte arrays, or using them for text parsing, which byte arrays tend to be best in high throughput scenarios, than integer arrays will suffice. The index can be scrambled based on how you want to represent that one string, though it's smarter to only cast a new reference type when you want to display text on the screen. If you spend too much time manually parsing using built-in libraries, it's REALLY SLOW. A byte array is better to use than an integer array in this sort of situation. You might have to create separate indexes with a byte array representing a set of binary flags, to determine whether each one is a letter, number, symbol, etc, or how you want to classify each one based on the code chart that you're using. You would be better off in that situation to just use right shift / left shift / XOR, etc, to set the flags. Then you have something which is also very fast, and almost equivalent to a Barrel Shifter, given C# does not allow you to use pointers, as it's managed code / a managed environment. All the Boolean Logical Operators with Compound Assignment rely on pre-initialized Cast Expressions of Integer Literals, Bitwise and Shift Operators, combined with Lambda Expressions, and Operator Overloading. The purpose of the Shift Operators is to mask / pad the bits of one byte value with zeroes, so that your Cast Expression Of An Integer Literal, which serves at the mask, always gives you a fixed / deterministic result, when used in conjunction with Boolean Logical Operators, especially if the value is smaller than 8-bits / a single byte, or you're dealing with a larger array has to represent a flag "register" with a size of ( 2 ^ 8 ) 256 bits, which is basically a Double-Word: "Microsoft Learn - Boolean logical operators - AND, OR, NOT, XOR - Compound assignment" -> "https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/operators/boolean-logical-operators#compound-assignment" "Microsoft Learn - Bitwise and shift operators (C# reference)" -> "https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/operators/bitwise-and-shift-operators" "Microsoft Learn - Operator overloading - predefined unary, arithmetic, equality and comparison operators" -> "https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/operators/operator-overloading" "Microsoft Learn - Lambda expressions and anonymous functions" -> "https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/operators/lambda-expressions" "Microsoft Learn - Integral numeric types (C# reference) - Integer literals" -> "https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/builtin-types/integral-numeric-types#integer-literals" "Microsoft Learn - Type-testing operators and cast expressions - is, as, typeof and casts - Cast expression" -> "https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/operators/type-testing-and-cast#cast-expression" "Microsoft Learn - Deserialization risks in use of BinaryFormatter and related types" -> "https://learn.microsoft.com/en-us/dotnet/standard/serialization/binaryformatter-security-guide" "Microsoft Learn - BinaryReader Class" -> "https://learn.microsoft.com/en-us/dotnet/api/system.io.binaryreader?view=netframework-4.0" "Microsoft Learn - Stream Class" -> "https://learn.microsoft.com/en-us/dotnet/api/system.io.stream?view=netframework-4.0" "Microsoft Learn - StreamWriter Class" -> "https://learn.microsoft.com/en-us/dotnet/api/system.io.streamwriter?view=netframework-4.0" "Microsoft Learn - StreamReader Class" -> "https://learn.microsoft.com/en-us/dotnet/api/system.io.streamreader?view=netframework-4.0" "Microsoft Learn - File and Stream I/O" -> "https://learn.microsoft.com/en-us/dotnet/standard/io/" "Microsoft Learn - Pipe Functions" -> "https://learn.microsoft.com/en-us/windows/win32/ipc/pipe-functions" "Microsoft Learn - System.IO.Pipes Namespace" -> "https://learn.microsoft.com/en-us/dotnet/api/system.io.pipes?view=netframework-4.0" "Microsoft Learn - Custom Partitioners for PLINQ and TPL" -> "https://learn.microsoft.com/en-us/dotnet/standard/parallel-programming/custom-partitioners-for-plinq-and-tpl" "Microsoft Learn - Expression Trees" -> "https://learn.microsoft.com/en-us/dotnet/csharp/advanced-topics/expression-trees/" "Microsoft Learn - Build expression trees" -> "https://learn.microsoft.com/en-us/dotnet/csharp/advanced-topics/expression-trees/expression-trees-building" "Microsoft Learn - Translate expression trees" -> "https://learn.microsoft.com/en-us/dotnet/csharp/advanced-topics/expression-trees/expression-trees-translating" "Microsoft Learn - Execute expression trees" -> "https://learn.microsoft.com/en-us/dotnet/csharp/advanced-topics/expression-trees/expression-trees-execution" "Microsoft Learn - System.Windows.Threading Namespace" -> "https://learn.microsoft.com/en-us/dotnet/api/system.windows.threading?view=netframework-4.0" "Microsoft Learn - System.Threading Namespace" -> "https://learn.microsoft.com/en-us/dotnet/api/system.threading?view=netframework-4.0" "Microsoft Learn - System.Threading.Channels Namespace" -> "https://learn.microsoft.com/en-us/dotnet/api/system.threading.channels?view=netcore-3.0" "Microsoft Learn - System.Threading.Tasks Namespace" -> "https://learn.microsoft.com/en-us/dotnet/api/system.threading.tasks?view=netframework-4.0" "Microsoft Learn - Timer Class (System.Timers)" -> "https://learn.microsoft.com/en-us/dotnet/api/system.timers?view=netframework-4.0" "Microsoft Learn - Timer Class (System.Threading)" -> "https://learn.microsoft.com/en-us/dotnet/api/system.threading.timer?view=netframework-4.0" "Microsoft Learn - DispatcherTimer Class (System.Windows.Threading)" -> "https://learn.microsoft.com/en-us/dotnet/api/system.windows.threading.dispatchertimer?view=netframework-4.0" "Microsoft Learn - Dispatcher Class (System.Windows.Threading)" -> "https://learn.microsoft.com/en-us/dotnet/api/system.windows.threading.dispatcher?view=netframework-4.0" "Microsoft Learn - Threads and threading" -> "https://learn.microsoft.com/en-us/dotnet/standard/threading/threads-and-threading" "Microsoft Learn - Using threads and threading" -> "https://learn.microsoft.com/en-us/dotnet/standard/threading/using-threads-and-threading" "Microsoft Learn - Introduction to PLINQ" -> "https://learn.microsoft.com/en-us/dotnet/standard/parallel-programming/introduction-to-plinq" "Microsoft Learn - Task Parallel Library (TPL)" -> "https://learn.microsoft.com/en-us/dotnet/standard/parallel-programming/task-parallel-library-tpl" "Microsoft Learn - Lambda Expressions in PLINQ and TPL" -> "https://learn.microsoft.com/en-us/dotnet/standard/parallel-programming/lambda-expressions-in-plinq-and-tpl" "Microsoft Learn - Multithreading in Windows Forms Controls" -> "https://learn.microsoft.com/en-us/dotnet/desktop/winforms/controls/multithreading-in-windows-forms-controls" "Microsoft Learn - Managed threading best practices" -> "https://learn.microsoft.com/en-us/dotnet/standard/threading/managed-threading-best-practices" "Microsoft Learn - Parallel programming in .NET: A guide to the documentation" -> "https://learn.microsoft.com/en-us/dotnet/standard/parallel-programming/" "Microsoft Learn - Thread Class" -> "https://learn.microsoft.com/en-us/dotnet/api/system.threading.thread" "Microsoft Learn - BackgroundWorker Class" -> "https://learn.microsoft.com/en-us/dotnet/api/system.componentmodel.backgroundworker" "Microsoft Learn - Synchronizing data for multithreading" -> "https://learn.microsoft.com/en-us/dotnet/standard/threading/synchronizing-data-for-multithreading" "Microsoft Learn - Overview of synchronization primitives" -> "https://learn.microsoft.com/en-us/dotnet/standard/threading/overview-of-synchronization-primitives" "Microsoft Learn - Environment.ProcessorCount Property" -> "https://learn.microsoft.com/en-us/dotnet/api/system.environment.processorcount" "Microsoft Learn - Managed threading best practices - Number of Processors" -> "https://learn.microsoft.com/en-us/previous-versions/dotnet/netframework-1.1/1c9txz50(v=vs.71)#number-of-processors" "Microsoft Learn - lock statement - ensure exclusive access to a shared resource" -> "https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/statements/lock" "Microsoft Learn - Reliability Best Practices" -> "https://learn.microsoft.com/en-us/dotnet/framework/performance/reliability-best-practices" "Microsoft Learn - BackgroundWorker Component Overview" -> "https://learn.microsoft.com/en-us/dotnet/desktop/winforms/controls/backgroundworker-component-overview" "Microsoft Learn - How to: Run an Operation in the Background" -> "https://learn.microsoft.com/en-us/dotnet/desktop/winforms/controls/how-to-run-an-operation-in-the-background" "Microsoft Learn - Thread-safe collections" -> "https://learn.microsoft.com/en-us/dotnet/standard/collections/thread-safe/" "Microsoft Learn - Threading objects and features" -> "https://learn.microsoft.com/en-us/dotnet/standard/threading/threading-objects-and-features" "Microsoft Learn - Barrier" -> "https://learn.microsoft.com/en-us/dotnet/standard/threading/barrier" "Microsoft Learn - Barrier Class" -> "https://learn.microsoft.com/en-us/dotnet/api/system.threading.barrier?view=netframework-4.0" "Microsoft Learn - SpinWait" -> "https://learn.microsoft.com/en-us/dotnet/standard/threading/spinwait" "Microsoft Learn - SpinWait Struct" -> "https://learn.microsoft.com/en-us/dotnet/api/system.threading.spinwait?view=netframework-4.0" "Microsoft Learn - SpinLock" -> "https://learn.microsoft.com/en-us/dotnet/standard/threading/spinlock" "Microsoft Learn - SpinLock Struct" -> "https://learn.microsoft.com/en-us/dotnet/api/system.threading.spinlock?view=netframework-4.0" "Microsoft Learn - How to: use SpinLock for low-level synchronization" -> "https://learn.microsoft.com/en-us/dotnet/standard/threading/how-to-use-spinlock-for-low-level-synchronization" "Microsoft Learn - How to: Enable Thread-Tracking Mode in SpinLock" -> "https://learn.microsoft.com/en-us/dotnet/standard/threading/how-to-enable-thread-tracking-mode-in-spinlock" "Microsoft Learn - Chaining tasks using continuation tasks" -> "https://learn.microsoft.com/en-us/dotnet/standard/parallel-programming/chaining-tasks-by-using-continuation-tasks" "Microsoft Learn - Interlocked Class" -> "https://learn.microsoft.com/en-us/dotnet/api/system.threading.interlocked?view=netframework-4.0" "Microsoft Learn - TaskFactory Class" -> "https://learn.microsoft.com/en-us/dotnet/api/system.threading.tasks.taskfactory?view=netframework-4.0"390Views0likes2Comments_EnumDesktopWindows API problem
_EnumDesktopWindows API which uses User32.dll stopped working when the patch having OS build number 26100.3775 was updated on Windows 11. The version which was thus installed for Windows 11 is 24H2. It happen also on windows 2019 server having 24H2 as version number. When it tries to get a windows handle of a FORM, it freezes and doesn't allow the user to do anything. What seems to be the problem & how this can be rectified? Anyone's help will be deeply appreciated. Thanks...41Views0likes0CommentsSpecify locale/language for exception messages to user during runtime.
I am using VB 2022 with .NET 8 & 9. I develop applications that allow the user to specify which language they want the GUI elements to be displayed in while the application is running - regardless of what language the users' MS Windows profile is set to. The reason I do this is because some companies (mine included) require all systems to be set to the U.S. English (EN-US) regional/locale settings. This is very easy for me to achieve, even for expected user errors which I can head off. However, unexpected system error messages are always displayed in the English language because the system is set to the English language. Is there any known way to force unexpected error/exception messages to be displayed in a language other than the system's current language? If I knew every single possible error that could be thrown up, as well as its corresponding error code/number, I could create translations myself. But, unfortunately, I have no way of listing every possible system error that could be thrown up during runtime. It would be nice if Microsoft had a COMPLETE list of these somewhere. But every list I've seen is fragmented and incomplete, and sometimes doesn't include a corresponding error code with the message. Try 'Attempt the following block of code. '/////////////////////////////////////////////// ' E X E C U T E S O M E C O D E H E R E ! '\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ Catch e As System.Exception 'Trap any unexpected system error. MessageBox.Show(e.HResult.ToString & NewLine & e.Message, MyCaption, MessageBoxButtons.OK, MessageBoxIcon.Error) 'Display the error message to the user. End Try Even though the user's system is set to the English language, I want to be able to display the message passed in the 'e.Message' member from the codes sample in whatever language the user has specified their GUI to display in during that application's runtime. Any ideas how to achieve this? BTW... the ability to do this would be an AWESOME feature for Microsoft to add to future .NET releases.109Views0likes0CommentsGraph API getAsync() in C#
If I use graph in a console application I get the information that I request. Such as test = await graphClient.Users.GetAsync(); If I try this in a WebMethod in the code behind of a page or in an .asmx, it never returns with the data. I know this has been mentioned before in many forums, but I did not have issues in V1.0 of the SDK. When I try to use V5+ I can't seem to get the information back. Any ideas on overcoming the issue? Any help would be great.174Views0likes1CommentUsing Visual Studio Notebooks for learning C#
Getting Started Install Notebook Editor Extension: Notebook Editor - Visual Studio Marketplace C# 101 GitHub Repo dotnet/csharp-notebooks: Get started learning C# with C# notebooks powered by .NET Interactive and VS Code. (github.com) Machine Learning and .NET dotnet/csharp-notebooks: Get started learning C# with C# notebooks powered by .NET Interactive and VS Code. (github.com) .NET Interactive Notebooks for C# dotnet/csharp-notebooks: Get started learning C# with C# notebooks powered by .NET Interactive and VS Code. (github.com)15KViews0likes4CommentsC# JSON deserialization returns null
Hi there, so first of, I just started working with JSON deserialization today and just know the bare basics. Basically I am currently developing a bot wich should get data from an API of a minecraft server. So far everything works perfectly except one thing: Since a minecraft uuid can start with a number (like for example "121e2d73913041ffaa49af39d124673f") visual studio automatically creates the uuid part with "_" ("public _121E2d73913041ffaa49af39d124673f _121e2d73913041ffaa49af39d124673f { get; set; }"). Now, when I try to request data from a uuid wich starts with a letter everything works fine. However when I request it from a uuid with a number, it will return null. My guess is, that since the JSON itself does not have "_", it wont get deserialized into the in-code version with "_". -> btw, I am using System.Text.Json, not Newtonsoft.Json but would have no problems changing... I hope. So, my question is, if there is a way to get this to work or maby work arround it? Thanks in advance :D121Views0likes0CommentsEnhancing Console.ReadLine to Accept a Message Parameter
n Python, the input function allows you to display a message and wait for user input in a single line of code. However, in C#, achieving the same functionality requires two lines: one for Console.WriteLine to display the message and another for Console.ReadLine to capture the input. Is it possible to have a ReadLine function in C# that takes a message as a parameter and waits for user input?50Views0likes0CommentsPregunta PdfViewer
Hola a todos Estoy trabajando con PdfViewer de Patagames para visualizar PDFs en un formulario WindowsForm. Tengo dos botones, uno para ajustar al ancho de página y otro para mostrar toda la página, pero no consigo implementar el cĂłdigo. ÂżPodrĂan ayudarme? Gracias de antemano.47Views0likes0Comments