Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1075841
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T21:19:41+00:00 2026-05-16T21:19:41+00:00

I have this C# DLL: namespace TestCSProject { public class TestClass { public static

  • 0

I have this C# DLL:

namespace TestCSProject
{
    public class TestClass
    {
        public static TestClass Instance = null;

        public int Add(int a, int b)
        {
            if (this == null)
                Console.WriteLine("this is null");
            return a + b;
        }
    }
}

And this F# app which references the DLL:

open TestCSProject
printfn "%d" (TestClass.Instance.Add(10,20))

No-one initiates the Instance static variable. Guess what the output of the F# app is?

this is null
30
Press any key to continue . . .

After a few tests I found out that unless I use this (e.g. to access instance field), I won’t get NullReferenceExpcetion.

Is that an intended behaviour or a gap in F# compilation / CLR?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T21:19:42+00:00Added an answer on May 16, 2026 at 9:19 pm

    I suspect you’ll find it’s using call instead of callvirt if you look at the IL. The C# compiler always uses callvirt, even for non-virtual methods, because it forces a nullity check.

    Is this a bug? Well, not necessarily. It depends on what the F# language specification states about methods calls on null references. It’s perfectly possible that it states that the method will be called (non-virtually) with a null “this” reference, which is exactly what has happened.

    C# happens to specify that this sort of dereferencing will throw a NullReferenceException, but that’s a language choice.

    I suspect the F# approach may be a little faster, due to the lack of nullity checking involved… and don’t forget that null references are less “expected” in F# than in C#… that may explain the different approach taken here. Or it could just be an oversight, of course.

    EDIT: I’m not an expert at reading the F# specification, but section 6.9.6 at least suggests to me that it’s a bug:

    6.9.6 Evaluating Method Applications

    For elaborated applications of methods, the elaborated form of the expression will be
    either expr.M(args) or M(args).

    • The (optional) expr and args are evaluated in left-to-right order and the body of the member is evaluated in an environment with formal parameters that are mapped to corresponding argument values.

    • If expr evaluates to null then NullReferenceException is raised.

    • If the method is a virtual dispatch slot (that is, a method that is declared abstract) then the body of the member is chosen according to the dispatch maps of the value of expr.

    Whether this counts as an elaborated application or not is a little beyond me, I’m afraid… but I hope this has been at least somewhat helpful.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have build a c# class library verification.dll using OpenCVSharp. This references OpenCvSharp.dll in
I have a DLL written in C#. In this DLL there is a class
I have DLL and application that will call some function in this dll. For
I have this c# web app that relies heavily on two dll files that
I have a DLL made in C#, this DLL contains some clases like Creator.
I have a win32 COM dll. I want to register this dll for different
The situation is this: I have a shared (DLL) build of Qt 4.5.1 and
I have a program that simulates mouse click. Code is something like this: [DllImport(user32.dll,
I have EXE and 2 DLL used by this EXE. All binary were compiled
I have a C++ DLL with code like this: LogMessage( Hello world ); try

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.