concept property in category c#

This is an excerpt from Manning's book C# in Depth, Fourth Edition.
LINQ also brought other benefits for initialization in the form of object initializers and collection initializers. These allow you to specify a set of properties to set on a new object or items to add to a new collection within a single expression. It’s simpler to show than describe, and I’ll borrow an example from chapter 3. Consider code that you might previously have written like this:
I always write immutable structs, so the lack of read-only automatically implemented properties was always a pain point.
Some properties are trivial: if the implementation in terms of fields matches the logical state of the type, the property can return the field value directly. That’s what automatically implemented properties are for. Other properties involve computations based on other fields or properties. To demonstrate the problem that C# 6 addresses, the following listing adds another property to our Point class: DistanceFromOrigin, which uses the Pythagorean theorem in a simple way to return how far the point is from the origin.
Don’t worry about the details of initialization and so on; just think about what you’d want out of the three types. Obviously, a date will have properties for the year, month, and day, and a time will have hours, minutes, seconds, and so on. What about the type combining the two? It’s handy to be able to get the date and time components separately, but often you want the subcomponents of the date and time. Each implementation of LocalDate and LocalTime is carefully optimized, and I wouldn’t want to duplicate that logic in LocalDateTime, so the subcomponent properties are pass-throughs that delegate to properties of the date or time components. The implementation shown in the following listing is now extremely clean.
Listing 8.14. Delegating properties in Noda Time
public struct LocalDateTime { public LocalDate Date { get; } #1 public int Year => Date.Year; #2 public int Month => Date.Month; #2 public int Day => Date.Day; #2 public LocalTime TimeOfDay { get; } #3 public int Hour => TimeOfDay.Hour; #4 public int Minute => TimeOfDay.Minute; #4 public int Second => TimeOfDay.Second; #4 #5 }A lot of properties are like this; removing the { get { return ... } } part from each of them was a real pleasure and leaves the code much clearer.

This is an excerpt from Manning's book C# in Depth.
Enter C#. In some ways, C# 1 could have been seen as a nicer version of the Java language, circa 2001. The similarities were all too clear, but C# had a few extras: properties as a first-class feature in the language, delegates and events, foreach loops, using statements, explicit method overriding, operator overloading, and custom value types, to name a few. Obviously, language preference is a personal issue, but C# 1 definitely felt like a step up from Java when I first started using it.
Listing 1.3. Automatically implemented properties and simpler initialization (C# 3)
using System.Collections.Generic; class Product { public string Name { get; private set; } public decimal Price { get; private set; } public Product(string name, decimal price) { Name = name; Price = price; } Product() {} public static List<Product> GetSampleProducts() { return new List<Product> { new Product { Name="West Side Story", Price = 9.99m }, new Product { Name="Assassins", Price=14.99m }, new Product { Name="Frogs", Price=13.99m }, new Product { Name="Sweeney Todd", Price=10.99m} }; } public override string ToString() { return string.Format("{0}: {1}", Name, Price); } }Now the properties don’t have any code (or visible variables!) associated with them, and you’re building the hardcoded list in a very different way. With no name and price variables to access, you’re forced to use the properties everywhere in the class, improving consistency. You now have a private parameterless constructor for the sake of the new property-based initialization. (This constructor is called for each item before the properties are set.)

This is an excerpt from Manning's book ASP.NET 4.0 in Practice.
The first time you request a page, ASP.NET renders its content and generates the correct markup. Let’s suppose we have a page with a Button; this Button will be the control that causes a PostBack when it’s clicked. A second request for the page is caused by the PostBack and is executed differently by ASP.NET. Every control on the page has its state restored; as the developer, you don’t need to explicitly set the properties for every control.
The first option is the default and inherits the container setting, but the last two are more interesting. When the value is set to Disabled, the control stops using the ViewState, but its children can override this behavior and explicitly set it to Enabled. By doing so, the nested controls can have their own state, even if the container has it disabled. Because the property is defined by Control, the page itself has this ability. You can define this property on the page and enable it only when it’s used. Enabling the property only when you need it is a much better practice than having it on all the time.
If you opt for the first option, you need to define the properties in web.config, using the syntax contained in the following listing.
If you take a look at Visual Studio, the result at this point is quite interesting because IntelliSense shows the property, mapped with its defined type. You can take a look at figure 13.3 to understand what we’re talking about.

This is an excerpt from Manning's book DSLs in Boo: Domain-Specific Languages in .NET.
Automatic variable declaration and type inference[1]— The compiler takes care of things for you, so you don’t have to type the same thing over and over again. Some would say that this is bad, due to the lack of explicit typing, but it’s worth trying. In my experience, it works. Take a look at listing 2.1. 1 C# 3.0 has introduced the var keyword, which allows the C# compiler to do much the same for local variables. Boo supports this for just about everything (fields, properties, methods, local variables, and so on).
1 C# 3.0 has introduced the var keyword, which allows the C# compiler to do much the same for local variables. Boo supports this for just about everything (fields, properties, methods, local variables, and so on).

This is an excerpt from Manning's book Real-World Functional Programming with Examples in F# and C#.
Evaluation of expressions— In the functional style, the program code is an expression that specifies properties of the object we want to get as the result. We don’t specify the steps necessary to construct the object and we can’t accidentally use the object before it’s created. For example, we say that we want to get a coffee with two packets of sugars. We can’t drink it before the sugar is added, because when we get the cup, it already contains the sugar.[1] 1 The analogy with coffee making was also used by Luca Bolognese in his great talk about F# at TechEd in 2009 [Bolognese, 2009]. This coincidence suggests that by learning functional programming you’ll learn to think differently not only about programming problems but also about afternoon breaks.
This is similar to anonymous types in C# 3.0, where the compiler also creates a hidden class behind the scenes that we can’t access directly. In C#, we know what the properties of the class are, but this information about properties is available only locally inside the method. On the other hand, in F# we know which interface the class implements, so we can work with it without any such limitations. We can write methods that return well-known interface types and implement them using object expressions.
This means that in F#, encapsulation of data types with their operations is typically one of the last steps of the development process. This can be done using members, which can be added to any F# type and behave just like C# methods or properties. Listing 9.2 shows how to augment the Rect type with two operations using members.

This is an excerpt from Manning's book LINQ in Action.
In addition to associating entity classes with tables, we need to denote each field or property we intend to associate with a column of the table. This is done with the Column attribute:
[Table(Name="Contacts")] class Contact { [Column(IsPrimaryKey=true)] public int ContactID { get; set; } [Column(Name="ContactName"] public string Name { get; set; } [Column] public string City { get; set; } }The Column attribute is also part of the System.Data.Linq.Mapping namespace. It has a variety of properties we can use to customize the exact mapping between our fields or properties and the database’s columns. You can see that we use the IsPrimaryKey property to tell LINQ to SQL that the table column named ContactID is part of the table’s primary key. Notice how we indicate that the ContactName column is to be mapped to the Name field. We don’t specify the names of the other columns or the types of the columns: In our case, LINQ to SQL will deduce them from the fields of the class.
Like the child axis property, the descendants axis property returns an IEnumerable(Of XElement). If you need to access a particular item within the list of returned XElement objects, you can use the extension indexer or the value extension properties, which we’ll look at next.

This is an excerpt from Manning's book ASP.NET AJAX in Action.
We’ll explore the nuts and bolts of client components in chapter 8. For now, it’s enough to treat a client component as a black box that encapsulates reusable client logic and exposes it through methods, properties, and events, as shown in figure 2.2. You’ve already met your first component: the Application object introduced in the previous section. In the following section, we’ll explain how the Application object and client components interact during the client-page lifecycle.
The Microsoft Ajax Library provides a similar technique on the client side, letting you add comments to JavaScript classes, properties, and methods. In a way similar to what happens with server-side classes, XML comments enable the use of the IntelliSense tool even in custom JavaScript files, with custom JavaScript objects. Figure 13.3 shows how the new IntelliSense works in a JavaScript file, in the new version of Visual Studio (codename Orcas).
Each descriptor describes a particular parameter by specifying a list of requirements that it must meet. These requirements are represented by properties of the object. For example, look at the first parameter descriptor supplied:
{name:"a", type:Number, mayBeNull:false, optional:false}The name property contains a string with the parameter name. If the validation fails, you can access the name of the parameter through the Error object. The second property, type, puts a constraint on the type of the parameter. In this case, you mandate that the a parameter is of type Number. The third property, mayBeNull, determines whether you can pass null as the value of the argument. If the property is set to false, you can’t pass null as the value for the a parameter. The last property, optional, specifies whether the parameter is mandatory. If you set the property to false, the caller must supply at least one parameter, because you’re dealing with the first parameter descriptor.
The properties illustrated in the previous example aren’t the only ones available. Table 13.1 lists the properties you can declare in a parameter descriptor.
Table 13.1. Properties used in parameter descriptors
Property
Description
type The expected type of a parameter. The possible values are String, Number, Array, Function, and Object. mayBeNull If true, the parameter can be null. optional If true, the parameter can be omitted. integer If the parameter is a Number, specify if it must be an integer. elementType If a parameter is an Array, specify the expected type for its elements. The possible values are the same as for the type property. elementMayBeNull If a parameter is an Array, specify if it can contain null elements. elementInteger If a parameter is an Array, specify if its elements are integers. domElement If true, the parameter must be a DOM element.
If you read chapter 3, you should be aware that you can expose properties in JavaScript objects. Properties are nothing more than methods that act as the getter and setter for a particular value stored in an object. If you look at some of the listings in this book, you’ll notice that declaring a property is expensive in terms of lines of code written. This is true especially when you use properties to expose the value of private members without performing any additional logic. This situation is shown in listing 13.7, which reports the code for a property called someProperty, declared in the prototype of a client class called someClass.
The first two methods, _createGetter and _createSetter, are supposed to be private methods used by the createProperty helper. Their purpose is to return a closure with the code for the property’s getter and setter. Closures are functions that can be bound to the local variables of the parent functions in which they’re declared, as we explained in section 3.1.3.
You declare a property with the createProperty method. It calls the _createGetter and _createSetter methods and assigns the returned functions to the prototype of the client class from which you call createProperty. Note that it’s not necessary to declare a private field in the constructor; the helper creates the field by adding an underscore character to the property name. Finally, the getter and setter methods are added as properties of the prototype object using the naming convention for properties introduced in section 3.3.3.
With the new createProperty method, adding properties to a class is easy. Listing 13.9 demonstrates by declaring a class called Samples.Customer that exposes three properties: fullName, address, and city.
Listing 13.9. Creating client properties with the createProperty method
Type.registerNamespace("Samples"); Samples.Customer = function() { } Samples.Customer.createProperty("fullName"); Samples.Customer.createProperty("address"); Samples.Customer.createProperty("city"); Samples.Customer.registerClass("Samples.Customer");Properties are declared after the Samples.Customer constructor by calling the createProperty method on the constructor. The createHelper method accepts a string with the name of the property as an argument. As we mentioned previously, an interesting side effect of createProperty is that private fields are implicitly created by the _createSetter function when the following statement is executed:
this[fieldName] = value;In this case, trying to access the fieldName variable on the current instance causes the field to be created if it doesn’t exist. By convention, private fields are created by prefixing the name of the property with the underscore character.

This is an excerpt from Manning's book ASP.Net 2.0 Web Parts in Action: Building Dynamic Web Portals.
When the project has been created, we want to ensure that the assembly will be created with the correct namespace and with a desirable assembly name. You can view these project settings by right-clicking on the project node in the solution explorer and choosing Properties. Notice that the assembly and namespace are set to AW.Portal.Data. This means that building the project will create an assembly named AW.Portal.Data.dll that can be referenced from other projects.
This chapter is the first step in a journey through the properties, methods, and operations of web parts. In it we’ll learn how to add custom verbs to web parts and how to customize their look and feel through themes and other customization techniques. We’ll also delve a little deeper into the subject to see how web parts implement various interfaces that expose their behavior to the rest of the ASP.NET web portal framework. By the end of this chapter we’ll be well on our way to understanding how the http://Start.com website achieves its magic. Let’s begin!
With the page still in debug mode, right-click on the wp variable and choose the “Quick Watch” menu option from the resulting context menu. In response, the Quick Watch dialog is displayed for the wp variable allowing us to see the values for all of its properties. Figure 2.5 displays the Quick Watch dialog with many of the properties for the wp variable shown.
In the Quick Watch dialog we can see that the GenericWebPart has a large number of properties that do not belong to the Label class but, instead are members of the WebPart class. These properties include: “IsClosed,” “Title,” “CatalogIconImageUrl,” and “ChromeState” to name just a few. We see that there is also a property named “WebBrowsableObject” and that it is currently displaying a value of {Text = “I’m a label”}—the very same text value that we assigned to the original label in the mark-up for the page! This is quite a significant discovery, because it tells us that the very same Label control that we added to our WebPartZone earlier has been replaced with a new Type of control and has been wrapped by the WebBrowsableObject property. We can actually still get at the underlying Label control via the ChildControl property of the GenericWebPart—as demonstrated by the code in listing 2.1. Why would we want to get at the underlying control of the generic web part? One reason might be that we are using a control such as a GridView as the child control and we need to access the GridView after a page postback to re-bind it to a data source control.
The first web part that we’ll build is a custom server control that inherits directly from the WebPart class. This will allow us to see for the first time what’s involved in getting a web part up and running using that method. The web part that we will build will be a weather web part to display the weather for a variable number of days. It will have a property that allows us to specify how many days of weather to display. This is a good example of the sort of informational web part that would appear on a typical portal. Figure 2.6 displays what the weather web part control will look like when displayed in a browser.
To allow users to set the number of days of weather to be displayed, we’ll add a property named NumberOfDays. By default we’ll set it to a value of 4 so that at least that number of days of weather will be displayed—even before the user has had time to configure it to be some other value. We can store the value for this property in ViewState so that it is persisted even after multiple trips between the browser and the web server (postbacks). The last bit of logic to be added ensures that the user cannot accidentally set the number of days to a value greater than 10 or less than 1. The code for our property is shown in listing 2.2, with all of its logic in place.
Listing 2.2. The NumberOfDays property contains the number of days of weathers appear in the web part.
public int NumberOfDays { get { if (ViewState["NumberOfDays"] == null) { return 4; } else { return (int)ViewState["NumberOfDays"]; } } set { if (value < 1 || value > 10) { ViewState["NumberOfDays"] = 4; } else { ViewState["NumberOfDays"] = value; } } }