当前位置: 移动技术网 > IT编程>开发语言>c# > 弃元

弃元

2020年03月16日  | 移动技术网IT编程  | 我要评论

弃元就是使用下划线_作为一个占位符,但不占存储空间。

元组(valuetuple、tuple)使用弃元例子。

using system;

namespace consoleapp4
{
    class program
    {
        public static void main()
        {
            // valuetuple和tuple都可以使用弃元
            var (_, _, _, pop1, _, pop2) = querycitydataforyears("new york city", 1960, 2010);
           
            console.writeline($"population change, 1960 to 2010: {pop2 - pop1:n0}");
        }

        private static (string,double,int,int,int,int) querycitydataforyears(string name,int year1,int year2)
        {
            int population1 = 0, population2 = 0;
            double area = 0;

            if (name == "new york city")
            {
                area = 468.48;
                if (year1 == 1960)
                {
                    population1 = 7781984;
                }
                if (year2 == 2010)
                {
                    population2 = 8175133;
                }
                return (name, area, year1, population1, year2, population2);
            }
            return ("", 0, 0, 0, 0, 0);
            }     
    }   
}

对象析构,析构函数的参数决定了元组的参数 。

using system;

namespace consoleapp4
{
    class program
    {
        public static void main()
        {
            var p = new person("john", "quincy", "adams", "boston", "ma");

            // deconstruct(out string fname, out string lname, out string city, out string state) 
            var (fname,_,city,_) = p;
          
            console.writeline($"hello {fname} of {city}!");            
        }

        private static (string,double,int,int,int,int) querycitydataforyears(string name,int year1,int year2)
        {
            int population1 = 0, population2 = 0;
            double area = 0;

            if (name == "new york city")
            {
                area = 468.48;
                if (year1 == 1960)
                {
                    population1 = 7781984;
                }
                if (year2 == 2010)
                {
                    population2 = 8175133;
                }
                return (name, area, year1, population1, year2, population2);
            }
            return ("", 0, 0, 0, 0, 0);
            }     
    }

    public class person
    {
        public string firstname { get; set; }
        public string middlename { get; set; }
        public string lastname { get; set; }
        public string city { get; set; }
        public string state { get; set; }

        public person(string fname, string mname, string lname,
                      string cityname, string statename)
        {
            firstname = fname;
            middlename = mname;
            lastname = lname;
            city = cityname;
            state = statename;
        }

        // 返回firstname、lastname的元组
        public void deconstruct(out string fname,out string lname)
        {
            fname = firstname;
            lname = lastname;
        }        

        public void deconstruct(out string fname, out string mname, out string lname)
        {
            fname = firstname;
            mname = middlename;
            lname = lastname;
        }

        public void deconstruct(out string fname, out string lname,
                                out string city, out string state)
        {
            fname = firstname;
            lname = lastname;
            city = city;
            state = state;
        }
    }
}

switch,case分支可以使用占位符,相当default的作用。

using system;
using system.globalization;

namespace consoleapp4
{
    class program
    {
        public static void main()
        {
            object[] objects = { cultureinfo.currentculture,
                           cultureinfo.currentculture.datetimeformat,
                           cultureinfo.currentculture.numberformat,
                           new argumentexception(), null };
            foreach (var obj in objects)
                providesformatinfo(obj);

            console.readline();
        }

        private static void providesformatinfo(object obj)
        {
            switch (obj)
            {
                // 实现了iformatprovider,if obj is iformatprovider fmt 
                case iformatprovider fmt:
                    console.writeline($"{fmt} object");
                    break;
                case null:
                    console.writeline("null");
                    break;
                case object _:
                    console.writeline("without format information");
                    break;
            }
        }      
    }
}

具有out参数的调用方法,有时只想知道是否能转换而不需要其转换之后的值。

 bool isdate = datetime.tryparse("8989", out _);

独立的弃元 (好像没有用....)

 _ = "";

 

当上下文存在下划线标识符,则无法再用下划线作为占位符,即不能使用弃元。

private static void showvalue(int _)
{
    byte[] arr = { 0, 0, 1, 2 };
    // 此时无法作为占位符,因为和参数的有效标识符冲突
    _ = bitconverter.toint32(arr, 0);
    console.writeline(_);
}
// the example displays the following output:
//       33619968
private static void showvalue(int _)
{
    string value = _.tostring();
    int newvalue = 0;
    // 无法作为占位符,类型和参数标识符_也不一样,编译报错
    _ = int32.tryparse(value, out newvalue);
    return _ == newvalue;
}       

 

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网