Results 1 to 6 of 6

Thread: Read only Members of C sharp

  1. #1
    Join Date
    Dec 2009
    Posts
    32

    Read only Members of C sharp

    Hello friends,

    I am studying C sharp programming language. I know that C sharp is based on .Net framework. But there is one concept in C sharp is quit difficult to understand and which is read only members. I don't have idea about the use of read only members of C sharp. I wonder if you are able to provide me some details over read only members of C sharp. I would greatly appreciate you help over this.

  2. #2
    Join Date
    Apr 2008
    Posts
    1,948

    Re: Read only Members of C sharp

    The constructor is a good place to initialize a read only variable. read only variables need not to be initialized when declared. It is optional for read only members to be initialized when they are declared and read only members can be initialized only once in a class. The const variable must be initialized when it is declared. So it's a syntax error for a const members value to change. Variables that must not be modified must use the const or read only keywords.It is assumed that a const member is static. So, a const member has class wide scope.

  3. #3
    Join Date
    May 2008
    Posts
    2,012

    Re: Read only Members of C sharp

    The read only modifier of C sharp is basically used for the declaration of the constant data member in a class. The read only modifier extends the const modifier by allowing the data member to be initialized in any class constructor. It is optional for read only members to be initialized when they are declared. If the const is not initialized along with the declaration, the compiler will generate an error. It provides flexibility than using the const modifier. The const modifier implicitly defines the data member as static, and must be initialized along with its declaration.

  4. #4
    Join Date
    Apr 2008
    Posts
    2,005

    Re: Read only Members of C sharp

    The read only Members of C sharp used to make an specific data member. In C sharp read only can be used to set dynamic value. Read only members can be initialized with an expression at run time. In C sharp A read only modifier specifies that the member can have its value set once only, This doesn’t mean that a read only data member cannot be static, simply it must explicitly be declared.

  5. #5
    Join Date
    May 2008
    Posts
    2,297

    Re: Read only Members of C sharp

    Hello friends,

    Please refer following C sharp example which shows how to declare read only members of C sharp.
    using System;
    class Circle1
    {
    private const double pi1=3.14;
    public readonly int radius1; //declaration of a Readonly Member

    public Circle(int r1)
    {
    radius = r1;

    }
    public double Area1()
    {
    return pi1 * radius1 * radius1;
    }
    }

    class Test1
    {
    public static void Main(String [] args)
    {
    Circle c1 = new Circle1(10);
    Console.WriteLine(c1.Area1());
    }
    }

  6. #6
    Join Date
    Oct 2005
    Posts
    2,393

    Re: Read only Members of C sharp

    Read only member of C sharp is initialized within a member of class and which is not a constructor. This read only members doesn't need to be initialized when declared. Read only members of C sharp language can be initialized at compile time.It is optional for read only members to be initialized when they are declared and read only members can be initialized only once in a class. I hope from this information you will get some idea about Read only Members of C sharp.

Similar Threads

  1. Listing members of Group with >1500 members
    By Umesh Thakur in forum Windows Server Help
    Replies: 11
    Last Post: 03-02-2012, 05:29 AM
  2. Display members of a group with more than 1500 members
    By Simon G in forum Windows Server Help
    Replies: 5
    Last Post: 25-10-2011, 12:35 PM
  3. Sharp 007SH Hybrid An 3D Clamshell Android Phone by Sharp
    By jackalz in forum Web News & Trends
    Replies: 3
    Last Post: 22-05-2011, 07:28 AM
  4. Replies: 3
    Last Post: 14-12-2010, 04:10 AM
  5. How to toggle between read-only and read-write in Word 2007
    By Mae Huckett in forum MS Office Support
    Replies: 1
    Last Post: 13-02-2008, 02:50 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,718,008,713.07174 seconds with 16 queries