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 6194695
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T03:20:22+00:00 2026-05-24T03:20:22+00:00

Given a simple piece of code which can return the name of a property

  • 0

Given a simple piece of code which can return the name of a property in VB.NET:

Function NameForProperty(Of T)(ByVal field As Expression(Of Action(Of T))) As String

    Dim expression = DirectCast(field.Body, MemberExpression)

    Return expression.Member.Name

End Function

Which works like this:

NameForProperty(Of String)(Function (s) s.Length) ' ==> returns "Length"

And what I thought would have been the equivalent in C#:

string NameForProperty<T>(Expression<Action<T>> field)
{
    var expression = (MemberExpression)field.Body;

    return expression.Member.Name;
}

When I try to call the C# version:

NameForProperty<string>(s=>s.Length);

It returns a compiler error:

Only assignment, call, increment, decrement, and new object
expressions can be used as a statement

My question is: what is the difference between the two pieces of code?

EDIT

Ivan has provided an answer as to why the code does not work in C#. I am still curious as to why it does work in VB.NET.

EDIT#2

To be clear, I’m not looking for code which works — simply why the code would work in VB.NET and not C#.

  • 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-24T03:20:23+00:00Added an answer on May 24, 2026 at 3:20 am
    public static class TypeMember<TClass>
    {
        public static string PropertyName<TProp>(Expression<Func<TClass, TProp>> expression)
        {
            var body = expression.Body as MemberExpression;
    
            if (body == null)
                throw new ArgumentException("'expression' should be a property expression");
    
            return body.Member.Name;
        }
    }
    
    var propName = TypeMember<string>.PropertyName(s => s.Length);
    

    The problem with your version lies in the fact that you’re trying to use Action<T>. It returns nothing and thus lambda’s body (s.Length) should be a statement. And it is not a statement actually. So compiler complains about it.

    It would complain in the same way if you’d wrote

    public void A()
    {
        string s = "abc";
        s.Length;
    }
    

    It is the same thing.

    I’m not an expert in VB.NET though, so I can’t explain why it is working in VB, sorry.

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

Sidebar

Related Questions

This is a simple piece of code which i wrote to check whether it
I have a very simple piece of code below which I think gives the
I wrote this simple test code, by adapting a piece from a book, to
Given the code : SQLiteCommand cmd = new SQLiteCommand(UPDATE \Category\ SET \name\=?name, \description\=?description, \color\=?color,
I have a simple piece of code: object[] result = this.getData(baseLogin, args); if (result.Count()
Given a simple statement, such as: <statement id=SelectProducts resultMap=???> SELECT * FROM Products </statement>
Given a simple (id, description) table t1, such as id description -- ----------- 1
Given a simple switch statement switch (int) { case 1 : { printf(1\n); break;
This is a purely theoretical question. Given three simple classes: class Base { }
Given a relatively simple CSS: div { width: 150px; } <div> 12333-2333-233-23339392-332332323 </div> How

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.