Curso C#. POO IX. Clases anónimas. Vídeo 35
Methods and Static Variables in C#
In this section, the video discusses methods and static variables in C#, exploring the differences between static and non-static variables within classes.
Understanding Non-Static Variables
- Non-static variables create independent copies for each object or instance within a class.
- Objects can have their own variable copies without affecting others.
- Each object has its unique counter variable, separate from others.
Introducing Static Variables
- Static variables are shared among all instances of a class.
- When declared as static, no individual copies are created; instead, all objects share the same variable.
- Objects do not own static variables; they belong to the class itself.
Behavior of Static Variables
- Static variables are referred to as class variables.
- Only the class can act on static variables; objects cannot modify them.
- Constants can also be declared as static, belonging to the class exclusively.
Utility of Static Methods
This part delves into the significance of creating constant static variables and methods in programming scenarios where object interaction is unnecessary.
Significance of Static Methods
- Static methods allow actions exclusive to the class without object instantiation.
- Examples include mathematical operations like finding powers or square roots.
- Objects do not interact with static methods; only the class accesses them directly.
Eliminating Object Dependency
- Certain scenarios render object interaction with methods or constants impractical.
- Using objects for tasks like mathematical calculations complicates processes unnecessarily.
- Some classes inherently require static properties due to their nature.
Accessing Constant Static Variables
Accessing constant static variables ensures uniformity across all instances within a class by restricting modifications to maintain consistency.
Utilizing Constant Pi Example
- The value of pi serves as a constant static variable accessible only through the class itself.
Creating Static Variables and Methods
In this section, the speaker explains the process of creating static variables and methods in a class, emphasizing encapsulation and access control.
Understanding Static Variables
- The speaker introduces the concept of static variables, highlighting that they belong to the class itself and are shared among all objects created from that class.
- The order of modifiers for a static variable is discussed, emphasizing placing "static" before other modifiers like "private" for convention.
Implementing Static Methods
- Static methods are explained as functions that can access static variables and are demonstrated to increment the value of a static variable within a class.
- The importance of static variables in scenarios like creating a general counter for objects is highlighted, showcasing how each object creation increments the shared variable.
Accessing Static Variables Through Methods
This section delves into accessing static variables through methods to maintain encapsulation while enabling external access when necessary.
Encapsulation Principles
- Encapsulation principles are reiterated, emphasizing the practice of keeping data private and accessing it through designated methods.
Creating Access Methods
- A method to access the value stored in a static variable is created within the program, ensuring it is also marked as static for external accessibility.
- The method designed to return the value of the object counter variable is named and implemented following standard syntax conventions.
Importance of Method Design
- The significance of having static methods to access static variables efficiently is underscored, simplifying code structure and usage within classes.
Understanding Classes and Objects
In this section, the speaker explains the concept of classes and objects in programming, using a specific example to demonstrate their implementation.
Explaining Class Name and Object Creation
- The speaker clarifies that a class name refers to a type name in programming.
- Demonstrates creating an object named "counter" based on the class.
Multiple Object Instances
- Discusses how the value stored in the "counter" object variable increases with each new object created.
- Illustrates creating multiple instances of objects from the same class.
Static Variables and Constants
- Introduces static variables and methods created by the user.
- Explains constants as static by default in Visual Studio without needing additional modifiers.
Accessing Constants
- Shows accessing constants through the class name rather than instances.
- Highlights errors when trying to access constants using instances instead of directly through the class name.