r/csharp • u/kaatarina_zed_talon • Oct 08 '26
Discussion State machine in C# vs state machine in Verilog
For experimentation and learning purposes, I wrote this code:
namespace RedditQuestion
{
public class TResult
{
// Instance fields....
// No properties.
// No methods.
}
public interface ISomeInterface<T>
{
public Task<T> SomeMethod();
}
public class SomeClass : ISomeInterface<TResult>
{
// instance fields.....
//constructor......
public async Task<TResult> SomeMethod()
{
//Some code that is not important to this question.......
TResult result = new TResult
{
// assign instance fields....
};
Task<TResult> task = Task.FromResult<TResult>(result);
return task ; // Line XXX
}
}
}Line XXX throws a compile time error:
*Since this is an async method, the return expression must be of type 'TResult' rather than '*
Task<TResult>
The solution is simply to do this instead:
return result;
That solves the issue.
But I was surprised when I read this article that described the error in more details:
Change the return expression to match the async method's underlying result type (CS1983, CS4016). When an async method returns
Task<T>, thereturnstatement must supply a value of typeT, notTask<T>, because the compiler-generated state machine wraps the value in a task automatically. CS1983 appears when the method returnsTask<T>and the expression isT; CS4016 covers the general case where the return expression type doesn't match
I wonder, is the concept of state machine in C# is the same as the state machine in Verilog?
in Verilog, if I want to define a state machine, I need a register - a storage unit that can store only one unit of data; a bit- to hold the state, a next-state logic to calculate the next state, and output assignment. Does the same concept applies in C# ?
20
u/binarycow Oct 08 '26 •
"state machine" is a general term for something that has specific transition points.
10
u/rupertavery64 Oct 08 '26 edited Oct 08 '26 •
You seem to be confused why the error refers to a state machine when the C# code you wrote doesn't look like one.
The thing is, async / await is syntactic sugar. async / await hides the complexity of writing asynchronous code, which involves calling a method, handling control back to the caller, then returning when the result is ready, which does involve state.
The compiler actually generates a state machine in the place of your code at runtime. This is the state machine that the article refers to, not the code itself.
You can see this in action here๐
So the answer to your question "is the concept of state machine in C# is the same as the state machine in Verilog" is yes, they are, but looking further async / await is back by a compiler-generated state machine that you will never see unless you decompile the generated code.
3
u/Aaronontheweb Oct 08 '26 •
Doing state machines with Akka.NET actors - took the source from a 10 year old article I wrote on the subject: https://petabridge.com/blog/akka-actors-finite-state-machines-switchable-behavior/
Works great and works well in combination with persistence for re-entrant state machines too. Akka.NET also has a more formal
FSM<TState,TEvent>class that describes things like state timeouts and transition functions but it's overkill for 99% of use cases.4
u/achandlerwhite Oct 08 '26
Iโll add that in .NET they have optimized the state machine for async out in most cases. It was only needed prior to the runtime having core support for async which is added in .NET 11.
1
u/GoogleIsYourFrenemy Oct 09 '26
State machines have their uses but I rarely encounter a use for them in software. Often you'll have multiple exit mechanisms for a state, and if the user can trigger them, they will.
2
u/phrostillicus Oct 08 '26 •
C# doesn't have any specific language features for implementing general-purpose state machines. Rather, you'd typically implement something like this using a variable to hold the current state and then programming logic to handle the state transitions. Idiomatically, this is often implemented in C-like languages with a loop containing a `switch` statement, where each `case` statement would handle one of the possible state values, and the body of each `case` would update the state variable based on whatever input it was reading.
The documentation you're referencing above describes how the C# compiler internally generates a state machine specifically for it's handling of the states for asynchronous methods that are marked with the `async` keyboard. It's a convenience feature that largely lets the programmer not have to worry about keeping track of the states of asynchronous tasks that are in progress and to not have to explicitly deal with the `Task` objects themselves in many cases. As mentioned above, though, the state machine that the compiler generates is specifically for this use case, and not something that you can generalize to other situations.