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 the difference between user control an custom control? Advantages/disadvantages?

0 Answers  


Please explain what inheritance is, and why it's important?

0 Answers  


Readonly vs. const?

2 Answers   Hewitt,


what is managed data and managed code?

2 Answers  


What is an assembly? What are the different types of assemblies?

0 Answers  






What application do you use to install a Windows service?

1 Answers  


What is connected and diconnected database ?

0 Answers  


If you are executing these statements in commandobject. Select * from table1; select * from table2? How you will deal result set? 42. How do you sort a dataset.

0 Answers  


What does cli do?

0 Answers  


How do you create threading in .NET? What is the namespace for that?

2 Answers  


1 mor thing guyz....can i go for C# directly without C++ /JAVA knowledge for dot net ..???

3 Answers  


Explain when should you use .net web forms over asp.net mvc?

0 Answers  


Categories