Podcast Beta
Questions and Answers
What is the purpose of declaring a field as static in a class?
What is the difference between a static field and a non-static field?
What is the purpose of declaring a method as static in a class?
What is the correct way to call a static method?
Signup and view all the answers
What is the purpose of declaring a field as const in a class?
Signup and view all the answers
What is the difference between a static method and a non-static method?
Signup and view all the answers
Can a method parameter be declared as static?
Signup and view all the answers
What is the purpose of the Main method in a C# program?
Signup and view all the answers
What is the difference between a static variable and a non-static variable?
Signup and view all the answers
Why are static methods similar to functions in a procedural language?
Signup and view all the answers
Study Notes
Properties
- A property is a member that provides a flexible mechanism to read, write, or compute the value of a private field.
- Properties can be read-write, read-only, or write-only.
- The set accessor takes a single implicit parameter named value but does not return any value.
- Example:
public int Age { set { age = value; } get { return age; }
Read-only and Write-Only Properties
- get or set can be omitted to create a read-only or write-only property.
- Example:
public long Balance { get { return balance; } }
Benefits of Properties
- Allow read-only and write-only fields.
- Can validate a field when it is accessed.
- Can substitute for fields in interfaces.
Static Members
- Static members are accessed by the class name, not the instance name.
- Non-static class should also define a static constructor if the class contains static members.
Static Constructors
- A static constructor is a special constructor that gets called before the first object of the class is created.
- It is used to initialize any static data, or to perform a particular action that needs performed once only.
- Rules for static constructors:
- A class can have only one static constructor (no constructor overloading)
- Static constructor can not have any parameter
- Static constructor can not have any access specifier
- Example:
static class T { static int id; static T() { id = 0; } }
Static Constructor in Non-Static Class
- The compiler will provide the default parameterless constructor.
- A static constructor needs its class’ fields and properties to be static also to be able to initialize them.
Readonly Fields
- The readonly keyword is a modifier that can be used on fields.
- When a field declaration includes a readonly modifier, assignments to the fields introduced by the declaration can only occur as part of the declaration or in a constructor in the same class.
Static Fields
- Static fields are shared by all members of a class and are independent of any instances.
- Declare the field static to make it a static field.
- Non-static field is called an instance field.
- Example:
public static double PI = 3.14159265358979;
Static Methods
- Static methods are similar to functions in a procedural language.
- Static methods don’t need any data from the class and operate on values passed as arguments.
- Method parameters/variables can’t be static.
- Example:
public static double Sqrt(double d) { ... }
Studying That Suits You
Use AI to generate personalized quizzes and flashcards to suit your learning preferences.
Description
Understanding C# properties, get and set accessors, and their usage in programming. Learn about read-only and write-only properties in C#.