Static fields and class methods, Overloading class...

Static fields and methods of the class

When creating a new class object, a data structure is created in the memory with the fields described in the class. However, a class can have fields that are not associated with specific objects, but with the entire class. These fields are declared as static fields with a static modifier. Static fields are available to all methods of the class. Static fields describe the general data of all class objects. For example, the class Person can have a static message field, in which each object can leave a message for other objects in the class.

A class can also have static methods declared with a static modifier. Such methods can not use data from specific class objects, they only process general class data stored in its static fields. For example, in the Person class, there can be a static method that processes data from the static message field. Another common case of using static methods is when the class provides its methods to objects of other classes, such as the Console and Math classes from the FCL library that do not have their own fields.

Static fields and methods are called using the class name:

& lt; class name & gt;. & lt; static field or method & gt;

You can use static class elements at any time, regardless of whether objects of this class are created or not.

Overloading class operations

For class objects, you can describe the order of execution of built-in operations by describing static methods whose name consists of the keyword operator, followed by the sign of the overloaded operation (that is, "operator X", where X is the symbol of the overloaded operation). The parameters of this method are the operands involved in the operation. Unary operations have one parameter, and binary operations have two parameters. In each case, one parameter must be of the same type as the class in which the operation is defined, as shown in the following example:

public static complex operator + (Complex c1.Complex c2) {...}

Not all operations can be overloaded, and for some operations there are restrictions (Table 4.2).

Table 4.2

Ability to overload operations in classes


Description feature

+., ++, true, false

such unary operations can be overloaded

+, -, *, /,%, & amp ;, |, ^, & quot ;,

You can overload these binary operations

==. ! =, & lt ;, & gt ;, & lt; =, & gt; =

the comparison operations can be overloaded, but only in pairs: if the operation = is overloaded, then the operation! = (and vice versa) must also be overloaded; The same is true for the operations & lt; and & gt ;, and & lt; = and & gt; =

& amp ;, ||

conditional logical operations can not be overloaded, but they are computed using & amp; and |, which can be overloaded


the array indexing operation can not be overloaded, but you can describe the class indexer


the cast operation can not be overloaded, but you can describe the new type (explicit and implicit) operations

+ =, - =, * =,/=,% =, & amp; =, | =, A =, =, =

the assignment operation can not be overloaded, but, for example, + = is evaluated using the + operator, which allows for an overload

=,.,?:, ->, new, is, sizeof, typeof

these operations can not be overloaded!

Overloads in the operation class allow you to write expressions using objects of this class that are similar to arithmetic and conditional expressions with commonly used operation signs and preserving operation priorities.

An example of overloading an addition operation: public struct Point {float x, y;

// overloaded operation +

public static Point operator + (Point p1 .Point p2)

{return new Point (p1 .x + p2.x, p1 .y + p2.y);}


thematic pictures

Also We Can Offer!

Other services that we offer

If you don’t see the necessary subject, paper type, or topic in our list of available services and examples, don’t worry! We have a number of other academic disciplines to suit the needs of anyone who visits this website looking for help.

How to ...

We made your life easier with putting together a big number of articles and guidelines on how to plan and write different types of assignments (Essay, Research Paper, Dissertation etc)