How is a property designated as read-only?
Answers were Sorted based on User's Feedback
Answer / om nama shivaya
Properties can be made read-only.Read only property means
the property value can not be changed . values for read
only property can be assigned through object creation.
using System;
public class Customer
{
private int m_id = -1;
private string m_name = string.Empty;
public Customer(int id, string name)
{
m_id = id;
m_name = name;
}
public int ID
{
get
{
return m_id;
}
}
public string Name
{
get
{
return m_name;
}
}
}
public class ReadOnlyCustomerManager
{
public static void Main()
{
Customer cust = new Customer(1, "Amelio Rosales");
Console.WriteLine(
"ID: {0}, Name: {1}",
cust.ID,
cust.Name);
Console.ReadKey();
}
}
Is This Answer Correct ? | 0 Yes | 0 No |
Answer / deep
In VB.NET:
Private mPropertyName as DataType
Public ReadOnly Property PropertyName() As DataType
Get Return mPropertyName
End Get
End Property
In C#
Private DataType mPropertyName;
public returntype PropertyName
{
get{
//property implementation goes here
return mPropertyName;
}
// Do not write the set implementation
}
Is This Answer Correct ? | 0 Yes | 0 No |
Is .net core the future?
What is the concept of inheritance in .net?
How Garbage Collector (GC) Works?
What is Garbage Collection in .Net and what is the Garbage collection process?
what is managed data and managed code?
How is .net core cross platform?
How will you make .NET programs work in Linux ?
How do you view the methods and members of a dll?
What is a variable of implicit type and what is its scope?
What?s a Windows process?
With these events, why wouldn't microsoft combine invalidate and paint, so that you wouldn't have to tell it to repaint, and then to force it to repaint?
Readonly vs. const?