Problem:
You need to create a data type that behaves like a union type in C++. A union type is useful mainly in interop scenarios in which the unmanaged code accepts and/or returns a union type; we suggest that you do not use it in other situations....
Problem:
Your class or structure needs to control how its information is displayed when its ToString method is called. In addition, you need to apply different formats to this information. For example, when creating a new data type, such as a Line c...
Problem:
You need a way of accepting a string containing a textual representation of an object and converting it to an object usable by your application. For example, if you were provided with the string representation of a line (x1, y1)(x2, y2), yo...
Problem:
You need to build several classes that share many common traits. These classes may share common properties, methods, events, delegates, and even indexers; however, the implementation of these may be different for each class. These classes s...
Problem:
You have a data type that will be stored as elements in an array or an ArrayList. You would like to use the Array.Sort and ArrayList.Sort methods to allow custom sorting of your data types in the array. In addition, you may need to use this...
Problem:
You have a data type that will be stored as elements in an array or an ArrayList. You would like to use the Array.BinarySearch and ArrayList.BinarySearch methods to allow for custom searching of your data types in the array.
Solution:
...
Problem:
You need to control the handling of the +=, -=, /=, and *= operators within your data type; unfortunately, these operators cannot be directly overloaded.
Solution:
Overload these operators indirectly by overloading the +, -, /, and * ...
Indirectly Overloading the &&, ||, and ?: Operators
Problem:
You need to control the handling of the &&, ||, and ?: operators within your data type; unfortunately, these operators cannot be directly overloaded.
Solution:
...
Problem:
You have a numeric value or an enumeration that contains bit flags. You need a method to turn on (set the bit to 1) or turn off (set the bit to 0) one or more of these bit flags. In addition, you also want a method to flip one or more bit f...
Problem:
A complex expression in your code is returning incorrect results. For example, if you wanted to find the average area given two circles, you might write the following expression:
double radius1 = 2;
double radius2 = 4;
double aveAre...
Problem:
Many times a Boolean equation quickly becomes large, complex, and even unmanageable. You need a way to manage this complexity while at the same time verifying that your logic works as designed.
Solution:
To fix this situation, try app...
Problem:
You need to convert between any two of the following types: bool, char, sbyte, byte, short, ushort, int, uint, long, ulong, float, double, decimal, DateTime, and string. Different languages sometimes handle specific conversions differently;...
Problem:
You need to determine which operator is best in your situationthe cast (type) operator, the as operator, or the is operator.
Solution:
Use the information provided in the Discussion section to determine which operator is best to use.
...
Problem:
Ordinarily, when you attempt a casting operation, the .NET Common Language Runtime generates an InvalidCastException if the cast fails. Often, though, you cannot guarantee in advance that a cast will succeed, but you also do not want the ov...
Problem:
A method exists that creates an object from one of several types of classes. This object is then returned as a generic object type. Based on the type of object that was initially created in the method, you want to branch to different logic....
Problem:
You need to implement polymorphic functionality on a set of existing classes. These classes already inherit from a base class (other than Object), thus preventing the addition of polymorphic functionality through an abstract or concrete bas...
Problem:
You need to perform a particular action on a set of dissimilar objects contained within an array or collection, preferably without having to know each individual object's type.
Solution:
Use interfaces in a polymorphic manner. The fol...
Problem:
You need a flexible, well-performing callback mechanism that does not make use of a delegate because you need more than one callback method. So the relationship between the caller and the callee is more complex than can easily be represente...
Problem:
Some companies reuse the same duplicated, but slightly modified, application, with each version built especially for a particular client or group of clients. Bug fixes as well as testing, adding, and modifying code in each of these code bas...
Problem:
You need to force a client to use an overloaded constructor, which accepts parameters to fully initialize the object, rather than a default constructor, which may not fully initialize the object. Often a default constructor cannot fully ini...
Problem:
In many cases, a single return value for a method is not enough. You need a way to return more than one item from a method.
Solution:
Use the out keyword on parameters that will act as return parameters. The following method accepts a...
Problem:
You require your applications to accept one or more command-line parameters in a standard format. You need to access and parse the entire command line passed to your application.
Solution:
Use the ParseCmdLine class shown in Example 3...
problem
An existing C# class needs to be usable by a COM object or will need to be usable sometime in the future. You need to make your class work seamlessly with COM.
Solution
Microsoft has made COM interop quite easy. In fact, you really hav...
problem
A field marked as const can be initialized only at compile time. You need to initialize a field to a valid value at runtime, not at compile time. This field must then act as if it were a constant field for the rest of the application's life....
problem
You need to make sure your C# code will interoperate with all other managed languages that are CLS-compliant consumers, such as VB.NET.
Solution
Mark the assembly with the CLSCompliantAttribute:
[assembly: CLSCompliantAttribute(tru...
problem
You need a method of performing a shallow cloning operation, a deep cloning operation, or both on a data type that may also reference other types.
Solution
Shallow copying means that the copied object's fields will reference the same o...
problem
You require a way to always have the Dispose method of an object called when that object's work is done or it goes out of scope.
Solution
Use the using statement:
using System;
using System.IO;
// …
using(FileStream...
problem
You need to release a COM object from managed code without forcing a garbage collection to occur.
Solution
Use the static ReleaseComObject method of the Marshal class:
int newRefCount = System.Runtime.InteropServices.Marshal.Releas...
problem
Your application creates many objects that are expensive to create and/or have a large memory footprintfor instance, objects that are populated with data from a database or a web service upon their creation. These objects are used throughout...
problem
You have an object that allows its state to be changed. However, you do not want these changes to become permanent if other changes to the system cannot be made at the same time. In other words, you want to be able to roll back the changes i...
problem
Your class references unmanaged resources such as some type of handle or manipulates a block of memory or a file via P/Invoke methods or uses a COM object that requires some cleanup method to be called before it is released. You need to make...
problem
You have a project consisting of some very complex code that is a performance bottleneck for the entire application. You have been assigned to increase performance, but you do not know where to start looking.
Solution
A great way to st...
AJAX popular information C# language guides Windows books and cookbooks
.......
С 2009 года мы стали переводить структура сайта на различные языки. Сайт теперь будет содержать книги не только на английском языке, но также и на других европейских языках, в том числе и на Русском языке.