C# – Variables and Constants
(7 minutes of reading time) As we have seen in previous articles, programming is a field that has gained more prominence with each passing day due to the growing demand present in the job market. To operate in this market, it is essential to have knowledge of programming languages, a set of rules by which a computer can be instructed to perform a task. There are many programming languages available for use today, and they are all developed with different purposes in mind. To facilitate your understanding, in today's article, we will talk about variables and constants in C#. Keep following the content and learn more about the topic! WHAT IS C#? C#, or C-Sharp, is a programming language developed by Microsoft as part of the .NET platform. Its purpose is to facilitate the development process. The language was developed to support object orientation and concepts such as encapsulation, inheritance, and polymorphism. Furthermore, it is type-strong and case-sensitive. That is, able to differentiate between lowercase and uppercase letters. Its typing is static and strong, which means that its variables cannot be typed during the execution of the program. TYPING CHARACTERISTICS The language is statically typed, that is, it is composed of a well-defined type of system that is validated throughout the code compile time. For this, you need to define explicit types for the variables that are created. Static type languages tend to have some advantages, such as a higher level of understanding of how code works and the ability to identify typos more easily. Also, refactoring processes are generally less error prone with static types. If a parameter of a function needs to change its type, the code will not compile until all places that use that function start specifying the new type. However, there are also some disadvantages: statically typed languages tend to be more verbose, as you always need to declare the types of variables. In addition, they can give a false sense of security, especially in cases of newer versions of languages, which tend to work with type inference. C# STANDARD DATA As each type of information has its specific variable, it is essential to know the types of data that a given language offers. Below we list the different C# data types, the values it accepts, and how much space a variable of that type takes up in memory. WHOLE NUMERIC TYPES Integer numeric types store numbers with no fractional part. Each type accepts a range of values, and the greater this range, the greater the number occupied in memory. In addition, types whose name comments with the letter “u”, from unsigned, do not accept negative values. They therefore support a wider range of positive values. Most programmers use the int type to work with integer values. However, you can choose the type that best fits your needs and takes up less memory. NUMERICAL TYPES OF FLOATING POINTS This type is represented by real numbers, that is, those that can have a fractional part. BOOLEAN TYPE They are used to store values that can be represented as “true” or “false”, with no intermediate values. TEXTUAL TYPES Texts can be stored by char and string types. The char type is used for one character only, while the string is a collection of characters. NOMINATION OF VARIABLES To follow a minimum standardization of the source code, C# determines some standards for the definition of names for the variables. The main standards are: - All variables are named with the first letter in lower case. Except in the case of variables with a composite name, in this scenario, the first letter must be lowercase, while the others must be uppercase; - Spaces must not be used in the middle of variable names; - Special characters must not be used to name variables. Except the underscore, in special situations as in the definition of private structures; - Variables must have declarative and self-explanatory names; - Variables are case-sensitive. DECLARING CONSTANTS Constants are like variables, but they cannot be changed. That is, the value cannot be modified during the execution of the application. The declaration of a constant in C# is done through the keyword “const”. TYPE INFERENCE Type inference is a technique used by compilers to automatically define the types of variables based on context. Which means that the compiler will automatically type the variable at the time of its declaration of the data type being assigned to it. This process allows the writing of less detailed code, as there is no longer the need to explicitly inform what the type of the variable will be at the time of its declaration. This inference is made by using the reserved word “var”. It is important to note that type inference does not interfere with C#'s statically typed language status, since the type was defined even if automatically. Furthermore, it should be noted that type inference is only possible if the variable is declared and has its initial value assigned at the same time. MODIFIERS In the case of variables, modifiers define their visibility, whether they can be accessed by classes other than their own, whether they will only be accessed by classes derived from the class they are in, and so on. In C#, the following modifiers are present: PUBLIC Access is not restricted. PROTECTED Access of this modifier is limited to classes or types derived from the class the variable is in. INTERNAL In this case, access is limited to the current module set. INTERNAL PROTECTED Access is only allowed to the current set or types derived from the containing class. PRIVATE Access to this variable is only available for the class it belongs to. Also, when a variable is declared without an access modifier, the compiler assigns the modified default private. Do you like our content? So, follow us on social media to stay on top of innovation and read our blog.
Share this article on your social networks:
Rate this article:
Other articles you might be interested in reading
- All (113)
- Career (31)
- Competitions (4)
- Design (6)
- Development (57)
- Diversity and Inclusion (3)
- Events (3)
- History (9)
- Industries (6)
- Innovation (25)
- Leadership (7)
- Projects (19)
- Well being (12)
Tips to Improve Productivity
(7 minutes of reading) We all have those days when nothing flows. We spend hours looking at the computer, we strive to finish a simple task, but nothing happens. Our productivity is simply close to zero. What to do these days?...
(5 minutes reading) Do you know what a Cloud Engineer does? A Cloud Engineer is a software and infrastructure specialist, responsible for the design, implementation, optimization, and management of cloud-based computing systems....
(7 minutes of reading) Developing quality software is what every developer wants most. But how to ensure this?...
How Many Programming Languages Are There?
(14 minutes of reading) Programming languages are the tools for software developers, engineers, and coders. They allow us to communicate instructions to computers and create amazing applications. Do you have an idea of how many programming languages there are? Check this article and put an end to your questions!...
(6 minutes of reading) Data governance is an increasingly important aspect of modern businesses and organizations. It is the process of managing, organizing, and controlling access to data in an organization....
(13 minutes of reading) Do you know what is Git? It is a version control system that is used a lot nowadays. In today's text we'll talk about some of the most important Git commands to make managing your code even simpler...
Would you like to have your article or video posted on beecrowd’s blog and social media? If you are interested, send us an email with the subject “BLOG” to [email protected] and we will give you more details about the process and prerequisites to have your article/video published in our channels
Rua Funchal, 538
São Paulo, SP
I’m a School
I’m a Company
© 2023 beecrowd
All Rights Reserved