Вопрос

Access modifiers like public and keywords like virtual customize the behavior of a class or property. So their behavior is similar to attributes. But how do they internally work? Do they call any code or any attributes from .Net class library?

Это было полезно?

Решение

No, public is an access modifier. Virtual is a keyword as you correctly indicated, though neither refer to any .NET library in particular. They don't refer to any attributes and it wouldn't be correct to call them attributes for that matter. Attributes are distinctly different.

How does they internally work?

Please refer to the links provided above.

Does they call any code or any attributes from .Net class library?

No, as stated already they're distinctly different from attributes and are not associated with any .NET library.

Другие советы

No, the are telling the C# compiler how to handle the method in terms of accessibility and inheritance, respectively.

They have nothing to do with the .Net class library. Also, the word "attribute" has a very specific meaning in the context of C#, and they have nothing to do with these keywords.

public and virtual are basically reserved words which means they have specific meaning. when compiler identifies those reserved words it would take necessary actions defined in the design architecture.hence user could not declare reserved words as identifiers. i think they wont refer anything like datatypes( data types will be referred internally as they are designed as structures.)

They are part of the language. They are known as keywords that are reserved by the language and will be parsed in the compiler explicitly based on what they do. They are reserved, so you cannot do something like this:

  User public = new User();

in this I used the "public" keyword as a variable name, which is illegal by C# syntax. If a keyword is found to be inappropriately used, the compiler will not compile your code. (often in IDEs, the compiler wont even get invoked if there is an error)

One thing to note, is that they are NOT operators. (such as + - =). Operators, among many things, can be overloaded (you can define how they mutate your data). In C# (I cannot vouch for other languages), keywords cannot be overloaded.

A keyword in a programming language is often used to tell the compiler what to do with a specific piece of code within a certain context. Some effects of using certain keywords are more apparent than others. Kind of like a hint to what this code is doing (in your case, what is it's protection level). Each keyword has it's own rules on what they do. For instance, LINQ has its own operators such as as, where, select which cannot be used like public, protected and private.

For more information about keywords in C#, check out the following:

http://msdn.microsoft.com/en-us/library/x53a06bb.aspx

The public keyword is an instruction to the compiler, known as an access modifier. It means you intend a type or member to be accessible publicly, that is, in an unrestricted way.

Microsoft describe it quite well here:

http://msdn.microsoft.com/en-us/library/yzh058ae(v=vs.80).aspx

The virtual keyword is another instruction to the compiler. It is a different kind of modifier than public. It does not relate to access. It instead indicates that you intend to be able to override the behavior of a given method / property in a derived class.

Microsoft have a good description here:

http://msdn.microsoft.com/en-us/library/9fkccyh4(v=vs.71).ASPX

Neither of them relate in any way to attributes, which are a different concept entirely. They are described here:

http://msdn.microsoft.com/en-us/library/z0w1kczw.aspx

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top