楼主: Reader's
1598 4

C# Cookbook, Second Edition [推广有奖]

  • 0关注
  • 0粉丝

已卖:1521份资源

博士生

59%

还不是VIP/贵宾

-

TA的文库  其他...

可解釋的機器學習

Operations Research(运筹学)

国际金融(Finance)

威望
0
论坛币
41198 个
通用积分
2.6173
学术水平
7 点
热心指数
5 点
信用等级
5 点
经验
2201 点
帖子
198
精华
1
在线时间
36 小时
注册时间
2015-6-1
最后登录
2024-3-3

楼主
Reader's 发表于 2015-6-1 06:44:47 |AI写论文

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

求职就业群
赵安豆老师微信:zhaoandou666

经管之家联合CDA

送您一个全额奖学金名额~ !

感谢您参与论坛问题回答

经管之家送您两个论坛币!

+2 论坛币

  • C# Cookbook, Second Edition
  • By: Jay Hilyard; Stephen Teilhet

  • Publisher: O'Reilly Media, Inc.

  • Pub. Date: January 30, 2006   Most Recent Edition

  • Print ISBN-13: 978-0-596-10063-6

  • Print ISBN-10: 0-596-10063-9

  • Pages in Print Edition: 1192

  • Subscriber Rating: [0 Ratings]



二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

关键词:Cookbook Edition Second dition editio Stephen Media

本帖被以下文库推荐

沙发
Reader's 发表于 2015-6-1 06:46:35
Recipe 3.1. Creating Union-Type Structures
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.

Solution
Use a structure and mark it with the StructLayout attribute (specifying the LayoutKind.Explicit layout kind in the constructor). In addition, mark each field in the structure with the FieldOffset attribute. The following structure defines a union in which a single signed numeric value can be stored:

  1. using System.Runtime.InteropServices;
  2.         [StructLayoutAttribute(LayoutKind.Explicit)]
  3.         struct SignedNumber
  4.         {
  5.            [FieldOffsetAttribute(0)]
  6.            public sbyte Num1;
  7.            [FieldOffsetAttribute(0)]
  8.            public short Num2;
  9.            [FieldOffsetAttribute(0)]
  10.            public int Num3;
  11.            [FieldOffsetAttribute(0)]
  12.            public long Num4;
  13.            [FieldOffsetAttribute(0)]
  14.            public float Num5;
  15.            [FieldOffsetAttribute(0)]
  16.            public double Num6;
  17.            [FieldOffsetAttribute(0)]
  18.            public decimal Num7;
  19.         }
复制代码

  1. The next structure is similar to the SignedNumber structure, except that it can contain a String type in addition to the signed numeric value:

  2.         [StructLayoutAttribute(LayoutKind.Explicit)]
  3.         struct SignedNumberWithText
  4.         {
  5.            [FieldOffsetAttribute(0)]
  6.            public sbyte Num1;
  7.            [FieldOffsetAttribute(0)]
  8.            public short Num2;
  9.            [FieldOffsetAttribute(0)]
  10.            public int Num3;
  11.            [FieldOffsetAttribute(0)]
  12.            public long Num4;
  13.            [FieldOffsetAttribute(0)]
  14.            public float Num5;
  15.            [FieldOffsetAttribute(0)]
  16.            public double Num6;
  17.            [FieldOffsetAttribute(0)]
  18.            public decimal Num7;
  19.            [FieldOffsetAttribute(16)]
  20.            public string Text1;
  21.         }
复制代码

藤椅
Reader's 发表于 2015-6-1 06:49:03
Recipe 3.2. Allowing a Type to Represent Itself as a String
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 class, you might want to allow objects of this type to be able to display themselves in a textual format. In the case of a Line object, it might display itself as (x1, y1)(x2, y2).

Solution
Override and/or implement the IFormattable.ToString method to display numeric information, such as for a Line structure:

  1. using System;
  2.         using System.Text;
  3.         using System.Text.RegularExpressions;

  4.         public struct Line : IFormattable
  5.         {
  6.            public Line(int startX, int startY, int endX, int endY)
  7.            {
  8.               x1 = startX;
  9.               x2 = endX;
  10.               y1 = startY;
  11.               y2 = endY;
  12.            }
  13.            public int x1;
  14.            public int y1;
  15.            public int x2;
  16.            public int y2;

  17.            public double GetDirectionInRadians( )
  18.            {
  19.               int xSide = x2 - x1;
  20.               int ySide = y2 - y1;
  21.               if (xSide == 0) // Prevent divide-by-zero.
  22.                     return (0);
  23.               else
  24.                     return (Math.Atan (ySide / xSide));
  25.            }
  26.            public double GetMagnitude( )
  27.            {
  28.               int xSide = x2 - x1;
  29.               int ySide = y2 - y1;
  30.               return ( Math.Sqrt((xSide * xSide) + (ySide * ySide)));
  31.            }

  32.            // This overrides the Object.ToString method.
  33.            // This override is not required for this recipe
  34.            // and is included for completeness.
  35.            public override string ToString( )
  36.            {
  37.               return (String.Format("({0},{1}) ({2},{3})", x1, y1, x2, y2));
  38.            }
  39.            public string ToString(string format)
  40.            {
  41.               return (this.ToString(format, null));
  42.            }
  43.            public string ToString(IFormatProvider formatProvider)
  44.            {
  45.               return (this.ToString(null, formatProvider));
  46.            }
  47.            public string ToString(string format, IFormatProvider formatProvider)
  48.            {
  49.               StringBuilder compositeStr = new StringBuilder("");
  50.               if ((format != null) && (format.ToUpper( ).Equals("V")))
  51.               {
  52.                     double direction = this.GetDirectionInRadians( );
  53.                     double magnitude = this.GetMagnitude( );
  54.                     string retStringD = direction.ToString("G5", formatProvider);
  55.                     string retStringM = magnitude.ToString("G5", formatProvider);
  56.                     compositeStr.Append("magnitude = ").Append(retStringM).Append
  57.                             ("\tDirection = ").Append(retStringD);
  58.               }
  59.               else
  60.               {
  61.                     string retStringX1 = this.x1.ToString(format, formatProvider);
  62.                     string retStringY1 = this.y1.ToString(format, formatProvider);
  63.                     string retStringX2 = this.x2.ToString(format, formatProvider);
  64.                     string retStringY2 = this.y2.ToString(format, formatProvider);
  65.                     compositeStr.Append("(").Append(retStringX1).Append(",").Append
  66.                           (retStringY1).Append(")(").Append(retStringX2).Append
  67.                           (",").Append(retStringY2).Append(")");
  68.               }
  69.               return (compositeStr.ToString( ));
  70.            }
  71.         }
复制代码

板凳
Reader's 发表于 2015-6-1 06:52:22
Recipe 3.3. Converting a String Representation of an Object into an Actual Object
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), you would want to convert it into a Line structure.

Solution
Implement a Parse method on your Line structure:
  1. using System;
  2.         using System.Text;
  3.         using System.Text.RegularExpressions;
  4.         public struct Line : IFormattable
  5.         {
  6.            public Line(int startX, int startY, int endX, int endY)
  7.            {
  8.            x1 = startX;
  9.            x2 = endX;
  10.            y1 = startY;
  11.            y2 = endY;
  12.         }
  13.         public int x1;
  14.         public int y1;
  15.         public int x2;
  16.         public int y2;
  17.         public override bool Equals(object obj)
  18.         {
  19.            bool isEqual = false;
  20.            if (obj == null || (this.GetType() != obj.GetType( )))
  21.            {
  22.               isEqual = false;
  23.            }
  24.            else
  25.            {
  26.               Line theLine = (Line)obj;
  27.               isEqual = (this.x1 == theLine.x1) &&
  28.                (this.y1 == theLine.y1) &&
  29.                (this.x2 == theLine.x2) &&
  30.                (this.y2 == theLine.y2);
  31.            }
  32.            return (isEqual);
  33.         }
  34.         public bool Equals(Line lineObj)
  35.         {
  36.            bool isEqual = (this.x1 == lineObj.x1) &&
  37.             (this.y1 == lineObj.y1) &&
  38.             (this.x2 == lineObj.x2) &&
  39.             (this.y2 == lineObj.y2);
  40.             return (isEqual);
  41.         }
  42.         public override int GetHashCode()
  43.         {
  44.            return ((x1 + x2) ^ (y1 + y2));
  45.         }
  46.         public static Line Parse(string stringLine)
  47.         {
  48.            if (stringLine == null)
  49.            {
  50.               throw (new ArgumentNullException(
  51.                        "stringLine",
  52.                        "A null cannot be passed into the Parse method."));
  53.            }
  54.            // Take this string (x1,y1)(x2,y2) and convert it to a Line object.
  55.            int X1 = 0;
  56.            int Y1 = 0;
  57.            int X2 = 0;
  58.            int Y2 = 0;
  59.            MatchCollection MC = Regex.Matches(stringLine,
  60.               @"\s*\(\s*(?<x1>\d+)\s*\,\s*(?<y1>\d+)\s*\)\s*\(\s*(?<x2>" +
  61.               @"\d+)\s*\,\s*(?<y2>\d+)\s*\)" );
  62.            if (MC.Count == 1)
  63.            {
  64.               Match M = MC[0];
  65.               X1 = int.Parse(M.Groups["x1"].Value);
  66.               Y1 = int.Parse(M.Groups["y1"].Value);
  67.               X2 = int.Parse(M.Groups["x2"].Value);
  68.               Y2 = int.Parse(M.Groups["y2"].Value);
  69.            }
  70.            else
  71.            {
  72.               throw (new ArgumentException("The value " + stringLine +
  73.                                            " is not a well formed Line value."));
  74.            }
  75.            return (new Line(X1, Y1, X2, Y2));
  76.         }
  77.         public double GetDirectionInRadians()
  78.         {
  79.            int xSide = x2 - x1;
  80.            int ySide = y2 - y1;
  81.            if (xSide == 0) // Prevent divide-by-zero.
  82.               return (0);
  83.            else
  84.               return (Math.Atan (ySide / xSide));
  85.         }
  86.         public double GetMagnitude()
  87.         {
  88.            int xSide = x2 - x1;
  89.            int ySide = y2 - y1;
  90.            return (Math.Sqrt( Math.Sqrt((xSide * xSide) + (ySide * ySide))));
  91.         }
  92.         public override string ToString()
  93.         {
  94.            return (String.Format("({0},{1}) ({2},{3})", x1, y1, x2, y2));
  95.         }
  96.         public string ToString(string format)
  97.         {
  98.            return (this.ToString(format, null));
  99.         }
  100.         public string ToString(IFormatProvider formatProvider)
  101.         {
  102.            return (this.ToString(null, formatProvider));
  103.         }
  104.         public string ToString(string format, IFormatProvider formatProvider)
  105.         {
  106.            StringBuilder compositeStr = new StringBuilder("");
  107.            if ((format != null) && (format.ToUpper().Equals("V")))
  108.            {
  109.               double direction = this.GetDirectionInRadians();            
  110.               double magnitude = this.GetMagnitude();
  111.               string retStringD = direction.ToString("G5", formatProvider);
  112.               string retStringM = magnitude.ToString("G5", formatProvider);
  113.               compositeStr.Append(
  114.                     "magnitude = ").Append(retStringM).Append(
  115.                                 "\tDirection = ").Append(retStringD);
  116.            }
  117.            else
  118.            {
  119.               string retStringX1 = this.x1.ToString(format, formatProvider);
  120.               string retStringY1 = this.y1.ToString(format, formatProvider);
  121.               string retStringX2 = this.x2.ToString(format, formatProvider);
  122.               string retStringY2 = this.y2.ToString(format, formatProvider);
  123.               compositeStr.Append("(").Append(retStringX1).Append(",").Append(
  124.                     retStringY1).Append(")(").
  125.                        Append(retStringX2).Append(",").Append(
  126.                           retStringY2).Append(")");
  127.               }
  128.               return (compositeStr.ToString());
  129.            }
  130.         }
复制代码

报纸
casbbyli 发表于 2015-6-6 13:20:38
hao shu

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注jltj
拉您入交流群
GMT+8, 2025-12-22 15:06