Questions tagged [c#-to-f#]

This tag topic is about converting C# code to F#, or learning F# from the perspective of someone with C# background.

Filter by
Sorted by
Tagged with
1 vote
1 answer
40 views

Early return from a loop in a task expression (CE)

I'd like to implement a retry utility for TPL-Tasks in F#, for retrying things like HttpClient.GetAsync(...). Specification: it will retry a given task-starting function up to n times by swallowing ...
  • 7,613
-1 votes
0 answers
14 views

How to convert this C# Func to F#

Saw a solution for large Excel files generated in C# that works well but have been struggling to get the Func<List> getList) part converted to F#. public class ElementListProxy : ...
  • 251
0 votes
1 answer
67 views

F# library that subclasses a class from a C# library

I want to convert the C# classes in my console app to F#, one at a time. With stunning help from Co-pilot, I got one class done. But I can’t build with it. So I made a tiny test solution, to ...
  • 297
2 votes
1 answer
44 views

Why doesn't a null expression in C# convert to an option of None in F# but a literal null does?

I'm creating instances of an F# record type from C#. One of the fields is a string option. If I pass an expression that evaluates to null for that field, it becomes Some(null) in F#. If I pass the ...
1 vote
1 answer
59 views

F# - Class member does not keep state

I wrote a wrapper around List. I expect the internal list to keep state but it doesn't. What am I doing wrong? The methods are definitely executed but the internal list is always empty. open System ...
  • 139
-2 votes
1 answer
148 views

Is there a way to run F# code from a C# program?

I use Rider and/or Visual Studio for Mac to run my C# code. I can individually create an F# project if needed. How do I run the F# code within the C# main program: class MainClass { public static ...
1 vote
3 answers
94 views

Iterating through a calendar in F#

for (int ctr = 1; ctr <= gregorianCalendar.GetMonthsInYear(gregorianCalendar.GetYear(startOfYear)); ctr++) { Console.Write(" {0,2}", ctr); Console.WriteLine("{0,12}{1,15:MMMM}...
0 votes
1 answer
38 views

Use the TResult from the Task<TResult> in F#

I'm publishing events to an Azure Event Hub with an F# script. The equivalent C# code is as follows: var connectionString = "<< CONNECTION STRING FOR THE EVENT HUBS NAMESPACE >>";...
  • 3,207
2 votes
1 answer
163 views

How to use while loop in F# Async expression?

How would I go about writing the following in F#? var reader = await someDatabaseCommand.ExecuteReaderAsync(); while (await reader.ReadAsync()) { var foo = reader.GetDouble(1); // ... } I'm very ...
  • 784
0 votes
1 answer
207 views

Need help in conversion of C# code to F# in .NET Core 6

My C# code for file upload Web API works perfectly fine. My F# knowledge is limited so pardon me if anywhere I am wrong. C# code using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using ...
1 vote
1 answer
170 views

F# bind option and a task

I want to to replicate the following C# code: var things = url is null ? null : await GetThings(url); What I got is: task { match urlOption with | None -> return None ...
1 vote
1 answer
184 views

F#: How can I define a computed property in a type?

In C#, I can define a computed property as such: public class MyViewModel { public DateTime StartDate { get; set; } public string StartDateFormatted => StartDate.ToString("yyyy.MM.dd h:...
  • 15k
2 votes
1 answer
91 views

Is there any alternative to C#'s public member initializer for F#?

in C#, public members can be assigned in place of constructor call: options.TokenValidationParameters = new TokenValidationParameters { NameClaimType = "preferred_username" } To achieve the ...
  • 1,368
1 vote
1 answer
248 views

Convert C# HostBuilder pipeline to F# in isolated process Azure function

The following code is from https://learn.microsoft.com/en-us/azure/azure-functions/dotnet-isolated-process-guide using Microsoft.Extensions.DependencyInjection; var host = new HostBuilder() ....
  • 137
0 votes
0 answers
106 views

The value is not a function and cannot be applied F#

I have the following extension method: module AzureTableExtensions = type IServiceCollection with member this.AddAzureTableStorage(options : Action<AzureTableClientOptions>) = ...
0 votes
1 answer
168 views

How do I write a delegate with params in F#?

I have a C# delegate that I need to replicate in F#: public delegate object InvokeDelegate(string method, params object[] parameters) How do I replicate this in F#? I tried: type InvokeDelegate = ...
  • 27.1k
1 vote
2 answers
168 views

execute c# method with multiple parameters from f#

I am new to F#, and I am trying to execute a static C# function that accepts multiple parameters from the F# file/code. I have a single solution which contains both, C# project as well as F# project. ...
  • 197
1 vote
1 answer
103 views

Using reflection and transform C# to F# code

I'm trying to move some C# code to F# and I'm struggling to achive that in a specific method. Lack of how to Seq and Pipeline work properly when using reflection. Here's in C# public static ...
1 vote
1 answer
116 views

A generic construct requires that the type 'IConditionalOperator' have a public default constructor

I would like to create a hierarchy of types that should implement generic unit tests, using an abstract type as a test fixture. The same pattern works well in C#, but I am facing a compiler error: &...
  • 553
1 vote
1 answer
126 views

Passing string list to F# asp.Net api

I actually solved my problem before posting, but I wonder if there are any better solutions? Also if there is somewhere where there is a way to use list as-is? I am writing a simple get endpoint if F#...
1 vote
1 answer
208 views

How do you catch a specific type of exception without passing the results to the caller in F#?

I am trying to capture a specific exception to acquire the underlying message for logging but I don't want the exception passed back to the calling method. I know how to do this in C#, F#? meh. Here ...
  • 1,667
1 vote
1 answer
55 views

In F#, how does one access the TestContext for .runsettings?

I'm writing an integration test for a third party C# library in an F# code base. There are some constraints to this integration test. For regulatory reasons I must test with MsTest The library only ...
  • 3,246
1 vote
1 answer
195 views

How can I map controller endpoints to actions on F#?

I am probably missing something here as I am new to F#, however, I need the following: open Microsoft.AspNetCore.Mvc [<ApiController>] [<Route("[controller]")>] type ...
4 votes
2 answers
577 views

How to merge arrays in f#

Hav not find a method or func to join arrays, and the "@" and "+" operator not works is there any simple func or operator to do this like in js [...array1, element1, elment2] given ...
  • 193
1 vote
0 answers
130 views

F# SQLProvider error FS0039: type "dataContext" does not define a field, constructor or member "test". in demo

demo is here https://fsprojects.github.io/SQLProvider/core/mysql.html type sql = SqlDataProvider< dbVendor, connString, ResolutionPath = resPath, ...
1 vote
3 answers
247 views

Catching inner exception in F# async block

I have an async block, within this block I call an async method from an external C# web service client library. This method call returns a data transfer object, or a custom exception of type ...
  • 105
3 votes
0 answers
111 views

Converting data objects - F#, Fabulous, sql-net-pcl and a web API client library

I'm very new to F# and trying to figure out the best way to convert between similar but different data objects while minimising duplication of code. I'm writing an app using the Fabulous F# MVU ...
  • 105
3 votes
1 answer
84 views

Initialize instance of internal discriminated union from F# to C#

I have a discriminated union type exported via a dll from F# to be used in C#: type Argument = | IntValue | FloatValue | BoolValue of bool In C# I can initialize something like Argument....
3 votes
1 answer
81 views

Unable to cast C# object in F# type

I have the following declaration in F#: type ConstArg = | Bool of bool | CharArray of char[] member Lambda (ConstArg : ConstArg[]) = ... In C#, I have constructed a char[] array of two ...
1 vote
1 answer
442 views

How to create an instance of C# class with a property whose setter is private..from F#

So I am working on a F# project, and need to access some C# classes. In particular, one of the C# class looks like this: class LoginRequest { public string Scope {get; private set;} } Now with C# ...
  • 13
7 votes
2 answers
362 views

F# - Using Concurrent.ConcurrentDictionary.TryRemove with dotnet 5

I'm migrating my F# code from dotnet3.1 to 5 and struggling with following code: let tryRemove key (dict: Concurrent.ConcurrentDictionary<'a, 'b>) = match dict.TryRemove(key) ...
1 vote
1 answer
202 views

F# - Use C# methods with out parameter (within arrays and void return)

I've read the-f-equivalent-of-cs-out but still I can't make it work for my case (the simplest solution/syntax). I have this method in a C# project: //<Project Sdk="Microsoft.NET.Sdk"> /...
  • 2,600
0 votes
1 answer
65 views

Subscribing to c# Events in F# With Partial Applications

I have this working after a few hours of bashing my head. Now I'm trying to understand why. This is an event definition from a c# lib: internal readonly AsyncEvent<Func<SocketMessage, Task>&...
  • 490
2 votes
2 answers
507 views

A function that compare a two lists of string

I am a new at F# and i try to do this task: Make a function compare : string list -> string list -> int that takes two string lists and returns: -1, 0 or 1 Please help. I spend a lot of time, ...
2 votes
1 answer
300 views

Proper use of P/invoke with pointers in F#

I'm trying to convert this c# code to f#: [DllImport("psapi.dll", SetLastError = true)] [return: MarshalAs(UnmanagedType.Bool)] private static extern bool GetPerformanceInfo([Out] out ...
4 votes
0 answers
169 views

How to apply conditional member visibility in F#

.NET languages in general are not meta-programming friendly, but C# at least has #define pragma directly in code which can be used with if statement or ConditionalAttribute. Looking for any ...
3 votes
1 answer
120 views

How do I implement this generic C# interface using F#?

I want to create an object with this interface in F#: namespace JWT { /// <summary> /// Provides JSON Serialize and Deserialize. Allows custom serializers used. /// </summary> ...
  • 32.3k
3 votes
1 answer
116 views

How do you write a SelectMany from a Task in F#

I would like to write a SelectMany monadic bind from a Task in F#. How would I write the following C# code which uses language-ext in F#? Task<int> result = from task in Task.Run<int>(() =...
0 votes
1 answer
32 views

Problem between one of my binded property and my ActivityIndicator

I'm quite new at using F# and I'm trying to rewrite a full C# library in F#.I've have a Xamarin.forms project with a C# library containing all my Views and my goal is to have two others library, one ...
3 votes
2 answers
222 views

How do I create a C# style async member in F#?

In C# you can annotate methods with async like this: class Foo { public async void Bar() { } } This is different to an F# async; I believe that in F# these are called tasks. So, ...
  • 32.3k
0 votes
1 answer
86 views

F# Event in class constructor

I'm currently learning F# by re-doing a simple mobile application I did in C# and Xamarin.forms which has forgoal to connect a user with facebook and get his profile and posts. I almost finish ...
0 votes
2 answers
180 views

Using extension methods in architecture

Is there any problem with performance, memory, security ,etc. in using of extension methods? and I want to use extension methods vastly in my architecture, make a functions layer(in F# that works with ...
2 votes
1 answer
227 views

How to use EF Include method(to load related data) in F#?

How to eager load related data in F# using Include method I tried this one: _ctx.Reports.Include(x => x.Category) and getting the following error: Severity Code Description Project File ...
4 votes
1 answer
316 views

Why F#'s idiomatic dictionary collection (Map<K,V>) needs the type K to implement comparable while C#'s Dictionary<K,V> doesn't?

I know this question is very similar to this one I asked some time ago: Why F#'s default set collection is sorted while C#'s isn't? However, I'd like to confirm if the reason given there ...
  • 16.6k
4 votes
1 answer
196 views

How to use a C# task in an F# Xamarin.Forms app?

I wanted to learn some F# so I decided to create a Xamarin.Forms app with it, but I'm pretty unfamiliar with asynchronous and functional programming in F#. I'm using Xam.Plugins.Media C# library to ...
  • 3,101
1 vote
1 answer
661 views

Implementing C# interfaces with nullable reference types in F#

I'm trying to learn F# by converting an existing .NET Core solution in C# over, one project at a time. I currently have an interface in C# with nullable reference types: public interface IVehicle { ...
  • 14k
0 votes
1 answer
377 views

F# use named tuple as function parameter

I have the following type in F#: type CodeNameError(CodeNameCombination: (Code*Name)[]) = member this.CodeNameCombinations = CodeNameCombination Is there a way to turn the (Code*Name)[] into a named ...
  • 2,854
1 vote
1 answer
148 views

F# 4.5.0.0: Compilation errors: FS1198, FS0661 and FS0001: I'm adapting a interface from C# to F#

My goal is to port FSharp.Core version 4.5.0.0 to .NET 4.0. To achieve my goal I am rewriting parts of Theraot.Core in F#, since FSharp.Core does not "accept" third party libraries: https://github.com/...
  • 45
1 vote
1 answer
113 views

F# multiple one to many relationship

I am trying to learn F# and I came across this question: let's say I have three tables. A,B,C How do i create a type if 1A -> ManyB, then 1B -> ManyC I mean in C# you can create a list of B in A ...
6 votes
1 answer
254 views

Convert F# func to Expression<Func<..,..>>

I have a module with a function with the following signature: module Something = let someFunc func = // ('TType -> 'TField) -> 'TValue ... and inside that function I invoke a ...
  • 1,337

1
2 3 4 5
8