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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T03:54:45+00:00 2026-05-16T03:54:45+00:00

In C#, it is possible to define a readonly getter function by not defining

  • 0

In C#, it is possible to define a readonly getter function by not defining the set function like so:

private int _id;

public int Id
{

   get { return _id; }
   // no setter defined
}

in VB.NET

Private _id as Integer
Public Readonly Property Id() As Integer
    Get
       Return _id
    End Get
End Property

Is it possible to tag such a function as readonly like you can in VB .NET in order to be more verbose?

  • 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-16T03:54:46+00:00Added an answer on May 16, 2026 at 3:54 am

    I don’t know what the ReadOnly gives you in VB. I guess the most explicit you can get is actually less verbose:

    public int Id { get; private set; }
    

    In C#, readonly indicates that a field’s value is set during creation of the object and is unchangeable after the constructor exits. You could achieve that via:

    private readonly int _id; // note field marked as 'readonly'
    
    public int Id
    {
       get { return _id; }
    }
    

    Unfortunately automatic properties (like I show in the first code snippet) are not allowed to be readonly. That is, you must enforce read-only semantics yourself by ensuring that none of your class’s code calls the private setter after the constructor exits. I guess this is different to what you’re referring to by VB’s usage of ReadOnly though.

    EDIT As Thomas points out, having no getter is different from having a private one. However VB’s usage of ReadOnly is different to the C# one, at least when used with properties:

    ' Only code inside class employee can change the value of hireDateValue.
    Private hireDateValue As Date
    ' Any code that can access class employee can read property dateHired.
    Public ReadOnly Property dateHired() As Date
        Get
            Return hireDateValue
        End Get
    End Property
    

    To a C# programmer, the ReadOnly keyword would seem redundant. It is already implied by the fact that no setter exists.

    As far as fields are concerned, C# and VB seem equivalent.

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

Sidebar

Related Questions

Is it possible to define some sort of capitalize() or toLowerCase() function when creating
It's possible to define an alias in C# like this using kvp = System.Collections.Generic.KeyValuePair<string,
Is is possible to define a macro BREF(...): struct bits { int b0:1; int
Is it not possible to define structure inside main() . I tried the following
Possible Duplicate: WPF: XAML property declarations not being set via Setters? I am stuck
Possible Duplicate: Is it possible to define more than one function per file in
Is it possible to define the same function in two files, include them and
Is it possible to define more then one function in AJAX Enabled WCF Service
Possible Duplicate: Define a method that has many (or infinite) arguments I have the
Possible Duplicate: Is it possible to define enumalpha? Is there any equivalent of Java

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.