Wednesday, October 13, 2004
Comparison of floating point numbers
Comparing floating point numbers, it must be first re-performed for the same accuracy, because of its inactivity. When you need to test if a FLOAT value or a double value is infinite or NaN, you cannot use the PositiveInfinity, NegativeInfinity, or Nan fields in the Single or Double Contribution; should be done by IsInfinity. It is recommended that it is best not to use the "==" operator to compare two Boolean values, because it is not careful to play a equal sign, will be fatal. You can use the different or operator "^" will be more secure. There is a "! =" Operator equivalent, so many people call "^" as logic "not equal" operators. Just use the "^" operator to "! =" The result of the "! =" Operation, the same effect as "==" operator can be obtained, such as:
BweareCompatible = byoulikemahler == bilikemahler bweareCompatible =! (BYOULIKEMAHLER ^ BilikeMahler) The local variable is only visible to the {state block} that is declared by it. How big is this statement block, and it is checked .... C # There is no "ELSE statement", the IF statement in the C # is constructed of one IF portion and an optional else section. C # also has no "ELSE IF statement", which is two IF combinations. Conditional operator
Conditions & (&&) with conditions or (||)
BEXPRESSION1 & BEXPRESSION2 // If the bexpression1 is evaluated for false, BEXPRESSION2 will not be evaluated BEXPRESSION1 || BEXPIRESSION2 // If Bexpression1 is evaluated to true, BEXPRESSION2 will not be evaluated to be quirky "conditional expression" (?: ), Unique three mesh operators. First, C # exception handling is used to keywords
TRY is used to check an exception that occurs and helps send any possible exceptions. Catch handles errors in a larger way to control, and you can have multiple CATCH clauses. Finally's code blocks will be executed regardless of whether abnormalities have caused an exception.
Throw is used to trigger an exception, which can trigger predefined exceptions and custom exceptions. Second, C # abnormal processing format
The following is a reference fragment: try {program code block;} catch (exception e) {exception handling code block;} finally {The code block to be executed regardless of whether there is an abnormality, the code block to be executed;} effective debugging skills No wireless net fish SHOULD NEVER Be EXECUTED. "The FOR statement starts with the word" for ", followed by a pair of parentheses, there are three brackets separated by a semicolon. Note that this is where the semicolon is not treated as a statement separator in C #! These three expressions are called "initialization expressions", "cycle conditional expressions", and "step-by-step expressions", respectively, called FOR cycles.
Friday, October 15, 2004
The algorithm "Sieve of E ERATHENCES" is an algorithm for generating the number of encompasses. The number of entries can only be 1 and its own integer. 2 is the number of first prime, and it is the only one of the numbers. CPU time-consuming comparison: Method call> Ride calculation> Adjustable Switch statement in Java, Switch statement can only handle integers. However, the Switch statement in the C # is different, and it can also handle the character variable. Switch (ARGS [0]) {CASE "boss": console.writeline ("Good morning! We are ready for you!"); break; case "employee": console.writeline ("Good morning! You can start working ! "); Break; default: console.writeline (" Good morning! I wish you good luck! "); Break;} Different from Java, the C # Switch statement requires every Case block or at the end of the block A Break statement, or use Goto to another CASE tag within Switch. This is the so-called "not allowed to leave the SwICTH branch funnel", but you can make a Switch branch with multiple Switch labels. Such as:
.... case "*": // Multiple multiplication operators Case "X": // Case "X": // Dresult = DNUM1 * DNUM2 BREAK; .... Write the "Basic Tool" for C # programs:
Declare statement; assignment statement; select statement (if, switch); // they enable the program to selectively change the execution route cycle statement (While, Do, For, Foreach) based on a comparison operation or other Boolean operation. They enable the program to perform the same statement multiple times. Jump statement (Return, Break, Continue, Throw, Goto); // They make the program to run from one place to another place to continue. C # Nothing >>> Shift operator C # supports unsigned variable types such as UINT and ULONG. Therefore, in C #, the right shift operator (ie, ">>") is different for unsigned variable types and symbolic variable types (such as int and long). Right shift UINT and ULONG discard the low and set the empty high position to zero; but for the INT and long type variables, ">>" operator discard the low position, while only when the variable value is positive, ">>" Set the empty high level to zero; if ">>" is a negative number, the high level of the empty out is set to 1. There is no symbolic variable type in Java. Therefore, we use the ">>>" operator to introduce the negative position at right time; otherwise, use the ">>" operator. Enumerator enumerator is an ENUM type (Enumerator, or a counter), it is a collection of related constants. Accurately, the Enum type declares a set of related symbol constants define a type name. For example, you can create an enumerator called FRUIT, use it as a type of variable value, thereby limiting the possible range of variables to the value that appears in the enumerator.
PUBLIC CLAIT DEMO {Apple, Banana, Cherry, Durian} Public Void Process (Fruit Fruit) {Switch (Fruit) {Case Fruit.Apple: ... Break; Case Fruit.Banana: ... Break; Case Fruit.Cherry: ... Break; Case Fruit.durian: ... Break;}}} Sunday, October 17, 2004
C # method calls passing parameters four categories:
The default value parameter (VALUE parameter); // transfer replica reference parameters, keyword "ref"; // transfer reference pointer output parameters, keyword "OUT". / / Method Returns an array parameter (Array Parameter), the keyword "params" reference parameter and output parameters when returning one or more return values: "Reference Parameters" is very similar to "Output Parameters". OUT modifier Ref modifiers have similar places: an address. In fact, the realization of the word "ref" and "out" itself is exactly the same in the intermediate language. However, the REF parameters in C # are still different from the OUT parameters:
The REF parameter must be assigned before entering the method; the OUT parameter must be assigned before leaving the method. Need to remember that the output parameter has a certain difference between the return value of the usual function: Function return value often exists in the stack, pops up when it returns; and the output parameter requires the user to pre-store the storage location, that is, the user needs to declare the variable in advance - of course also Can be initialized.
using System; class Test // Name resolution {static void ResoluteName (string fullname, out string firstname, out string lastname) {string [] strArray = fullname.Split (new char [] { ''}); firstname = strArray [ 0]; LastName = stratay [1];} public static void main () {// string myname = "cornfield lee"; console.writeline ("Enter your name:"); string myname = console.readline (); string MyFirstName, MyLastName; ResoluteName (MyName, out MyFirstName, out MyLastName); Console.WriteLine ( "My first name: {0}, My last name: {1}", MyFirstName, MyLastName);}} array parameter (keyword " Params ")
Using system; class test // Total all integer value {static int sum (params int [] args) {INT S = 0; Foreach (INT N IN ARGS) {s = n;} Return S;} static void main () {Int [] var = new int [] {1, 2, 3, 4, 5}; console.writeline ("the sum:" sum (var)); // 传 传 组 变 CONSOLE.WRITELINE ("The Sum : " SUM (10, 20, 30, 40, 50)); // Biography can implicit into arguments parameters}} Note the following two regulations: array parameters can only be one-dimensional; if there are multiple inputs Parameters, only one input parameter is parameters, and it must be the last one in the parameter table.
Tuesday, October 19, 2004
Data packaging - object
In essence, the object is data. As the programming language is developing, when people began to combine different data together and treat them as a whole thing, "object" is born! (Take a date object is understood) When declaring variables and constants, your program is actually an object, numerical constant and string are objects. However, with your program really implemented the statement, the object is not "born". Date Object: The date is organic as a combination of three discrete digital variables, which greatly simplifies the processing of date data. The New operator will allocate memory for the new instance and initialize all the fields of it into 0; the pile is always initialized 0 (that is, the stack is a value of 0 or null null). Date [] adate = new date [5];
If DATE is a structure: This statement will allocate the memory from the 5 instances of this structure (no need to initialize, the instance is required) from the heap, and the instance is now initialized to 0; if it is a class, This statement will only allocate memory only for this array itself (above the array of components, but also assign structural instances). Each element of this array will be NULL. Because each element of this array is a reference (pointer). Before using this array, you must first use a New constructor (including allocation memory and initialization): adate [3] = new date (); C # array is a reference type, each element in array will be stored in the stack . Such as int [] ai = new int [12]; // use the new keyword to save the array in the Heap assignment, C # has the following reference type: the execution efficiency of the stack is higher than the execution efficiency of the stack, but the resource of the stack is limited. Not suitable for handling large logic complex objects. Therefore, structural processing as a small object treated by the base type, and the class handles a business logic although the initialization of the structure also uses the New operator, but the structural object is still allocated on the stack instead of the pile, if "New" is not used (New " The field will remain unfamed before initializing all fields, and how objects do not use how to select structures or classes to discuss the structure and differences of the structure, how to choose how to use the structure or class:
The stack is limited, for a large number of logical objects, the creation class is to represent a lightweight object such as a bit, rectangle, and color than the creation structure, for example, if an array containing 1000 point objects is declared, Quote Each object assigns additional memory. In this case, the cost of the structure is low. When performing abstract and multi-level object hierarchies, the class is the best choice. When this type is just some data, the best selection is the best-oriented process to the object-oriented three steps: Packaging object simplifies the input parameter list:
Static int dayofyear (int iod) {....} // three integer input parameters static int dayofyear (Date DDATE) {....} // Date is like a predefined Good simple type. The encapsulation is related to the object to the class (or structure), becoming a class (or structure) behavior, further sharing code: distinguish between classes (or structure) methods and field staticity and examples, further simplifying the object Code. Every class of .NET Framewrok and each structure TOSTRING method, the self-defined classes and structures are no exception, because all classes and structures in C # are inherited from the same ancestor --system.Object class. Although the structure does not support inheritance, all structures are inherited directly or indirectly from the System.ValeType class. If the declaration of an instance method contains a Virtual modifier, the method is called a virtual method. If there is no Virtual modifier, the method is called a non-virtual method. The implementation of the non-virtual method does not change: Whether in the instance of declaring its class, the method is called on the instance of the derived class, and the implementation is the same. In contrast, the implementation of a virtual method can be replaced by a derived class. Keywords "Virtual" and "Override" are closely related: If the A class inherits the Class B, you can overwrite the Virtual method of the same name in the Class B in a class. You can also override a method without using the "Override" keyword, but this approach is only the purpose of the new, old method's signature and accessibility.
Wednesday, October 20, 2004
Class field initialization method:
Initialize directly in the statement statement:
Class Date {public int year = 2004; public intimonth = 10; public int iDay = 20; .......} Using constructors:
Class date {public int year, imthh, imth; public date () {// no parameter constructor = 1; im thermy = 1; iDay = 1;} .......} In order to speed up the structure based array The creating speed, the structure does not support the above two initialization structures. The structure of the C # is a major language function. Like the class, the structure can also contain other types. Since the structure is internally value type, the structure is sometimes referred to as a light version of the class. At the same time, the structure does not assume the overhead brought by the reference object, unless the exception is provided. C # structure Conclusion:
The definition of the structure is closed (cannot be used as a base class); the structure is implicitly derived from System.ValeType, and System.ValueType is a super-type (ultimate base class) of all value types. The structure has no default constructor. Preventing the creation of a class instance: a private (private) blank non-parameter constructor, such as Class CLSA {... private CLSA () {} ...} In fact, if we can deeply grasp the structure of the class The only purpose is to ensure that the members in the class can get the correct initialization, we have the understanding of various C # medium-shaped structures - it has no reason! }} ┣┣; `. ┣─┒` .. Wǒ ǒ ┟ ┟ ┟ ┍╄┓┟ ┍╄┓┟│ ╃ ╃ 人 ┝ ┝ ┣╈┤┣ ┣╈┤┣;; / ╈ ┗ ┘┗┸┛└ ┘┗┸┛└ ┛ / ┻ ┻┻: When you create a class or structure When you have to think more about the concept of equality. To be a qualified programmer, not only do you have to know how the class object and structural objects are different, assigning operations, method calls, etc., but also understand the process of new operators on class objects and structural objects. What is different. To create an instance of a class, you must use a new operator, the New operator will perform several very important processing:
Allocate memory from the pile for new objects; some constructor of that class is selected. Create an instance of a structure, also uses a New operator, the New operator will call the unique parameterless constructor of the call structure (cannot be rewritten), and the non-parameter constructor is just initializing all the fields 0 or NULL. Don't do anything else. The field is not allowed to be related to the speed of zero and the above structure. Reference Types VERSUS Value Types
The following is a reference fragment: // reference type ') Class Ref {public int x, y, cx, cy;} // value type (Because of' struct ') struct val {public int x, y, CX, cy;} static void homemethod {ref r1 = new ref (); // allocated in Heap Val v1; // allocated on stack (new optional) R1.x = 10; // Pointer Dereference v1.x = 10; // CHANGED ON Stack RectRef R2 = R1; // Copies Pointer Only RectVal V2 = V1; // Allocate On Stack & Copies Members R1.x = 20; // Changes R1 And R2 V1.x = 20; // Changes V1 , NOT V2}
Sunday, October 24, 2004
The domain access restrictions reflect the principle of encapsulation of object-oriented programming. The readonly domain cannot be written, the constant constant (const) cannot be modified, what is the difference between these two?
The read-only domain can only assign a value during initialization-declaration initialization or constructor, and other places cannot perform assignment operations for read-only domains, otherwise the compiler will report an error. A read-only domain can be an instance domain or a static domain. The type of read-only domain can be any type of C # language. However, constant constants must be assigned to the declaration, and the compiler can calculate this determined value during compilation period. Const modified constants are static variables that cannot be obtained for objects. The type of Const modified value is also limited, it can only be one of the following types (or can be converted to the following type): Sbyte, Byte, Short, USHORT, INT, UINT, Long, Ulong, Char, Float, Double, Dou, DECIMAL , BOOL, STRING, ENUM type, or reference type. The reference type capable of declaring Const can only be available for String or value NULL. In fact, if we can understand that constant constants is calculated when compiling, and substitution to each place referenced to the constant, and readonly is determined at runtime - just initialization After we don't want its value to change again, we can understand the goodness of C # designers, we can completely grasp the behavior of consT and readonly! C # does not advocate the use of the domain's protection level to public and use the user outside the class or any operation - that is too not Oo, or the specific point is too unsafe! For all the domains other than the class, C # recommends expressing attributes. Attributes do not represent storage locations, which is the foundation of attributes and domains. Inherit: An existing class (base class) When a new class (subclass) is delivered, the subclasses will automatically obtain all non-private methods, attributes, and fields that are declared in the base class (note the base class constructor is not Will be inherited.). Subclasses can expand the functionality of the base class by increasing or replacing the various methods, attributes, and fields of the inheritance. Inheritance capacity is one of the important differences between classes and structures. The structure does not support the inheritance mechanism. Learning the sea, the object-oriented programming is both a science, also an art. If this book can make everyone appreciate some of its mystery, the author is content! ! Inheritance and Precutructor Initializer, each constructor can use a preserver subclass that cannot inherit the base class constructor but can "use", specify the parent class and "this" by keyword "base". The constructor in this class is the pre-constructor of this constructor. When there is no designation, the parent class is not arrested by the default prepatructor, namely: "Base ()" access qualifier is understood: Many newcomers think that code sharing is the maximum open code, always do not think Declaring all things as "public", the less PUBLIC members inside, because you can reduce the debug, follow the principle of "to hide data. Make the class into a code black box".
Monday, October 25, 2004
One of the More Powerful Concepts in Object-Oriented Programming IS Polymorphism. The Roots of this Word, "Poly-" and "-morph" Mean, When Put Together, "Many Forms" Polymorphism (Breaky): " POLY- "A lot of intentions," - morph ", morphological shape polymorphism:
using System; public class DrawingBase {public virtual void Draw () {// the base class virtual method Console.WriteLine ( "I'm just a generic drawing object.");}} public class Line: DrawingBase {public override void Draw ( {// Sub-class rewriting the false method 1 console.writeline ("I'm a line.");}} Public class circle: Drawingbase {public override void draw () {// Sub-class rewriting false method 2 Console .Writeline ("I'm a circle.");}} Public class square: Drawingbase {public override void draw () {// Sub-class rewriting the false method 3 console.writeline ("I'm a square.") ;}}} Public class drawan (String [] args) {DrawingBase [] dobj = new drawingbase [4]; // Create a base class type array DOBJ [0] = new line (); // assignment type Convert DOBJ [1] = new circle (); // Declaration (time) is the base class, the actual type (runtime) is subclass type dobj [2] = new Square (); dobj [3] = new Drawingbase (); Foreach (DrawingBase Drawobj In Dobj) Drawobj.draw (); // Runtime "After Bind" Each DRAW version returnograph (}) AS operator is used to perform conversion between compatible types. The AS operator is used in the expressions in the following form: The Expression As Type AS operator is similar to type conversion, and the AS operator will generate empty when the conversion fails, rather than causing exceptions. Note that the AS operator only performs reference conversion and packing conversion. The AS operator cannot perform other conversions. The IS operator is used to check if the time type of the object is compatible with a given type. GetType () method and operator typeof () and system.type class
Type t = instance.gettype (); // An instance method, return a Type object type t = typeof (copy); // an operator, an operator is a type of non-aware, but also returns a TYPE object equal Sexual comparison: BOOL B = instance.gettype () == TypeOf (class); the so-called "implementation of an interface" refers to the method that the interface sounds in the interface is implemented in a certain class. The interface is essentially a definition of how the class needs to be responded. Interface Description Categories require implementation methods, properties, and events, and parameter types that each member needs to receive and returned, but leave the specific implementation of these members to implement class. Detective classes can override the virtual method and attributes in the base class, or hide the "(New keyword) of the non-virtual member in the base class. Operator Overloading :() Operators are defined for the built-in types, but that's not all You can add operators to your own types, allowing them to be used much like the operators with the built-in C # types.Requirement:. You create A Matrix Type and the Do a Mathematical Operation with Them. Solution:
Implement an add (), dotproduct (), AND Other methods to get the job done.
Matrix Result = mat1.add (mat2); // instanceormatrix results = matrix.add (mat1, mat2); // staticor Event Worsematrix Result = mat1.dotproduct (mat2) .dotproduct (mat3); // and so on .. . Much Easier to Have A Operator for the add operation and a * operator for the dot product operation.
Matrix Result = mat1 mat2; or matrix result = mat1 * mat2; or even bettermatrix results = mat1 * mat2 * mat3 * mat4;