Tải bản đầy đủ - 0 (trang)
1-20. Define an Automatically Implemented Property

1-20. Define an Automatically Implemented Property

Tải bản đầy đủ - 0trang

CHAPTER 1 ■ APPLICATION DEVELOPMENT



using System;

namespace Apress.VisualCSharpRecipes.Chapter01

{

public class Recipe01_20

{

// Define a static string property.

static string MyStaticProperty

{

get;

set;

}

static int MyStaticIntProperty

{

get;

set;

}

static void Main(string[] args)

{

// Write out the default values.

Console.WriteLine("Default property values");

Console.WriteLine("Default string property value: {0}", MyStaticProperty);

Console.WriteLine("Default int property value: {0}", MyStaticIntProperty);

// Set the property values.

MyStaticProperty = "Hello, World";

MyStaticIntProperty = 32;

// Write out the changed values.

Console.WriteLine("\nProperty values");

Console.WriteLine("String property value: {0}", MyStaticProperty);

Console.WriteLine("Int property value: {0}", MyStaticIntProperty);

Console.WriteLine("\nMain method complete. Press Enter.");

Console.ReadLine();

}

}

}



40



www.it-ebooks.info



CHAPTER 1 ■ APPLICATION DEVELOPMENT



Running the program gives the following result:

Default property values

Default string property value:

Default int property value: 0



Property values

String property value: Hello, World

Int property value: 32



Main method complete. Press Enter.



1-21. Overload an Operator

Problem

You want to be able to use your types with the standard operators (+, -, *, etc.).



Solution

Overload one or more operators by defining static methods with the operator symbol as the method

name and using the operator keyword in the method declaration.



How It Works

To implement operators in your classes, you simply define static methods to overload the operator you

want to use—for example, the following fragment shows the declaration of a method that implements

the addition operator (+) to be used when adding together two instances of the type Word:

public static string operator +(Word w1, Word w2)

Adding and implementing this method to the Word class allows us to define what happens when we

use the addition operator on two instances of the Word type:

string result = word1 + word2;



41



www.it-ebooks.info



CHAPTER 1 ■ APPLICATION DEVELOPMENT



Notice that the result of our addition is a string—you can return any type you choose. You can also

define the behavior for when operators are applied on different types, such as the following, which

declares a method that overrides the operator for when an instance of Word and an int are added

together:

public static Word operator +(Word w, int i)

The following fragment allows us to use the operator like this:

Word newword = word1 + 7;

Note that the order of the arguments is important—the previous fragment defines the behavior for a

Word + int operation, but not int + Word (i.e., the same types, but with their order reversed). We would

need to define another method to support both orderings.

See the code for this recipe for an example of how to use these operators and implementations for

the operator overloads we have referred to. You can override the following operators:

+, -, *, /, %, &, |, ^, <<, >>



The Code

The following example defines the type Word, which has two overridden addition operators: one that

inserts a space in between words when they are added together, and another for adding an int value to a

word.

using

using

using

using

using



System;

System.Collections.Generic;

System.Linq;

System.Text;

System.Dynamic;



namespace Apress.VisualCSharpRecipes.Chapter01

{

public class Word

{

public string Text

{

get;

set;

}

public static string operator +(Word w1, Word w2)

{

return w1.Text + " " + w2.Text;

}

public static Word operator +(Word w, int i)

{



42



www.it-ebooks.info



CHAPTER 1 ■ APPLICATION DEVELOPMENT



return new Word() { Text = w.Text + i.ToString()};

}

public override string ToString()

{

return Text;

}

}

public class Recipe01_21

{

static void Main(string[] args)

{

// Create two word instances.

Word word1 = new Word() { Text = "Hello" };

Word word2 = new Word() { Text = "World" };

// Print out the values.

Console.WriteLine("Word1: {0}", word1);

Console.WriteLine("Word2: {0}", word2);

Console.WriteLine("Added together: {0}", word1 + word2);

Console.WriteLine("Added with int: {0}", word1 + 7);

Console.WriteLine("\nMain method complete. Press Enter.");

Console.ReadLine();

}

}

}

Running the example produces the following results:

Word1: Hello

Word2: World

Added together: Hello World

Added with int: Hello7



Main method complete. Press Enter.



43



www.it-ebooks.info



CHAPTER 1 ■ APPLICATION DEVELOPMENT



1-22. Define a Conversion Operator

Problem

You need to be able to convert from one type to another.



Solution

Implement implicit or explicit conversion operator methods.



How It Works

You can specify how your type is converted to other types and, equally, how other types are converted to

your type, by declaring conversion operators in your class. A conversion operator is a static method that

is named for the type that you wish to convert to and that has the type you wish to convert from. For

example, the following method fragment is a conversion operator from the Word type (taken from the

code for this recipe) that converts an instance of string to an instance of Word:

public static explicit operator Word(string str)

{

return new Word() { Text = str };

}

Defining this member in the Word class allows us to perform conversions such as the following:

Word word = (Word)"Hello";

Note that we have had to explicitly cast the string to Word—this is because our conversion operator

included the explicit keyword. You can enable implicit conversion by using the implicit keyword, such

as this:

public static implicit operator Word(string str)

{

return new Word() { Text = str };

}

With the implicit keyword, now both of the following statements would compile:

Word word = (Word)"Hello";

Word word = "Hello";

Conversion operators must always be static, and you must choose between an explicit and an

implicit conversion—you cannot define different conversion operators for the same pair of types but

with different keywords.



44



www.it-ebooks.info



CHAPTER 1 ■ APPLICATION DEVELOPMENT



The Code

The following example defines and demonstrates implicit and explicit conversion operators for the Word

type.

using System;

namespace Apress.VisualCSharpRecipes.Chapter01

{

public class Word

{

public string Text

{

get;

set;

}

public static explicit operator Word(string str)

{

return new Word() { Text = str };

}

public static implicit operator string(Word w)

{

return w.Text;

}

public static explicit operator int(Word w)

{

return w.Text.Length;

}

public override string ToString()

{

return Text;

}

}

public class Recipe01_22

{

static void Main(string[] args)

{

// Create a word instance.

Word word1 = new Word() { Text = "Hello"};

// Implicitly

string str1 =

// Explicitly

string str2 =



convert the word to a string.

word1;

convert the word to a string.

(string)word1;



45



www.it-ebooks.info



Tài liệu bạn tìm kiếm đã sẵn sàng tải về

1-20. Define an Automatically Implemented Property

Tải bản đầy đủ ngay(0 tr)

×