How is a property designated as read-only?

Answers were Sorted based on User's Feedback



How is a property designated as read-only?..

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

How is a property designated as read-only?..

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

Post New Answer

More Dot Net General Interview Questions

What is a serviced component?

0 Answers  


What is .net environment?

0 Answers  


What are the differences between an interface and an abstract class in .net?

0 Answers  


How to create properties and methods using controls?

0 Answers   CGI,


What's the .net collection class that allows an element to be accessed using a unique key?

0 Answers  






Can any object be stored in a viewstate in .net?

0 Answers  


What is COM Interoperability in .NET

0 Answers  


State some of the different languages supported by .net?

0 Answers  


What is a strategy pattern? Implement it.

1 Answers  


What makes .net core cross platform?

0 Answers  


What is the new three features of COM+ services, which are not there in COM (MTS)

0 Answers   TCS,


Explain the difference between task and thread in .net?

0 Answers  


Categories