C# Properties

DiversifiedConstructivism avatar
DiversifiedConstructivism
·
·
Download

Start Quiz

Study Flashcards

10 Questions

What is the purpose of declaring a field as static in a class?

To share a single variable among all members of the class

What is the difference between a static field and a non-static field?

A static field is shared by all members, while a non-static field is specific to each instance

What is the purpose of declaring a method as static in a class?

To make the method independent of a particular object

What is the correct way to call a static method?

Using the class name

What is the purpose of declaring a field as const in a class?

To ensure that the field cannot be changed

What is the difference between a static method and a non-static method?

A static method is independent of a particular object, while a non-static method is not

Can a method parameter be declared as static?

No, it is not possible to declare a method parameter as static

What is the purpose of the Main method in a C# program?

To serve as the starting point for the program

What is the difference between a static variable and a non-static variable?

A static variable is shared by all members, while a non-static variable is specific to each instance

Why are static methods similar to functions in a procedural language?

Because they operate on values passed as arguments, without needing any data from the class

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) { ... }

Understanding C# properties, get and set accessors, and their usage in programming. Learn about read-only and write-only properties in C#.

Make Your Own Quizzes and Flashcards

Convert your notes into interactive study material.

Get started for free

More Quizzes Like This

Creating Menus in Visual Studio
12 questions
Programación en C# con Arrays
8 questions
C# Exception Handling
24 questions

C# Exception Handling

CompactPine8388 avatar
CompactPine8388
C# Files and Data Hierarchy
20 questions
Use Quizgecko on...
Browser
Browser