c#參數(shù)類型詳解(傳值參數(shù)常空,引用參數(shù),輸出參數(shù)铣缠,具名參數(shù)昆禽,可選參數(shù)醉鳖,數(shù)組參數(shù),擴展方法)

傳值參數(shù)

1壮韭、值參數(shù)創(chuàng)建變量副本
2纹因、對值參數(shù)的操作永遠不影響變量的值

傳值參數(shù)——值類型

傳值參數(shù)——值類型
class Program
    {
        static void Main(string[] args)
        {
            int i = 5;
            Function(i);
            Console.WriteLine(i);
        }
        static void Function(int a)
        {
            a++;
            Console.WriteLine(a);
        }
    }
結(jié)果

傳值參數(shù)——引用類型:創(chuàng)建新對象

傳值參數(shù)——引用類型:創(chuàng)建新對象
 class Program
    {
        static void Main(string[] args)
        {
            Student student1 = new Student() { Name="Tom"};
            Function(student1);
            Console.WriteLine($"{student1.Name}");
        }
        static void Function(Student stu)
        {
            stu = new Student() {Name="Tim" };
            Console.WriteLine($"{stu.Name}");
        }
    }
    class Student
    {
        public string Name;
    }
輸出結(jié)果

參數(shù)傳遞——引用類型:只操作對象,不創(chuàng)建新對象

參數(shù)傳遞——引用類型:只操作對象是牢,不創(chuàng)建新對象
 class Program
    {
        static void Main(string[] args)
        {
            Student student1 = new Student() { Name="Tom"};
            Function(student1);
            Console.WriteLine($"{student1.Name}");
        }
        static void Function(Student stu)
        {
            stu.Name = "Tim";
            Console.WriteLine(stu.Name);
        }
    }
    class Student
    {
        public string Name;
    }
運行結(jié)果

引用參數(shù)

1驳棱、引用類型并不創(chuàng)建變量的副本
2农曲、使用ref修飾符顯式指出——此方法的副作用是改變實際參數(shù)的值

引用參數(shù)——值類型

引用參數(shù)——值類型
 class Program
    {
        static void Main(string[] args)
        {
            int i = 5;
            Function(ref i);
            Console.WriteLine(i);
        }
        static void Function(ref int a)
        {
            a++;
            Console.WriteLine(a);
        }
    }
結(jié)果

引用參數(shù)——引用類型:創(chuàng)建新對象

引用參數(shù)——引用類型:創(chuàng)建新對象
 class Program
    {
        static void Main(string[] args)
        {
            Student student1 = new Student() { Name="Tom"};
            Console.WriteLine($"{student1.Name}");
            Function(ref student1);
            Console.WriteLine($"{student1.Name}");
        }
        static void Function(ref Student stu)
        {
            stu = new Student();
            stu.Name = "Tim";
            Console.WriteLine(stu.Name);
        }
    }
    class Student
    {
        public string Name;
    }
結(jié)果

引用參數(shù)——引用類型:不創(chuàng)建新對象

引用參數(shù)——引用類型:不創(chuàng)建新對象
 class Program
    {
        static void Main(string[] args)
        {
            Student student1 = new Student() { Name="Tom"};
            Console.WriteLine($"{student1.Name}");
            Function(ref student1);
            Console.WriteLine($"{student1.Name}");
        }
        static void Function(ref Student stu)
        {
            stu.Name = "Tim";
            Console.WriteLine(stu.Name);
        }
    }
    class Student
    {
        public string Name;
    }
結(jié)果

輸出參數(shù)

1暮的、輸出菜蔬并不創(chuàng)建變量的副本
2、方法體內(nèi)必須要有對輸出變量賦值的操作
3猖腕、使用out修飾符顯式指出——此方法的副作用是通過參數(shù)向外輸出值
4恨闪、從語義來講——ref是為了改變,out是為了輸出

輸出參數(shù)——值類型

輸出參數(shù)——值類型
 class Program
    {
        static void Main(string[] args)
        {
            double x = 0;
            bool b = DoubleParser.TryParse("123",out x);
            if (b == true)
            {
                Console.WriteLine(x);
            }
            else {
                Console.WriteLine(x);
            }
        }
        
    }
    class DoubleParser
    {
        public static bool TryParse(string input,out double result)
        {
            try
            {
                result = double.Parse(input);
                return true;
            }
            catch 
            {
                result = 0;
                return false;                
            }
        }
    }
結(jié)果

輸出參數(shù)——引用類型

輸出參數(shù)——引用類型
 class Program
    {
        static void Main(string[] args)
        {
            Student student = null;
            bool b = StudentFactory.Creat("Tom", 20, out student);
            if (b == true)
            {
                Console.WriteLine($"student.Nmae={student.Name},student.Age={student.Age}");
            }
            
        }  
    }
    class Student
    {
        public string Name { get; set; }
        public int Age { get; set; }
    }
    class StudentFactory
    {
        public static bool Creat(string name, int age, out Student student)
        {
            student = null;
            if (string.IsNullOrEmpty(name))
            {
                return false;
            }
            if (age > 100 && age < 0)
            {
                return false;
            }
           
            student = new Student() { Name = name, Age = age };
            return true;
        }
    }
結(jié)果

數(shù)組參數(shù)

必需是形參列表中的最后一個參數(shù)蜡豹,有params修飾
Sting.Fromat和Sting.split方法就是用到了數(shù)組參數(shù)

具名參數(shù)

參數(shù)的位置不受約束

 class Program
    {
        static void Main(string[] args)
        {
            Function(name: "Tom",age:20);
            Function(age: 20,name: "Tom" );
        }
        static void Function(string name, int age)
        {
            Console.WriteLine($"Name={name},Age={age}");
        }
    }

可選參數(shù)

參數(shù)因為具有默認值而變得“可選”

class Program
    {
        static void Main(string[] args)
        {
            Function();
        }
        static void Function(string name="Tom", int age=20)
        {
            Console.WriteLine($"Name={name},Age={age}");
        }
    }

擴展方法(this參數(shù))

1余素、擴展方法必須是共有炊昆,靜態(tài)的
2、必須是參數(shù)列表中的第一個凤巨,由this修飾
3敢茁、必須由一個靜態(tài)類(一般名為SomeTypeExtent)來統(tǒng)一收納對Some Type類型的擴展方法

class Program
    {
        static void Main(string[] args)
        {
            double x = 3.1415926;
            double y = x.round(5);
            Console.WriteLine(y);
        }  
    }
    static class DoubleExtansion
    {
        static public double round(this double input ,int digits)
        {
            double result = Math.Round(input, digits);
            return result;
        }
    }
各種參數(shù)的使用場景
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末彰檬,一起剝皮案震驚了整個濱河市谎砾,隨后出現(xiàn)的幾起案子捧颅,更是在濱河造成了極大的恐慌碉哑,老刑警劉巖,帶你破解...
    沈念sama閱讀 219,589評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件妆毕,死亡現(xiàn)場離奇詭異贮尖,居然都是意外死亡,警方通過查閱死者的電腦和手機闰蛔,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,615評論 3 396
  • 文/潘曉璐 我一進店門序六,熙熙樓的掌柜王于貴愁眉苦臉地迎上來蚤吹,“玉大人,你說我怎么就攤上這事裁着。” “怎么了二驰?”我有些...
    開封第一講書人閱讀 165,933評論 0 356
  • 文/不壞的土叔 我叫張陵桶雀,是天一觀的道長。 經(jīng)常有香客問我全肮,道長棘捣,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,976評論 1 295
  • 正文 為了忘掉前任评疗,我火速辦了婚禮,結(jié)果婚禮上邑彪,老公的妹妹穿的比我還像新娘胧华。我一直安慰自己宙彪,他們只是感情好,可當我...
    茶點故事閱讀 67,999評論 6 393
  • 文/花漫 我一把揭開白布悲没。 她就那樣靜靜地躺著示姿,像睡著了一般逊笆。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上难裆,一...
    開封第一講書人閱讀 51,775評論 1 307
  • 那天乃戈,我揣著相機與錄音,去河邊找鬼缩歪。 笑死谍憔,一個胖子當著我的面吹牛,可吹牛的內(nèi)容都是我干的骗污。 我是一名探鬼主播沈条,決...
    沈念sama閱讀 40,474評論 3 420
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼屋厘!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起议纯,我...
    開封第一講書人閱讀 39,359評論 0 276
  • 序言:老撾萬榮一對情侶失蹤瞻凤,失蹤者是張志新(化名)和其女友劉穎世杀,沒想到半個月后,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體瞻坝,經(jīng)...
    沈念sama閱讀 45,854評論 1 317
  • 正文 獨居荒郊野嶺守林人離奇死亡所刀,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,007評論 3 338
  • 正文 我和宋清朗相戀三年浮创,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片蒸矛。...
    茶點故事閱讀 40,146評論 1 351
  • 序言:一個原本活蹦亂跳的男人離奇死亡雏掠,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出摧玫,到底是詐尸還是另有隱情绑青,我是刑警寧澤,帶...
    沈念sama閱讀 35,826評論 5 346
  • 正文 年R本政府宣布坏挠,位于F島的核電站邪乍,受9級特大地震影響对竣,放射性物質(zhì)發(fā)生泄漏否纬。R本人自食惡果不足惜蛋褥,卻給世界環(huán)境...
    茶點故事閱讀 41,484評論 3 331
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望膜廊。 院中可真熱鬧弃理,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,029評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至骑冗,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間贼涩,已是汗流浹背薯蝎。 一陣腳步聲響...
    開封第一講書人閱讀 33,153評論 1 272
  • 我被黑心中介騙來泰國打工占锯, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人消略。 一個月前我還...
    沈念sama閱讀 48,420評論 3 373
  • 正文 我出身青樓艺演,卻偏偏與公主長得像婿失,于是被迫代替她去往敵國和親啄寡。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 45,107評論 2 356