用户定义类和String类之间的主要区别是什么。 [英] What is the main difference between user define class and String class.

查看:62
本文介绍了用户定义类和String类之间的主要区别是什么。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 student 类,它具有面向对象概念中包含的任何类的属性,方法和变量。当我在dot net中浏览 String 类的元数据时,它还包含属性,方法,变量。因此,当我们为String赋值时,我们通常会这样做:



I have one student class which has properties,methods and variables which any class in object orientation concept contains. When I gone through metadata of String class in dot net, It also contains properties,methods,variables. So when we assign value to the String we does this normally :

String s = "Code Project"; // No compile error (1st line)

String test = new String(); // Compile error  (2rd line)





第二行出错:

'string'不包含带0参数的构造函数



但是我知道dot net中的每个类应该有至少一个构造函数



其次,



如果String类包含属性,方法,变量。那么第一行代码是如何工作的。

以及我尝试用我的Student类做的同样的事情它向我显示编译错误。





Error in 2nd line:
'string' does not contain a constructor that takes 0 arguments

but as I know every class in dot net should have at least one constructor

Secondly,

If String class contains properties,methods,variables. Then how the 1st line code works.
and the if same thing I have tried to do with my Student class it shows me compile error.

Student s= "Code Project"; // Compile error  (3rd line)





他们用预定义的类做了额外的事情吗?

我们在用户定义类中无法做到。



我可以创建一个类似于此的另一个String类吗?



谢谢,

Suraj



Is there any extra things they did with the predefined classes?
which we cannot do in user define classes.

Can I make a another String class similar to this?

Thanks,
Suraj

推荐答案

1.您的第一行声明了一个类型为string的变量用字符串文字来表示它 - 字符串表达式,就像第二部分给出的临时常量一样。



2.第二行返回错误,因为 String 类没有没有参数的构造函数。

有关字符串类及其构造函数的详细信息,请参阅MSDN中的详细信息: http://msdn.microsoft.com/en-us/library/System.String(v = vs.110).aspx [ ^ ]



3.要创建自己的类,可以在第3个示例中初始化对象,您应该实现隐式运算符转换器。请参阅下一篇文章中的详细信息:了解C#中的隐式运算符重载 [ ^ ]
1.Your first line declare a variable of type string and inits it with the "string literal"-the string expression, like a temp constant, given in the second part.

2.The second line return error, because String class does not have an constructor with no parameters.
For details about string class, and its constructors, see details in MSDN : http://msdn.microsoft.com/en-us/library/System.String(v=vs.110).aspx[^]

3.To create your own class that could init your object like in your 3rd example your should implement your implicit operator converter. See details in the next article: Understanding Implicit Operator Overloading in C#[^]


如果查看

的文档字符串类 [ ^ ]

您将看到没有构造函数接受0个参数,因此第一个编译器错误。



第一行有效,因为文字字符串Code Project被编译器视为常量并存储在表中的堆。

然后将该常量的引用赋给变量 s

有关字符串存储的更多信息,请参阅此链接。

String.Intern Method [ ^ ]



为您自己添加分配功能class,你需要添加一个隐式运算符。

If you look at the documentation for the
String Class[^]
you will see that there is no constructor that takes 0 arguments, hence the first compiler error.

The first line works because the literal string "Code Project" is treated as a constant by the compiler and stored on the heap in table.
Then the reference of that constant is assigned to the variable s.
See this link for more information about string storage.
String.Intern Method[^]

To add assignment functionality to your own class, you need to add an implicit operator.
public static implicit operator Student(string s)
{
    Student stud = new Student(s); //Internally call Student constructor
    return stud;
}



=运营​​商(C#参考) [ ^ ]

隐式(C#参考) [ ^ ]



当然,你需要在 Student 类中有一个构造函数,它以字符串作为参数。



如果您想要像这样分配其他数据类型,则只需为每种类型添加一个运算符。


= Operator (C# Reference)[^]
implicit (C# Reference)[^]

Of course you need to have a constructor in your Student class that takes a string as an argument.

If you have other data types that you want to assign like this, you just add one operator per type.


这篇关于用户定义类和String类之间的主要区别是什么。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆