c#可空類型? 與 ??小盆友你是不是很多問號

       c# 中的可空類型,今天記錄了下c#中的可空類型,之前編寫沒有遇到過,而且之前c++中也沒有這個概念,可能我學的是個假的c++吧,這個是泛型中的一個知識點,相當於c++中的模板,話不多說一起來看看吧。

int ? op1 = null;
int ? result = op1 *2 ?? 5;

  如果沒有系統學c#的看到這個會不會一臉的問號,還可以用?來寫在這裏??,那我們簡單的分析一下吧,int ?相當於程序中的:

System.Nullable<int>

   的縮寫,說明變量可以是空變量。

 ??

 是空接合運算符,是一個二元的運算符:

相當於:

op1  ?? op2
等價於
op1 = null ? op2 : op1;

下面我們來看一個例子:

首先的Vector 中的一個類:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Ch12Ex01
{
    public class Vector
    {
        public double? R = null;
        public double? Theta = null;

        public double? ThetaRadians
        {
            get
            {
                // Convert degrees to radians.
                return (Theta * Math.PI / 180.0);
            }
        }

        public Vector(double? r, double? theta)
        {
            // Normalize.
            if (r < 0)
            {
                r = -r;
                theta += 180;
            }
            theta = theta % 360;

            // Assign fields.
            R = r;
            Theta = theta;
        }

        public static Vector operator +(Vector op1, Vector op2)
        {
            try
            {
                // Get (x, y) coordinates for new vector.
                double newX = op1.R.Value * Math.Sin(op1.ThetaRadians.Value)
                   + op2.R.Value * Math.Sin(op2.ThetaRadians.Value);
                double newY = op1.R.Value * Math.Cos(op1.ThetaRadians.Value)
                   + op2.R.Value * Math.Cos(op2.ThetaRadians.Value);

                // Convert to (r, theta).
                double newR = Math.Sqrt(newX * newX + newY * newY);
                double newTheta = Math.Atan2(newX, newY) * 180.0 / Math.PI;

                // Return result.
                return new Vector(newR, newTheta);
            }
            catch
            {
                // Return "null" vector.
                return new Vector(null, null);
            }
        }

        public static Vector operator -(Vector op1)
        {
            return new Vector(-op1.R, op1.Theta);
        }

        public static Vector operator -(Vector op1, Vector op2)
        {
            return op1 + (-op2);
        }

        public override string ToString()
        {
            // Get string representation of coordinates.
            string rString = R.HasValue ? R.ToString() : "null";
            string thetaString = Theta.HasValue ? Theta.ToString() : "null";

            // Return (r, theta) string.
            return string.Format("({0}, {1})", rString, thetaString);
        }
    }
}

主函數部分:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Ch12Ex01
{
    class Program
    {
        static void Main(string[] args)
        {
            Vector v1 = GetVector("vector1");
            Vector v2 = GetVector("vector1");
            Console.WriteLine("{0} + {1} = {2}", v1, v2, v1 + v2);
            Console.WriteLine("{0} - {1} = {2}", v1, v2, v1 - v2);
            Console.ReadKey();
        }

        static Vector GetVector(string name)
        {
            Console.WriteLine("Input {0} magnitude:", name);
            double? r = GetNullableDouble();
            Console.WriteLine("Input {0} angle (in degrees):", name);
            double? theta = GetNullableDouble();
            return new Vector(r, theta);
        }

        static double? GetNullableDouble()
        {
            double? result;
            string userInput = Console.ReadLine();
            try
            {
                result = double.Parse(userInput);
            }
            catch
            {
                result = null;
            }
            return result;
        }
    }
}

          就是這樣的簡單的,沒有啥,慢慢來,c#不是那麼難,還是慢慢一步一步來學習吧,我覺得要換一種學習方式了,一程序例子爲主,一步一步去理解裏面的東西。

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章