TERM

NONE of the CONTENT IS OWNED BY ME

BlogSpot SEO Tips: Best On-Page SEO Strategies for Bloggers

What if you can get traffic from search engines as WordPress blogs to your BlogSpot blog?Same traffic. Same People. Same conversion rates?But, Unlike WordPress blog users..

How to Create a Free Blog using Blogspot.com with Images

Do you want to create a blog using BlogSpot blogging platform? Not found a great guide on creating a blog on BlogSpot?

How to Set up Custom Domain Name for Blogger Blogs

The internet has become a more open place to everyone. Anyone can share their skills, knowledge and ideas through various mediums such as Videos, podcast and obviously through blogging.

Best Responsive Premium Blogger Templates

No doubt that Blogger is a great blogging platform for both beginners and professionals. Many pro bloggers such as Darren Rowse, Amit Agarwal and Harsh Agrawal have started their blogging journey first on Blogger.

How to Submit Your Website to Search Engines

The fundamental step to boost traffic to your site is the ranking your website on search engine result pages in higher places. To rank your site on the top of search engines,

Sunday, 15 March 2015

ACER ASPIRE DRIVER

Acer Aspire V5-431P Windows 8 Drivers

Acer Aspire V5-431P Chipset Driver
Filename: Chipset_Intel_9.3.0.1021_W8x64_A.zip
Size: 5.6 MB
Download
Acer Aspire V5-431P SATA AHCI Driver
Filename: AHCI_Intel_11.5.4.1001_W8x64_A.zip
Size: 19.8 MB
Download
Acer Aspire V5-431P Intel iAMT Driver
Filename: AMT_Intel_8.1.0.1248_W8x64_A.zip
Size: 54.5 MB
Download
Acer Aspire V5-431P Audio Driver
Filename: Audio_Realtek_6.0.1.6690_W8x64_A.zip
Size: 166.9 MB
Download
Acer Aspire V5-431P Bluetooth Driver
Filename: Bluetooth_Atheros_8.0.0.220_W8x64_A.zip
Size: 226.3 MB
Download
Acer Aspire V5-431P Bluetooth Driver
Filename: Bluetooth_Broadcom_12.0.0.1941_W8x64_A.zip
Size: 166.0 MB
Download
Acer Aspire V5-431P Card Reader Driver
Filename: CardReader_Realtek_6.2.8400.27028_W8x64_A.zip
Size: 17.2 MB
Download
Acer Aspire V5-431P LAN Driver
Filename: Lan_Realtek_8.002.0612.2012_W8x64_A.zip
Size: 5.6 MB
Download
Acer Aspire V5-431P RF Button
Filename: Others_Dritek_2.2.2000.801_W8x64_A.zip
Size: 196.8 KB
Download
Acer Aspire V5-431P Touchpad Driver
Filename: TouchPad_ELANTECH_11.6.10.001_W8x64_A.zip
Size: 180.4 MB
Download
Acer Aspire V5-431P Touchpad Driver
Filename: TouchPad_Synaptics_16.2.10.19_W8x64_A.zip
Size: 128.5 MB
Download
Acer Aspire V5-431P VGA Driver
Filename: VGA_Intel_9.17.10.2849_W8x64_A.zip
Size: 152.7 MB
Download
Acer Aspire V5-431P VGA Driver
Filename: VGA_NVIDIA_v9.18.13.717_W8x64_A.zip
Size: 235.8 MB
Download
Acer Aspire V5-431P Wireless LAN Driver
Filename: Wireless LAN_Atheros_10.0.0.225_W8x64_A.zip
Size: 226.3 MB
Download
Acer Aspire V5-431P Wireless LAN Driver (new)
Filename: Wireless LAN_Atheros_10.0.0.217.v3_W8x64_A.zip
Size: 216.5 MB
Download
Acer Aspire V5-431P Wireless LAN Driver
Filename: Wireless LAN_Broadcom_6.30.59.20_W8x64_A.zip
Size: 34.6 MB
Download
Acer Aspire V5-431P Acer Instant Update Utility Application
Filename: Acer Instant Update_Acer_1.00.3013_W8x64_A.zip
Size: 11.0 MB
Download
Acer Aspire V5-431P Device Fast-Lane Application
Filename: Device Fast-Lane_Acer_1.00.3011_W8x64_A.zip
Size: 3.2 MB
Download
Acer Aspire V5-431P ePower Management Application
Filename: ePower_Acer_7.00.3006_W8x64_A.zip
Size: 18.8 MB
Download
Acer Aspire V5-431P LaunchManager Application
Filename: LaunchManager_Dritek_7.0.6_W8x64_A.zip
Size: 21.3 MB
Download
Acer Aspire V5-431P Live Updater Application
Filename: Live Updater_Acer_2.00.3004_W8x64_A.zip
Size: 2.4 MB
Download

Monday, 9 March 2015


Wednesday, 4 March 2015

Lesson 14: Introduction to Delegates and Events This lesson introduces delegates and events. Our objectives are as follows: Understand What a Delegate Is Understand What an Event Is Implement Delegates Fire Events Delegates During previous lessons, you learned how to implement reference types using language constructs such as classes and interfaces. These reference types allowed you to create instances of objects and use them in special ways to accomplish your software development goals. Classes allow you to create objects that contained members with attributes or behavior. Interfaces allow you to declare a set of attributes and behavior that all objects implementing them would publicly expose. Today, I'm going to introduce a new reference type called a delegate. A delegate is a C# language element that allows you to reference a method. If you were a C or C++ programmer, this would sound familiar because a delegate is basically a function pointer. However, developers who have used other languages are probably wondering, "Why do I need a reference to a method?". The answer boils down to giving you maximum flexibility to implement any functionality you want at runtime. Think about how you use methods right now. You write an algorithm that does its thing by manipulating the values of variables and calling methods directly by name. What if you wanted an algorithm that was very flexible, reusable, and allowed you to implement different functionality as the need arises? Furthermore, let's say that this was an algorithm that supported some type of data structure that you wanted to have sorted, but you also want to enable this data structure to hold different types. If you don't know what the types are, how could you decide an appropriate comparison routine? Perhaps you could implement an if/then/else or switch statement to handle well-known types, but this would still be limiting and require overhead to determine the type. Another alternative would be for all the types to implement an interface that declared a common method your algorithm would call, which is actually a nice solution. However, since this lesson is about delegates, we'll apply a delegate solution, which is quite elegant. You could solve this problem by passing a delegate to your algorithm and letting the contained method, which the delegate refers to, perform the comparison operation. Such an operation is performed in Listing 14-1. Listing 14-1. Declaring and Implementing a Delegate: SimpleDelegate.cs using System; // this is the delegate declaration public delegate int Comparer(object obj1, object obj2); public class Name { public string FirstName = null; public string LastName = null; public Name(string first, string last) { FirstName = first; LastName = last; } // this is the delegate method handler public static int CompareFirstNames(object name1, object name2) { string n1 = ((Name)name1).FirstName; string n2 = ((Name)name2).FirstName; if (String.Compare(n1, n2) > 0) { return 1; } else if (String.Compare(n1, n2) < 0) { return -1; } else { return 0; } } public override string ToString() { return FirstName + " " + LastName; } } class SimpleDelegate { Name[] names = new Name[5]; public SimpleDelegate() { names[0] = new Name("Joe", "Mayo"); names[1] = new Name("John", "Hancock"); names[2] = new Name("Jane", "Doe"); names[3] = new Name("John", "Doe"); names[4] = new Name("Jack", "Smith"); } static void Main(string[] args) { SimpleDelegate sd = new SimpleDelegate(); // this is the delegate instantiation Comparer cmp = new Comparer(Name.CompareFirstNames); Console.WriteLine("\nBefore Sort: \n"); sd.PrintNames(); // observe the delegate argument sd.Sort(cmp); Console.WriteLine("\nAfter Sort: \n"); sd.PrintNames(); } // observe the delegate parameter public void Sort(Comparer compare) { object temp; for (int i=0; i < names.Length; i++) { for (int j=i; j < names.Length; j++) { // using delegate "compare" just like // a normal method if ( compare(names[i], names[j]) > 0 ) { temp = names[i]; names[i] = names[j]; names[j] = (Name)temp; } } } } public void PrintNames() { Console.WriteLine("Names: \n"); foreach (Name name in names) { Console.WriteLine(name.ToString()); } } } The first thing the program in Listing 14-1 does is declare a delegate. Delegate declarations look somewhat like methods, except they have the delegate modifier, are terminated with a semi-colon (;), and have no implementation. Below, is the delegate declaration from Listing 14-1. public delegate int Comparer(object obj1, object obj2); This delegate declaration defines the signature of a delegate handler method that this delegate can refer to. The delegate handler method, for the Comparer delegate, can have any name, but must have a first parameter of type object, a second parameter of type object, and return an int type. The following method from Listing 14-1 shows a delegate handler method that conforms to the signature of the Comparer delegate. public static int CompareFirstNames(object name1, object name2) { ... } Note: The CompareFirstNames method calls String.Compare to compare the FirstName properties of the two Name instances. The String class has many convenience methods, such as Compare, for working with strings. Please don't allow the implementation of this method to interfere with learning how delegates work. What you should concentrate on is that CompareFirstNames is a handler method that a delegate can refer to, regardless of the code inside of that method. To use a delegate, you must create an instance of it. The instance is created, similar to a class instance, with a single parameter identifying the appropriate delegate handler method, as shown below. Comparer cmp = new Comparer(Name.CompareFirstNames); The delegate, cmp, is then used as a parameter to the Sort() method, which uses it just like a normal method. Observe the way the delegate is passed to the Sort() method as a parameter in the code below. sd.Sort(cmp); Using this technique, any delegate handler method may be passed to the Sort() method at run-time. i.e. You could define a method handler named CompareLastNames(), instantiate a new Comparer delegate instance with it, and pass the new delegate to the Sort() method. Events Traditional Console applications operate by waiting for a user to press a key or type a command and press the Enter key. Then they perform some pre-defined operation and either quit or return to the original prompt that they started from. This works, but is inflexible in that everything is hard-wired and follows a rigid path of execution. In stark contrast, modern GUI programs operate on an event-based model. That is, some event in the system occurs and interested modules are notified so they can react appropriately. With Windows Forms, there is not a polling mechanism taking up resources and you don't have to code a loop that sits waiting for input. It is all built into the system with events. A C# event is a class member that is activated whenever the event it was designed for occurs. I like to use the term "fires" when the event is activated. Anyone interested in the event can register and be notified as soon as the event fires. At the time an event fires, registered methods will be invoked. Events and delegates work hand-in-hand to provide a program's functionality. It starts with a class that declares an event. Any class, including the same class that the event is declared in, may register one of its methods for the event. This occurs through a delegate, which specifies the signature of the method that is registered for the event. The delegate may be one of the pre-defined .NET delegates or one you declare yourself. Whichever is appropriate, you assign the delegate to the event, which effectively registers the method that will be called when the event fires. Listing 14-2 shows a couple different ways to implement events. Listing 14-2. Declaring and Implementing Events: Eventdemo.cs using System; using System.Drawing; using System.Windows.Forms; // custom delegate public delegate void Startdelegate(); class Eventdemo : Form { // custom event public event Startdelegate StartEvent; public Eventdemo() { Button clickMe = new Button(); clickMe.Parent = this; clickMe.Text = "Click Me"; clickMe.Location = new Point( (ClientSize.Width - clickMe.Width) /2, (ClientSize.Height - clickMe.Height)/2); // an EventHandler delegate is assigned // to the button's Click event clickMe.Click += new EventHandler(OnClickMeClicked); // our custom "Startdelegate" delegate is assigned // to our custom "StartEvent" event. StartEvent += new Startdelegate(OnStartEvent); // fire our custom event StartEvent(); } // this method is called when the "clickMe" button is pressed public void OnClickMeClicked(object sender, EventArgs ea) { MessageBox.Show("You Clicked My Button!"); } // this method is called when the "StartEvent" Event is fired public void OnStartEvent() { MessageBox.Show("I Just Started!"); } static void Main(string[] args) { Application.Run(new Eventdemo()); } } Note: If you're using Visual Studio or another IDE, remember to add references to System.Drawing.dll and System.Windows.Forms.dll before compiling Listing 14.2 or just add the code to a Windows Forms project. Teaching the operation of Visual Studio or other IDE's is out-of-scope for this tutorial. You may have noticed that Listing 14-2 is a Windows Forms program. Although I haven't covered Windows Forms in this tutorial, you should know enough about C# programming in general that you won't be lost. To help out, I'll give a brief explanation of some of the parts that you may not be familiar with. The Eventdemo class inherits Form, which essentially makes it a Windows Form. This automatically gives you all the functionality of a Windows Form, including Title Bar, Minimize/Maximize/Close buttons, System Menu, and Borders. A lot of power, that inheritance thing, eh? The way a Windows Form's application is started is by calling the Run() method of the static Application object with a reference to the form object as its parameter. This starts up all the underlying Windows plumbing, displays the GUI, and ensures that events are fired as appropriate. Let's look at the custom event first. Below is the event declaration, which is a member of the Eventdemo class. It is declared with the event keyword, a delegate type, and an event name. public event Startdelegate StartEvent; Anyone interested in an event can register by hooking up a delegate for that event. On the next line, we have a delegate of type Startdelegate, which the event was declared to accept, hooked up to the StartEvent event. The += syntax registers a delegate with an event. To unregister with an event, use the -= with the same syntax. StartEvent += new Startdelegate(OnStartEvent); Firing an event looks just like a method call, as shown below: StartEvent(); This was how to implement events from scratch, declaring the event and delegate yourself. However, much of the event programming you'll do will be with pre-defined events and delegates. This leads us to the other event code you see in Listing 14-2, where we hook up an EventHandler delegate to a Button Click event. clickMe.Click += new EventHandler(OnClickMeClicked); The Click event already belongs to the Button class and all we have to do is reference it when registering a delegate. Similarly, the EventHandler delegate already exists in the System namespace of the .NET Frameworks Class Library. All you really need to do is define your callback method (delegate handler method) that is invoked when someone presses the clickMe button. The OnClickMeClicked() method, shown below, conforms to the signature of the EventHandler delegate, which you can look up in the .NET Framework Class Library reference. public void OnClickMeClicked(object sender, EventArgs ea) { MessageBox.Show("You Clicked My Button!"); } Any time the clickMe button is pressed with a mouse, it will fire the Click event, which will invoke the OnClickMeClicked() method. The Button class takes care of firing the Click event and there's nothing more you have to do. Because it is so easy to use pre-defined events and delegates, it would be a good idea to check if some exist already that will do what you need, before creating your own. Summary This completes this lesson, which was an introduction to delegates and events. You learned how to declare and implement delegates, which provide dynamic run-time method invocation services. You also know how to declare events and use them in a couple different scenarios. One way is to declare your own event, delegate, and callback method from scratch. Another way is to use pre-existing events and delegates and only implement the callback method, which will save you time and make coding easier. I invite you to return for Lesson 15: Introduction to Exception Handling. Additional Resources

Lesson 13: Interfaces This lesson teaches C# Interfaces. Our objectives are as follows: Understand the Purpose of Interfaces. Define an Interface. Use an Interface. Implement Interface Inheritance. An interface looks like a class, but has no implementation. The only thing it contains are declarations of events, indexers, methods and/or properties. The reason interfaces only provide declarations is because they are inherited by classes and structs, which must provide an implementation for each interface member declared. So, what are interfaces good for if they don't implement functionality? They're great for putting together plug-n-play like architectures where components can be interchanged at will. Since all interchangeable components implement the same interface, they can be used without any extra programming. The interface forces each component to expose specific public members that will be used in a certain way. Because interfaces must be implemented by derived classes and structs, they define a contract. For instance, if class foo implements the IDisposable interface, it is making a statement that it guarantees it has the Dispose() method, which is the only member of the IDisposable interface. Any code that wishes to use class foo may check to see if class foo implements IDisposable. When the answer is true, then the code knows that it can call foo.Dispose(). Listing 13-1 shows how to define an interface: Listing 13-1. Defining an Interface: MyInterface.cs interface IMyInterface { void MethodToImplement(); } Listing 13-1 defines an interface named IMyInterface. A common naming convention is to prefix all interface names with a capital "I". This interface has a single method named MethodToImplement(). This could have been any type of method declaration with different parameters and return types. I just chose to declare this method with no parameters and a void return type to make the example easy. Notice that this method does not have an implementation (instructions between curly braces - {}), but instead ends with a semi-colon, ";". This is because the interface only specifies the signature of methods that an inheriting class or struct must implement. Listing 13-2 shows how this interface could be used. Listing 13-2. Using an Interface: InterfaceImplementer.cs class InterfaceImplementer : IMyInterface { static void Main() { InterfaceImplementer iImp = new InterfaceImplementer(); iImp.MethodToImplement(); } public void MethodToImplement() { Console.WriteLine("MethodToImplement() called."); } } The InterfaceImplementer class in Listing 13.2 implements the IMyInterface interface. Indicating that a class inherits an interface is the same as inheriting a class. In this case, the following syntax is used: class InterfaceImplementer : IMyInterface Now that this class inherits the IMyInterface interface, it must implement its members. It does this by implementing the MethodToImplement() method. Notice that this method implementation has the exact same signature, parameters and method name, as defined in the IMyInterface interface. Any difference between the method signature in the interface and the method signature in the implementing class or struct will cause a compiler error. Additionally, a class or struct that inherits an interface must include all interface members; You will receive a compiler error if you don't implement all interface members. Interfaces may also inherit other interfaces. Listing 13-3 shows how inherited interfaces are implemented. Listing 13-3. Interface Inheritance: InterfaceInheritance.cs using System; interface IParentInterface { void ParentInterfaceMethod(); } interface IMyInterface : IParentInterface { void MethodToImplement(); } class InterfaceImplementer : IMyInterface { static void Main() { InterfaceImplementer iImp = new InterfaceImplementer(); iImp.MethodToImplement(); iImp.ParentInterfaceMethod(); } public void MethodToImplement() { Console.WriteLine("MethodToImplement() called."); } public void ParentInterfaceMethod() { Console.WriteLine("ParentInterfaceMethod() called."); } } The code in listing 13.3 contains two interfaces: IMyInterface and the interface it inherits, IParentInterface. When one interface inherits another, any implementing class or struct must implement every interface member in the entire inheritance chain. Since the InterfaceImplementer class in Listing 13-3 inherits from IMyInterface, it also inherits IParentInterface. Therefore, the InterfaceImplementer class must implement the MethodToImplement() method specified in the IMyInterface interface and the ParentInterfaceMethod() method specified in the IParentInterface interface. Summary You now understand what interfaces are. You can implement an interface and use it in a class. Interfaces may also be inherited by other interface. Any class or struct that inherits an interface must also implement all members in the entire interface inheritance chain. I invite you to return for Lesson 14: Introduction to Delegates and Events.

Lesson 12: Structs This lesson will teach you about the C# struct. Our objectives are as follows: Understand the Purpose of structs. Implement a struct. Use a struct. What is a struct? A struct is a value type. To help understand the struct, it's helpful to make a comparison with classes, as described in Lesson 7: Introduction to Classes and subsequent chapters. While a struct is a value type, a class is a reference type. Value types hold their value in memory where they are declared, but reference types hold a reference to an object in memory. If you copy a struct, C# creates a new copy of the object and assigns the copy of the object to a separate struct instance. However, if you copy a class, C# creates a new copy of the reference to the object and assigns the copy of the reference to the separate class instance. Structs can't have destructors, but classes can have destructors. Another difference between a struct and class is that a struct can't have implementation inheritance, but a class can, as described in Lesson 8: Class Inheritance. Although a struct can't have implementation inheritance, it can have interface inheritance, as described in Lesson 13: Interfaces, which is the next lesson following this one. Lesson 22: Topics on C# Type, digs deeper into the differences between value and reference types, providing code that demonstrates the concepts that are introduced here. The .NET Framework includes many types that are structs, including many of the built-in types. For example, a System.Int32 is a C# int, a System.Single is a C# float, and a System.Bool is a C# bool. The C# built-in types are aliases for .NET Framework types, giving you language-specific syntax. If you look at the documentation for any of these .NET Framework types, you'll see them declared as struct types. That means you'll need to recognize what a struct type is when you see it, which the next section helps with by showing you how to create your own custom struct type. Creating a Custom struct Type While the behavior of class and struct types are very different, their syntax is similar. You declare the type and its members with the primary visual difference being that a struct uses the keyword struct and a class uses the keyword class. The example in Listing 12-1 demonstrates how to define a custom struct. In this case, the struct is a Rectangle with Width and Height properties, similar to what you might use to represent a rectangular shape on a screen. Listing 12-1. Defining a struct /// /// Custom struct type, representing a rectangular shape /// struct Rectangle { /// /// Backing Store for Width /// private int m_width; /// /// Width of rectangle /// public int Width { get { return m_width; } set { m_width = value; } } /// /// Backing store for Height /// private int m_height; /// /// Height of rectangle /// public int Height { get { return m_height; } set { m_height = value; } } } As you can see, the Rectangle struct in Listing 12-1 looks very much like a class with a couple properties, except that it uses the keyword struct, instead of the keyword class, to declare that Rectangle is a struct. Using a struct To use a struct, instantiate the struct and use it just like a class. Listing 12-2 shows how to instantiate the Rectangle struct and access its properties. Listing 12-2. Using a Struct using System; /// /// Example of declaring and using a struct /// class StructExample { /// /// Entry point: execution starts here /// static void Main() { // instantiate a new Rectangle struct // where Width is set to 1 and Height is set to 3 Rectangle rect1 = new Rectangle(); rect1.Width = 1; rect1.Height = 3; // show the value of Width and Height for rect1 Console.WriteLine("rect1: {0}:{1}", rect1.Width, rect1.Height); Console.ReadKey(); } } The code in the Main method of Listing 12-2 instantiates a new Rectangle struct and sets its Height and Width properties. The experience is similar to how a class can be used. Here's the output: rect1: 1:3 An alternate way of instantiating a struct and setting its properties is with an object initializer, shown below: // you can also use object initialization syntax Rectangle rect11 = new Rectangle { Width = 1, Height = 3 }; Notice that the object initializer uses curly braces and sets properties via a comma-separated list of name/value pairs. Overloading struct Constructors The two previous examples of instantiating a struct, via constructor only and via object initializer, used the default (parameterless) constructor of the struct. The default constructor is implicitly defined by C# and you can't implement the default constructor yourself. The default constructor initializes all struct fields to default values. i.e. integrals are 0, floating points are 0.0, and booleans are false. If you need custom constructor overloads, you can add new constructors, as long as they have one or more parameters. Listing 12-3 shows a customization of the Rectangle struct from Listing 12-1 that includes a constructor overload. Listing 12-3: Overloading a struct Constructor /// /// Custom struct type, representing a rectangular shape /// struct Rectangle { /// /// Backing Store for Width /// private int m_width; /// /// Width of rectangle /// public int Width { get { return m_width; } set { m_width = value; } } /// /// Backing store for Height /// private int m_height; /// /// Height of rectangle /// public int Height { get { return m_height; } set { m_height = value; } } /// /// Instantiate rectangle struct with dimensions /// /// Width to make new rectangle /// Height to make new rectangle public Rectangle(int width, int height) { m_width = width; m_height = height; } } The highlighted portion of code in Listing 12-3 is a constructor overload. Constructors are named the same as their containing struct, which is Rectangle in this case. This Rectangle constructor overload has two parameters, which it assigns to backing stores that are encapsulated by properties for calling code. Listing 12-4 shows an example of how you would use the constructor overload from Listing 12-3 to instantiate a new Rectangle. Listing 12-4: Instantiating a struct Through a Constructor Overload using System; /// /// Example of declaring and using a struct /// class StructExample { /// /// Entry point: execution starts here /// static void Main() { // instantiate a new Rectangle struct // where Width is set to 5 and Height is set to 7 Rectangle rect2 = new Rectangle(5, 7); // show the value of Width and Height for rect2 Console.WriteLine("rect2: {0}:{1}", rect2.Width, rect2.Height); Console.ReadKey(); } } The code in the Main method of Listing 12-4 instantiates a Rectangle struct and displays the values set via the constructor overload. When instantiating rect2, the code passes the values 5 and 7 as arguments. From the constructor in Listing 12-3, you can see that the Width of rect2 will be set to 5 and the Height of rect2 will be set to 7. Here's the output from Listing 12-4: rect2: 5:7 Adding a Method to a struct All of the examples so far showed how you can add properties and constructors to a struct, but you can also add methods to a struct. Defining a method in a struct is the same as defining a method in a class. Listing 12-5 shows the Rectangle struct with a method named Add. Listing 12-5: Adding a Method to a struct /// /// Custom struct type, representing a rectangular shape /// struct Rectangle { /// /// Backing Store for Width /// private int m_width; /// /// Width of rectangle /// public int Width { get { return m_width; } set { m_width = value; } } /// /// Backing store for Height /// private int m_height; /// /// Height of rectangle /// public int Height { get { return m_height; } set { m_height = value; } } /// /// Instantiate rectangle struct with dimensions /// /// Width to make new rectangle /// Height to make new rectangle public Rectangle(int width, int height) { m_width = width; m_height = height; } /// /// Increase the size of this rectangle by the size of the specified rectangle /// /// Rectangle that will be added to this rectangle /// New rectangle created by adding rect to this rectangle public Rectangle Add(Rectangle rect) { // create instance of rectangle struct with default constructor Rectangle newRect = new Rectangle(); // add matching axes and assign to new Rectangle struct newRect.Width = Width + rect.Width; newRect.Height = Height + rect.Height; // return new Rectangle struct return newRect; } } The highlighted code in Listing 12-5 is a method named Add. It might or might not make sense to add two Rectangle structs together, but the example demonstrates how to define a method in a struct. In this case, the Add method will increase the Height and Width of the current Rectangle instance by adding the Height and Width in the rect parameter. The result of the method is a new Rectangle with the added properties. Calling a struct Method You can call the Add method, from Listing 12-5, through an instance of a Rectangle struct. Listing 12-6 shows how to instantiate two Rectangle structs, call the Add method and assign the result of the Add method call to another Rectangle struct. Listing 12-6: Calling a struct Method using System; /// /// Example of declaring and using a struct /// class StructExample { /// /// Entry point: execution starts here /// static void Main() { // instantiate a new Rectangle struct // where Width is set to 1 and Height is set to 3 Rectangle rect1 = new Rectangle(); rect1.Width = 1; rect1.Height = 3; // show the value of Width and Height for rect1 Console.WriteLine("rect1: {0}:{1}", rect1.Width, rect1.Height); // instantiate a new Rectangle struct // where Width is set to 5 and Height is set to 7 Rectangle rect2 = new Rectangle(5, 7); // show the value of Width and Height for rect2 Console.WriteLine("rect2: {0}:{1}", rect2.Width, rect2.Height); // invoke the Add method on the rect1 Rectangle struct instance, // passing the rect2 Rectangle struct instance as an argument // and assigning the new copy of the value returned by the // Add method to the rect3 Rectangle struct. Rectangle rect3 = rect1.Add(rect2); // show the value of Width and Height for rect3 Console.WriteLine("rect3: {0}:{1}", rect3.Width, rect3.Height); Console.ReadKey(); } } In the Main method of Listing 12-6, the code instantiates rect1 and rect2, which are both Rectangle structs, assigning values to their Height and Width properties. The struct instantiation examples should be familiar by now because they are the same as earlier examples. What's useful about Listing 12-6 is the highlighted code, which shows how to invoke the Add method of the Rectangle struct. The code invokes the Add method of the rect1 instance and passes rect2 as the Rectangle struct to be added to rect1. The Add method in Listing 12-5 shows what happens when this code executes. In Listing 12-6, the return value of the Add method is assigned to rect3, which is a larger Rectangle with each of its sides equal to the sum of the individual sides of rect1 and rect2. Here's the output: rect1: 1:3 rect2: 5:7 rect3: 6:10 Summary This lesson described what a struct was and identified a few differences between class and struct types. You learned how to create a struct. You can instantiate a struct either via a default constructor or a custom constructor overload that you write. You also saw how to implement properties and methods in structs. I invite you to return for Lesson 13: Interfaces.

Lesson 11: Indexers This lesson teaches C# Indexers. Our objectives are as follows: Understand What Indexers Are For. Implement an Indexer. Overload Indexers. Understand How to Implement Multi-Parameter Indexers. Indexers allow your class to be used just like an array. On the inside of a class, you manage a collection of values any way you want. These objects could be a finite set of class members, another array, or some complex data structure. Regardless of the internal implementation of the class, its data can be obtained consistently through the use of indexers. Here's an example. Listing 11-1. An Example of An Indexer: IntIndexer.cs using System; /// /// A simple indexer example. /// class IntIndexer { private string[] myData; public IntIndexer(int size) { myData = new string[size]; for (int i=0; i < size; i++) { myData[i] = "empty"; } } public string this[int pos] { get { return myData[pos]; } set { myData[pos] = value; } } static void Main(string[] args) { int size = 10; IntIndexer myInd = new IntIndexer(size); myInd[9] = "Some Value"; myInd[3] = "Another Value"; myInd[5] = "Any Value"; Console.WriteLine("\nIndexer Output\n"); for (int i=0; i < size; i++) { Console.WriteLine("myInd[{0}]: {1}", i, myInd[i]); } } } Listing 11-1 shows how to implement an Indexer. The IntIndexer class has a string array named myData. This is a private array that external users can't see. This array is initialized in the constructor, which accepts an int size parameter, instantiates the myData array, and then fills each element with the word "empty". The next class member is the Indexer, which is identified by the this keyword and square brackets, this[int pos]. It accepts a single position parameter, pos. As you may have already guessed, the implementation of an Indexer is the same as a Property. It has get and set accessors that are used exactly like those in a Property. This indexer returns a string, as indicated by the string return value in the Indexer declaration. The Main() method simply instantiates a new IntIndexer object, adds some values, and prints the results. Here's the output: Indexer Output myInd[0]: empty myInd[1]: empty myInd[2]: empty myInd[3]: Another Value myInd[4]: empty myInd[5]: Any Value myInd[6]: empty myInd[7]: empty myInd[8]: empty myInd[9]: Some Value Using an integer is a common means of accessing arrays in many languages, but the C# Indexer goes beyond this. Indexers can be declared with multiple parameters and each parameter may be a different type. Additional parameters are separated by commas, the same as a method parameter list. Valid parameter types for Indexers include integers, enums, and strings. Additionally, Indexers can be overloaded. In listing 11-2, we modify the previous program to accept overloaded Indexers that accept different types. Listing 11-2. Overloaded Indexers: OvrIndexer.cs using System; /// /// Implements overloaded indexers. /// class OvrIndexer { private string[] myData; private int arrSize; public OvrIndexer(int size) { arrSize = size; myData = new string[size]; for (int i=0; i < size; i++) { myData[i] = "empty"; } } public string this[int pos] { get { return myData[pos]; } set { myData[pos] = value; } } public string this[string data] { get { int count = 0; for (int i=0; i < arrSize; i++) { if (myData[i] == data) { count++; } } return count.ToString(); } set { for (int i=0; i < arrSize; i++) { if (myData[i] == data) { myData[i] = value; } } } } static void Main(string[] args) { int size = 10; OvrIndexer myInd = new OvrIndexer(size); myInd[9] = "Some Value"; myInd[3] = "Another Value"; myInd[5] = "Any Value"; myInd["empty"] = "no value"; Console.WriteLine("\nIndexer Output\n"); for (int i=0; i < size; i++) { Console.WriteLine("myInd[{0}]: {1}", i, myInd[i]); } Console.WriteLine("\nNumber of \"no value\" entries: {0}", myInd["no value"]); } } Listing 11-2 shows how to overload Indexers. The first Indexer, with the int parameter, pos, is the same as in Listing 11-1, but there is a new Indexer that takes a string parameter. The get accessor of the new indexer returns a string representation of the number of items that match the parameter value, data. The set accessor changes each entry in the array that matches the data parameter to the value that is assigned to the Indexer. The behavior of the overloaded Indexer that takes a string parameter is demonstrated in the Main() method of Listing 11-2. It invokes the set accessor, which assigns the value of "no value" to every member of the myInd class that has the value of "empty". It uses the following command: myInd["empty"] = "no value";. After each entry of the myInd class is printed, a final entry is printed to the console, indicating the number of entries with the "no value" string. This happens by invoking the get accessor with the following code: myInd["no value"]. Here's the output: Indexer Output myInd[0]: no value myInd[1]: no value myInd[2]: no value myInd[3]: Another Value myInd[4]: no value myInd[5]: Any Value myInd[6]: no value myInd[7]: no value myInd[8]: no value myInd[9]: Some Value Number of "no value" entries: 7 The reason both Indexers in Listing 11-2 can coexist in the same class is because they have different signatures. An Indexer signature is specified by the number and type of parameters in an Indexers parameter list. The class will be smart enough to figure out which Indexer to invoke, based on the number and type of arguments in the Indexer call. An indexer with multiple parameters would be implemented something like this: public object this[int param1, ..., int paramN] { get { // process and return some class data } set { // process and assign some class data } } Summary You now know what Indexers are for and how they're used. You can create an Indexer to access class members similar to arrays. Overloaded and multi-parameter Indexers were also covered. I invite you to return for Lesson 12: Structs.

Lesson 10: Properties This lesson teaches C# Properties. Our objectives are as follows: Understand What Properties Are For. Implement a Property. Create a Read-Only Property. Create a Write-Only Property. Create an auto-implemented property. Overview of Properties Properties provide the opportunity to protect a field in a class by reading and writing to it through the property. In other languages, this is often accomplished by programs implementing specialized getter and setter methods. C# properties enable this type of protection while also letting you access the property just like it was a field. Another benefit of properties over fields is that you can change their internal implementation over time. With a public field, the underlying data type must always be the same because calling code depends on the field being the same. However, with a property, you can change the implementation. 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. Now, lets see how to use properties. Traditional Encapsulation Without Properties Languages that don't have properties will use methods (functions or procedures) for encapsulation. The idea is to manage the values inside of the object, state, avoiding corruption and misuse by calling code. Listing 10-1 demonstrates how this traditional method works, encapsulating Customer information via accessor methods. Listing 10-1. An Example of Traditional Class Field Access using System; public class Customer { private int m_id = -1; public int GetID() { return m_id; } public void SetID(int id) { m_id = id; } private string m_name = string.Empty; public string GetName() { return m_name; } public void SetName(string name) { m_name = name; } } public class CustomerManagerWithAccessorMethods { public static void Main() { Customer cust = new Customer(); cust.SetID(1); cust.SetName("Amelio Rosales"); Console.WriteLine( "ID: {0}, Name: {1}", cust.GetID(), cust.GetName()); Console.ReadKey(); } } Listing 10-1 shows the traditional method of accessing class fields. The Customer class has four methods, two for each private field that the class encapsulates: m_id and m_name. As you can see, SetID and SetName assign a new values and GetID and GetName return values. Observe how Main calls the SetXxx methods, which sets m_id to 1 and m_name to "Amelio Rosales" in the Customer instance, cust. The call to Console.WriteLine demonstrates how to read m_id and m_name from cust, via GetID and GetName method calls, respectively. This is such a common pattern, that C# has embraced it in the form of a language feature called properties, which you'll see in the next section. Encapsulating Type State with Properties The practice of accessing field data via methods was good because it supported the object-oriented concept of encapsulation. For example, if the type of m_id or m_name changed from an int type to byte, calling code would still work. Now the same thing can be accomplished in a much smoother fashion with properties, as shown in Listing 10-2. Listing 10-2. Accessing Class Fields With Properties using System; public class Customer { private int m_id = -1; public int ID { get { return m_id; } set { m_id = value; } } private string m_name = string.Empty; public string Name { get { return m_name; } set { m_name = value; } } } public class CustomerManagerWithProperties { public static void Main() { Customer cust = new Customer(); cust.ID = 1; cust.Name = "Amelio Rosales"; Console.WriteLine( "ID: {0}, Name: {1}", cust.ID, cust.Name); Console.ReadKey(); } } Listing 10-2 shows how to create and use a property. The Customer class has the ID and Name property implementations. There are also private fields named m_id and m_name; which ID and Name, respectively, encapsulate. Each property has two accessors, get and set. The get accessor returns the value of a field. The set accessor sets the value of a field with the contents of value, which is the value being assigned by calling code. The value shown in the accessor is a C# reserved word. When setting a property, just assign a value to the property as if it were a field. The CustomerManagerWithProperties class uses the ID and Name properties in the Customer class. The first line of Main instantiates a Customer object named cust. Next the value of the m_id and m_name fields of cust are set by using the ID and Name properties. To read from a property, use the property as if it were a field. Console.WriteLine prints the value of the m_id and m_name fields of cust. It does this by calling the ID and Name properties of cust. This was a read/write property, but you can also create read-only properties, which you'll learn about next. Creating Read-Only Properties Properties can be made read-only. This is accomplished by having only a get accessor in the property implementation. Listing 10-3 demonstrates how you can create a read-only property. Listing 10-3. Read-Only Properties 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(); } } The Customer class in Listing 10-3 has two read-only properties, ID and Name. You can tell that each property is read-only because they only have get accessors. At some time, values for the m_id and m_name must be assigned, which is the role of the constructor in this example. The Main method of the ReadOnlyCustomerManager class instantiates a new Customer object named cust. The instantiation of cust uses the constructor of Customer class, which takes int and string type parameters. In this case, the values are 1 and "Amelio Rosales". This initializes the m_id and m_name fields of cust. Since the ID and Name properties of the Customer class are read-only, there is no other way to set the value of the m_id and m_name fields. If you inserted cust.ID = 7 into the listing, the program would not compile, because ID is read-only; the same goes for Name. When the ID and Name properties are used in Console.WriteLine, they work fine. This is because these are read operations which only invoke the get accessor of the ID and Name properties. One question you might have now is "If a property can be read-only, can it also be write-only?" The answer is yes, and explained in the next section. Creating a Write-Only Property You can assign values to, but not read from, a write-only property. A write-only property only has a set accessor. Listing 10-4 shows you how to create and use write-only properties. Listing 10-4. Write-Only Properties using System; public class Customer { private int m_id = -1; public int ID { set { m_id = value; } } private string m_name = string.Empty; public string Name { set { m_name = value; } } public void DisplayCustomerData() { Console.WriteLine("ID: {0}, Name: {1}", m_id, m_name); } } public class WriteOnlyCustomerManager { public static void Main() { Customer cust = new Customer(); cust.ID = 1; cust.Name = "Amelio Rosales"; cust.DisplayCustomerData(); Console.ReadKey(); } } This time, the get accessor is removed from the ID and Name properties of the Customer class, shown in Listing 10-1. The set accessors have been added, assigning value to the backing store fields, m_id and m_name. The Main method of the WriteOnlyCustomerManager class instantiates the Customer class with a default constructor. Then it uses the ID and Name properties of cust to set the m_id and m_name fields of cust to 1 and "Amelio Rosales", respectively. This invokes the set accessor of ID and Name properties from the cust instance. When you have a lot of properties in a class or struct, there can also be a lot of code associated with those properties. In the next section, you'll see how to write properties with less code. Creating Auto-Implemented Properties The patterns you see here, where a property encapsulates a property with get and set accessors, without any other logic is common. It is more code than we should have to write for such a common scenario. That's why C# 3.0 introduced a new syntax for a property, called an auto-implemented property, which allows you to create properties without get and set accessor implementations. Listing 10-5 shows how to add auto-implemented properties to a class. Listing 10-5. Auto-Implemented Properties using System; public class Customer { public int ID { get; set; } public string Name { get; set; } } public class AutoImplementedCustomerManager { static void Main() { Customer cust = new Customer(); cust.ID = 1; cust.Name = "Amelio Rosales"; Console.WriteLine( "ID: {0}, Name: {1}", cust.ID, cust.Name); Console.ReadKey(); } } Notice how the get and set accessors in Listing 10-5 do not have implementations. In an auto-implemented property, the C# compiler creates the backing store field behind the scenes, giving the same logic that exists with traditional properties, but saving you from having to use all of the syntax of the traditional property. As you can see in the Main method, the usage of an auto-implemented property is exactly the same as traditional properties, which you learned about in previous sections. Summary You now know what properties are for and how they're used. Traditional techniques of encapsulation have relied on separate methods. Properties allow you to access objects state with field-like syntax. Properties can be made read-only or write-only. You also learned how to write properties with less code by using auto-implemented properties. I invite you to return for Lesson 11: Indexers.