reubencartwright

August 4, 2010

Eighth say C # sharp experience indexer and operator overloading

Filed under: Uncategorized

Eighth speaking indexer and operator overloading

Indexer

Indexer (Indexer) is a C # introduced a new type of class members, which makes an array of objects as can be as easy and intuitive reference. Indexer is very similar to the properties we mentioned above, but the indexer can have the parameter list, and can only function in the instance of the object, not a direct role in the class. The following is a typical index for the design, we are here to ignore the specific implementation.

class MyClass

(

public object this [int index]

(

get

(

/ / Get data

)

set

(

/ / Save data

)

)

)

Indexer did not like the names of properties and methods, keywords, this clearly expresses the characteristics of the object reference to the indexer. And the same property, value keywords in the set there after the block parameter significance. In fact from the compiled IL intermediate language code view, above the indexer is implemented as:

class MyClass

(

public object get_Item (int index)

(

/ / Get data

)

public void set_Item (int index, object value)

(

/ / Save data

)

)

Since our indexer behind is compiled into get_Item (int index) and set_Item (int index, object value) two methods, we can not even longer to achieve in a statement indexer inside a class declaration to achieve these two methods, the compiler the behavior of such error. This method can also be implemented implied by our calls, inheritance, operation, and our own methods to achieve indistinguishable. Proficient in C # language compiler implemented as our underlying understanding of C # the following, the behavior of the index provides a good basis.

And methods, as there are five kinds of devices to access the index level of protection, and four kinds of inheritance behavior modification, as well as external indexer. These acts the same way no difference, not repeat them here. The only difference is that the indexer can not be static (static), which, under the semantics of the object reference is easy to understand. It is noteworthy that in the cover (override) to achieve the indexer, you should use the base [E] to access the parent class's indexer.

And attributes to achieve the same data type of the indexer to get the same time block the return type and set block type in the keyword value.

Indexer parameter list is also noteworthy place. “Index” feature makes the indexer must have at least one parameter in this keyword after the brackets. Indexer parameters can only be passed by value types can not have the ref (reference) and out (output) modified. Parameter data type in C # can be any data type. C # according to different parameters of signatures to conduct multi-state Analysis of the indexer. In brackets and set all parameters and under get quote, and value keyword can only be set as the pass next parameter.

The following is an indexer of the specific application example, we understand that its index for the design and application helpful.

using System;

class BitArray

(

int [] bits;

int length;

public BitArray (int length)

(

if (length <0)

throw new ArgumentException ();

bits = new int [((length - 1)>> 5) + 1];

this.length = length;

)

public int Length

(

get (return length;)

)

public bool this [int index]

(

get

(

if (index <0 | | index> = length)

throw new IndexOutOfRangeException ();

else

return (bits [index>> 5] & 1 < )

set

(

if (index <0 | | index> = length)

throw new IndexOutOfRangeException ();

else if (value)

bits [index>> 5] | = 1

bits [index>> 5] & = ~ (1 < )

)

)

class Test

(

static void Main ()

(

BitArray Bits = new BitArray (10);

for (int i = 0; i <10; i + +)

Bits [i] = (i% 2) == 0;

Console.Write (Bits [i] + “”);

)

)

Compile and run the program can get the following output:

True False True False True False True False True False

The above procedure used by the indexer for the user-friendly interface provides a bool array, while it greatly reduces the cost of storage space program. Indexer is commonly used for container object within the object provides access to a friendly interface - which is why C # will be packaged into the reason for the indexer. In fact, we can see in the indexer. NET Framework class library in a large number of applications.

Operator overloading

Operator is the C # in the instance of the class object is used to define operations between the expression of a member. And the indexer is similar to the method operator is still a logical interface to achieve the abstract, that is compiled into the IL in the intermediate language code, the operator is a method still in the form of call. In the class members within the defined operators called operator overloading. C #, there are three overloaded operators: unary operators, binary operators and conversion operators. Not all operators can be overloaded, three operators have a corresponding overloaded operator can set the table below:

Unary operators + -! ~ + + - True false

Binary operators + - backup bin conf config data eshow_sitemap.html generate.sh log maint sitemap.html svn tmp /% & | ^ < <>>==! => <> = < ; =

Convert implicit conversion operator () and explicit conversion ()

Overloaded operator must be public and static modification, otherwise it will cause compilation errors, which the operator is self-evident logic semantics. Parent overloaded operator will quilt class inheritance, but inheritance is not covered, hidden, abstract and other behavior, can not overload operators for virtual sealed override abstract modified. Operator must pass the parameter value parameter. We look at a concrete example of the following:

using System;

class Complex

(

double r, v; / / r + v i

public Complex (double r, double v)

(

this.r = r;

this.v = v;

)

public static Complex operator + (Complex a, Complex b)

(

return new Complex (a.r + b.r, a.v + b.v);

)

public static Complex operator - (Complex a)

(

return new Complex (-a.r,-a.v);

)

public static Complex operator + + (Complex a)

(

double r = a.r +1;

double v = a.v +1;

return new Complex (r, v);

)

public void Print ()

(

Console.Write (r + “+” + v + “i”);

)

)

class Test

(

public static void Main ()

(

Complex a = new Complex (3,4);

Complex b = new Complex (5,6);

Complex c =- a;

c.Print ();

Complex d = a + b;

d.Print ();

a.Print ();

Complex e = a + +;

a.Print ();

e.Print ();

Complex f = + + a;

a.Print ();

f.Print ();

)

)

Compiler and run get the following output:

-3 +-4i 8 + 10i 3 + 4i 4 + 5i 3 + 4i 5 + 6i 5 + 6i

We are here to achieve a “+” sign binary operator, a “-” No. unary operator (negative value), and a “+ +” unary operator. Note here, we did not pass any changes incoming parameters - This parameter is a reference type variable is particularly important, although the parameters of overloaded operators can only be passed by value. We return value, often require “new” a new variable - in addition to true and false operators. This overload “+ +” and “-” operator is particularly important. In other words we do in a + +, we will discard the original a value, and a replacement is the new new out of value to a! It is noteworthy that e = a + + or f = + + a in the e of the value or f, the fundamental values of our overloaded operator return value is not a little contact! Their values only in the front and rear access to a case of the old values or new values only! Conduct pre-and post easy to understand.

Operator overloading on return value and parameter type has very strict requirements. Unary operators, only one parameter. Operator “+ +” and “-” return value type and parameter types must be declared as the type of operator. Operator “+ -! ~” The argument type must be declared as the type of operator, return value type can be arbitrary. true and false operators must declare the parameter type of the type of the operator, as the return value type must be bool, but there must be matched - that is only one statement is wrong, it will cause compilation errors. Parameters of the different types of operators will lead to overloading of the same name - indeed, this is method overloading performance.

Binary operators have two parameters, and the two must have at least one parameter type declarations of the type of operator. Return value types can be arbitrary. Have three pairs of operators also need to be paired statements appear, they are “==” and “!=”,”>” and “< ",">=” and “< ="??Note that the two parameters of different types, although the same type but in a different order will lead to the same name, operator overloading.

Conversion between different types of operators to provide implicit conversion and explicit conversion, mainly used for method calls, expression and assignment transformation. Conversion operator to its argument type (converted type) and return value types (conversion types) also have strict requirements. Parameter types and return value types can not be the same, and between the two definitions must be at least one and the same type of operator. Conversion operator must be converted to type, or defined in any one of the conversion types inside. System definition can not ever be re-defined conversion operator. Also two types of object or interface type can not be between the two can not have a direct or indirect inheritance relations - these three cases the system has the default conversion. We look at an example:

using System;

public struct Digit

(

byte value;

public Digit (byte value)

(

if (value <0 | | value> 9)

throw new ArgumentException ();

this.value = value;

)

public static implicit operator byte (Digit d)

(

return d.value;

)

public static explicit operator Digit (byte b)

(

return new Digit (b);

)

)

The above example provides the type and byte Digit implicit conversions between types and explicit conversion. Conversion from Digit to byte is implicit conversion, the conversion process will not lose any information and throws an exception. Digit conversion from byte to the explicit conversion, the conversion process may be an exception because of missing information. In fact, this also shows us when to declare an implicit conversion, when the declaration shows that conversion of the design principles. While the same parameter types can not declare implicit conversion and explicit conversion. Implicit conversion and explicit conversion no match with - although the C # recommended.

In fact you can see, for properties, indexers, and operators of these C # interface operation provided to us are the methods of the abstract logic of some form of packaging, it is designed to define the type of users we provide a friendly and easy to use interface - we can achieve through the ways to achieve their function. This is designed to understand, we will properly and correctly use these operations, without leading to abuse and misuse.

????:

flv to MOV converter

ts files

Taiwan's Shipbuilding Industry Still Needs Time And Strong Temperature

ProE parting process

JPA entities of state and API

Expert Printer

Managed and leased operators should pay attention to the problems and suggestions

Fireworks Produced Hidden Color Photo

Report Hobby

matroska FILE

Versed in pop-up WINDOW for ASP.NET

Professional human resources workers 132 Tool - factors into account point method

mkv to avi converter free download

China does not thereby adversely affecting the Timing of 3G licenses as early as 4 years will be hug

My favorite Web Servers

Comments »

The URI to TrackBack this entry is: http://reubencartwrightus.blogsome.com/2010/08/04/eighth-say-c-sharp-experience-indexer-and-operator-overloading/trackback/

No comments yet.

RSS feed for comments on this post.

Leave a comment

Line and paragraph breaks automatic, e-mail address never displayed, HTML allowed: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>



Anti-spam measure: please retype the above text into the box provided.






















Get free blog up and running in minutes with Blogsome
Theme designed by Hermanitos Verdes