首页 > 编程语言 >(二十八)C#编程基础复习——This关键字

(二十八)C#编程基础复习——This关键字

时间:2023-11-28 09:57:00浏览次数:38  
标签:Console 复习 C# 编程 class WriteLine Test public string

在C#中,可以使用this关键字来表示当前对象,日常开发中我们可以使用this关键字来访问类中的成员属性以及函数。不仅如此this关键字还有一些其他的用法,示例如下:

一、使用this表示当前类的对象

namespace _016
{
    internal class Program
    {
        static void Main(string[] args)
        {          
            Website site = new Website("C语言中文网", "http://c.biancheng.net/");
            site.Display();
            Console.ReadKey();
        }
    }
    public class Website
    {
        private string name;
        private string url;
        public Website(string a,string b)
        {
            this.name = a;
            this.url = b;
        }
        public void Display()
        {
            Console.WriteLine(name+" "+url);
        }
    }
}

运行结果:

二、使用this关键字串联构造函数

namespace _016
{
    internal class Program
    {
        static void Main(string[] args)
        {
            Test test = new Test("C语言中文网");
            Console.ReadKey();
        }
    }
    public class Test
    {
        public Test()
        {
            Console.WriteLine("无参构造函数");
        }
        public Test(string text) : this()
        {
            Console.WriteLine(text);
            Console.WriteLine("实例化构造函数");
        }
    }
}

运行结果:

三、使用this关键字作为类的索引器

 


namespace _016
{
    internal class Program
    {
        static void Main(string[] args)
        {
            Test a = new Test();
            Console.WriteLine("Temp0:{0},Temp1:{1}", a[0], a[1]);
            a[0] = 15;
            a[1] = 20;
            Console.WriteLine("Temp0:{0},Temp1:{1}", a[0], a[1]);
            Console.ReadKey();
        }
    }
    public class Test
    {
        int Temp0;
        int Temp1;
        public int this[int index]
        {
            get
            {
                return (0 == index) ? Temp0 : Temp1;
            }
            set
            {
                if(0==index)
                {
                    Temp0 = value;
                }
                else
                {
                    Temp1 = value;
                }
            }
        }
    }
}

运行结果:

四、使用this关键字作为原始类型的扩展方法

namespace _016
{
    internal class Program
    {
        static void Main(string[] args)
        {
            string str = "C语言中文网";
            string newstr = str.ExpandString();
            Console.WriteLine(newstr);
            Console.ReadKey();
        }
    }
    public static class Test
    {
        public static string ExpandString(this string name)
        {
            return name + "http://c.biancheng.net/";
        }
    }


}

运行结果:

标签:Console,复习,C#,编程,class,WriteLine,Test,public,string
From: https://www.cnblogs.com/daihaoliulingyi601/p/17861128.html

相关文章

  • Switchhost修改host配置
    hosts是什么定义:是一个没有扩展名的系统文件,用来告诉浏览器网址对应的ip地址。作用:用户在浏览器访问网址时,浏览器首先访问host文件,寻找对应的ip地址,如果找到,便打开对应网页,不需要DNS域名解析,如果没找到,系统会将其网址提交DNS域名解析服务器进行ip地址的解析。由此可见,通过修改h......
  • C++标准库类std::packaged_task
    std::packaged_task是C++11引入的标准库类,用于封装可调用对象,如函数等,并将封装对象作为异步任务进行管理,通过与std::future结合使用,完成异步任务结果的获取。#include<iostream>#include<thread>#include<future>std::stringpromise_string(std::stringstr){......
  • c#之图书借阅系统
    人机交互大作业:c#完成一项系统cs结构的,我选择的是图书借阅管理系统,具体教程跟着b站上的一个博主【C#图书管理系统教程 2023年winform窗体应用 SQLServer-哔哩哔哩】https://b23.tv/Lxz3SJK这个博主全程代码详解主要我写的时候遇到的问题:vstdio版本过低导致sql语句不可以......
  • TS版LangChain实战:基于文档的增强检索(RAG)
    LangChainLangChain是一个以LLM(大语言模型)模型为核心的开发框架,LangChain的主要特性:可以连接多种数据源,比如网页链接、本地PDF文件、向量数据库等允许语言模型与其环境交互封装了ModelI/O(输入/输出)、Retrieval(检索器)、Memory(记忆)、Agents(决策和调度)等核心组件可以使用链......
  • c语言中函数指针用法
    #include<stdio.h>#defineMAX10voidswap(int*x,int*y){inttemp;temp=*x;*x=*y;*y=temp;}voidfun(int*height,int*age){intn=10;*height=n*10;*age=n*2000;}int*createArray(intsize){......
  • CF1901E Compressed Tree(树dp)
    Problem题目地址Solution来自fcy大佬的思路记\(f_u\)表示假定以\(u\)为根的子树,在压缩后,(子树内的某一个点(包括\(u\)))可以向外(除\(u\)为根的子树外所以点的集合)连一条边时的最大\(sum\)。换言之,我们把树拆成以\(u\)为根的子树(记作\(Tree_u\))和非\(Tree_u\)部分。而......
  • 关于CSS3的学习
    CSS的普通选择器:标签选择器、id选择器、类选择器、通用选择器(*)、分组选择器(将具有相同样式的元素放在一起,之间用","分隔)。注:类名和id名不能以数字开头。 rgba是rgb颜色的扩展,前三个值为三原色和rgb一致,第四个值是设置透明度的,1为完全不透明,0是完全透明为白色。hex颜色是用......
  • Count Beautiful Substrings II
    CountBeautifulSubstringsIIYouaregivenastring s andapositiveinteger k.Let vowels and consonants bethenumberofvowelsandconsonantsinastring.Astringis beautiful if:vowels==consonants.(vowels*consonants)%k==0,inothert......
  • Make Lexicographically Smallest Array by Swapping Elements
    MakeLexicographicallySmallestArraybySwappingElementsYouaregivena 0-indexed arrayof positive integers nums anda positive integer limit.Inoneoperation,youcanchooseanytwoindices i and j andswap nums[i] and nums[j] if |nums......
  • apache的文件工具类FileUtils
    org.apache.commons.io.FileUtils是apache提供用来操作文件的工具类,可以简化文件操作。<!--FileUtils--><dependency><groupId>commons-io</groupId><artifactId>commons-io</artifactId><version>2.4</version></dependen......