.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Forum >> OOPS >> Post New QuestionBookmark and Share Subscribe to Forum

propeties in c#

Posted By: boopathi     Posted Date: March 28, 2011    Points:5   Category :OOPS
hi,
i am new to c# can any one help me on functionality and use of property i am confusing so much in this............

regards,
boopathi.K

Responses
Author: Mahesh             
Posted Date: March 28, 2011     Points: 20   

Hi,
This sample shows a Person class that has two properties: Name (string) and Age (int). Both properties are read/write.

using System;
class Person
{
private string myName ="N/A";
private int myAge = 0;

// Declare a Name property of type string:
public string Name
{
get
{
return myName;
}
set
{
myName = value;
}
}

// Declare an Age property of type int:
public int Age
{
get
{
return myAge;
}
set
{
myAge = value;
}
}

public override string ToString()
{
return "Name = " + Name + ", Age = " + Age;
}

public static void Main()
{
Console.WriteLine("Simple Properties");

// Create a new Person object:
Person person = new Person();

// Print out the name and the age associated with the person:
Console.WriteLine("Person details - {0}", person);

// Set some values on the person object:
person.Name = "Joe";
person.Age = 99;
Console.WriteLine("Person details - {0}", person);

// Increment the Age property:
person.Age += 1;
Console.WriteLine("Person details - {0}", person);
}
}


for details follow link,
http://msdn.microsoft.com/en-us/library/aa288470(v=vs.71).aspx

Thanks & Regards
Mahesh

Author: Maverick             
Posted Date: March 29, 2011     Points: 20   

public string PropertyName {
get
{
return _PropertyName ;
}
set
{
_PropertyName = value;
}
}

Thanks,
Maverick

Thanks,
Maverick
Author: Pankaj Chahar             
Posted Date: November 26, 2011     Points: 20   

Hi,

I suggest you to go through the OOPS Concepts like

1--Encapsulation
2--Abstraction
3--Interfaces
4--Inheritance


You will get some idea on c sharp programming.

Pankaj Chahar

Deserve More...


Author: Baimey Rajesh             
Posted Date: August 01, 2012     Points: 20   

There are fields and properties.

Fields are variables.
Unlike fields, properties do not denote storage locations. Instead, properties have accessors that specify the statements to be executed when their values are read or written.

OOPS have a concept of encapsulation. Properties provide the opportunity to protect a field in a class by reading and writing to it through the property. benefit of properties over fields is that you can change their internal implementation over time.

For example, if a customer has an ID that is originally stored as an int, you might have a requirements change that made you perform a validation to ensure that calling code could never set the ID to a negative value. If it was a field, you would never be able to do this, but a property allows you to make such a change without breaking code.

Please do see if the concept is clear.

Thanks
Baimey



Post Reply

You must Sign In To post reply
 
 
Find more Forum Questions on C#, ASP.Net, Vb.Net, SQL Server and more Here
Quick Links For Forum Categories:
ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
Sql Server  SharePoint  OOPs  SilverlightIISJQuery
JavaScript/VBScriptBiztalkWPFPatten/PracticesWCFOthers
www.DotNetSpark.comUnAnsweredAll

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend