Podcast
Questions and Answers
What is the purpose of declaring a field as static in a class?
What is the purpose of declaring a field as static in a class?
- To make the field accessible only through an instance of the class
- To initialize the field with a default value
- To create a single instance of the class
- To share a single variable among all members of the class (correct)
What is the difference between a static field and a non-static field?
What is the difference between a static field and a non-static field?
- A static field is initialized with a default value, while a non-static field is not
- A static field is private, while a non-static field is public
- A static field can be changed, while a non-static field cannot
- A static field is shared by all members, while a non-static field is specific to each instance (correct)
What is the purpose of declaring a method as static in a class?
What is the purpose of declaring a method as static in a class?
- To make the method independent of a particular object (correct)
- To override a method from the base class
- To create a singleton instance of the class
- To make the method accessible only through an instance of the class
What is the correct way to call a static method?
What is the correct way to call a static method?
What is the purpose of declaring a field as const in a class?
What is the purpose of declaring a field as const in a class?
What is the difference between a static method and a non-static method?
What is the difference between a static method and a non-static method?
Can a method parameter be declared as static?
Can a method parameter be declared as static?
What is the purpose of the Main method in a C# program?
What is the purpose of the Main method in a C# program?
What is the difference between a static variable and a non-static variable?
What is the difference between a static variable and a non-static variable?
Why are static methods similar to functions in a procedural language?
Why are static methods similar to functions in a procedural language?
Flashcards
Property
Property
A member that provides a way to read, write, or calculate the value of a private field.
Read-only Property
Read-only Property
A property that can only be read; its value cannot be changed.
Write-only Property
Write-only Property
A property that can only be written to; its value can only be set.
Static Member
Static Member
Signup and view all the flashcards
Static Constructor
Static Constructor
Signup and view all the flashcards
Readonly Field
Readonly Field
Signup and view all the flashcards
Static Field
Static Field
Signup and view all the flashcards
Static Method
Static Method
Signup and view all the flashcards
Static Constructor in Non-Static Class
Static Constructor in Non-Static Class
Signup and view all the flashcards
Instance field
Instance field
Signup and view all the flashcards
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#.