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 (184)
- Career (38)
- Competitions (6)
- Design (7)
- Development (112)
- Diversity and Inclusion (3)
- Events (3)
- History (15)
- Industries (6)
- Innovation (37)
- Leadership (8)
- Projects (23)
- Well being (18)
Tech in Education
(9 minutes of reading)
In the contemporary educational landscape, technology plays an increasingly crucial role, revolutionizing not only the way students learn, but also how educators teach. As we adapt to a digitally connected world, new trends are emerging that promise to further transform the way education is designed and delivered. Come read this text to learn about the latest trends in educational technology and explore their impact on student development and the evolution of teaching. Come with us!...
Ethical Software Development
(5 minutes of reading)
Developing software is a complex activity that goes far beyond simple coding. It involves a meticulous process of planning, design, implementation, testing and maintenance to create reliable, efficient, and secure systems. However, in addition to seeking functionality and performance, developers must also carefully consider the ethical aspects of the software they are creating. In this text we will talk about ethics and responsibility when developing software. Come read!...
Balance Between Professional and Personal Growth
(6 minutes of reading)
In a world driven by the constant search for professional success, we often find ourselves immersed in our careers, forgetting the fundamental balance between professional and personal growth. As we dedicate hours to coding, solving problems, and advancing our technical skills, it's essential to remember that our journey as human beings go beyond the lines of code. Come read our text and see super cool tips on how to achieve this balance!...
How to Highlight Programming Competition Awards on your CV
(6 minutes of reading)
In a field as dynamic as software development, it is crucial to stand out from the crowd. An exceptional way to do this is through recognition and awards won in competitive programming competitions. In addition to demonstrating your superior technical skills, these awards attest to your ability to solve complex problems, collaborate as a team, and deliver exceptional results under pressure. Today we will talk about the curriculum and competitions, are you interested? Come with us!...
Open Source and Collaboration
(5 minutes of reading)
If you're ready to start exploring the world of open source, be aware that you will encounter many learning opportunities and challenges. Collaboration is at the heart of this environment, driving innovation and influencing the direction of technology. Come read our text to find out more about this subject!...
Women in Programming: Celebrating Achievements and Inspiring the Future
(5 minutes of reading)
This March 8th we will talk about the inspiring impact of women on programming throughout history, from pioneers like Ada Lovelace to contemporary leaders like Doina Precup and Reshma Saujani. This text celebrates their achievements and highlights progress towards a more inclusive and diverse technology industry. Join us to inspire and empower future generations of women in coding, creating a more equitable and brighter future for everyone in the technology industry....
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
Headquarter:
Rua Funchal, 538
Cj. 24
Vila Olímpia
04551-060
São Paulo, SP
Brazil