C# 2 Flashcards: Struct vs Class
17 Questions
100 Views

C# 2 Flashcards: Struct vs Class

Created by
@LavishDiopside625

Questions and Answers

What are some differences between a struct and a class? (Select all that apply)

  • Classes are reference types. (correct)
  • Structs are reference types.
  • Structs cannot be inherited. (correct)
  • All struct types implicitly inherit from System.Object.
  • How can we create an array with non-default values?

    Using the Enumerable.Repeat method.

    What is the difference between the 'is' and 'as' operators in C#?

    'is' checks compatibility and returns boolean; 'as' casts an object to a type.

    What are multicast delegates?

    <p>A delegate with multiple handlers assigned to it.</p> Signup and view all the answers

    What are indexers?

    <p>Smart arrays that allow class instances to be indexed.</p> Signup and view all the answers

    What is the difference between 'throw' and 'throw ex'?

    <p>'throw' preserves the original stack; 'throw ex' does not.</p> Signup and view all the answers

    What are C# attributes and their significance?

    <p>They associate declarative information with C# code.</p> Signup and view all the answers

    Is C# code managed or unmanaged?

    <p>True</p> Signup and view all the answers

    What are some characteristics of C#? (Select all that apply)

    <p>Object-oriented</p> Signup and view all the answers

    Can a class inherit from multiple interfaces?

    <p>True</p> Signup and view all the answers

    Define scope?

    <p>The region of code where a variable may be accessed.</p> Signup and view all the answers

    What is the difference between public, static, and void?

    <p>Public allows access; static is globally accessible; void means no return value.</p> Signup and view all the answers

    What are some examples of modifiers in C#? (Select all that apply)

    <p>public</p> Signup and view all the answers

    What access modifiers are available in C#? (Select all that apply)

    <p>public</p> Signup and view all the answers

    What are the different classifications for arrays in C#? (Select all that apply)

    <p>Single-dimensional</p> Signup and view all the answers

    What is the difference between an object and an instance?

    <p>An instance of a user-defined type is called an object.</p> Signup and view all the answers

    What is a class destructor?

    <p>A destructor is called when a class object goes out of scope or is deleted.</p> Signup and view all the answers

    Study Notes

    Differences Between Structs and Classes

    • Classes are reference types; structs are value types.
    • Structs are stored on the stack, leading to faster retrieval but potential overhead.
    • Structs do not support inheritance; they inherit implicitly from System.ValueType.
    • Assigning a struct to another variable copies its data; changes in one do not affect the other.
    • Structs have default values based on their fields: value types to their defaults, reference types to null.
    • Boxing and unboxing convert between struct types and objects.
    • The meaning of "this" differs; instance field declarations in structs cannot include initializers.
    • Structs lack a parameterless constructor and destructors.
    • Best suited for small, immutable data structures.

    Creating Arrays with Non-default Values

    • Arrays can be initialized with non-default values using the Enumerable.Repeat method.

    Difference Between "is" and "as" Operators

    • "is" checks if an object is compatible with a specified type, returning a boolean result.
    • "as" attempts to cast an object to a specified type, returning null if the cast fails.

    Multicast Delegates

    • A multicast delegate can have multiple handlers assigned, each associated with a method.

    Indexers

    • Indexers, or smart arrays, allow class instances to be accessed like arrays.

    Difference Between "throw" and "throw ex"

    • "throw" maintains the original error stack trace; "throw ex" resets it to the throw point.

    C# Attributes

    • Attributes are a way to attach declarative information to C# code elements, which can be queried at runtime.
    • Common uses include associating help documentation and value editors in GUIs.

    Managed vs Unmanaged Code

    • C# is considered managed code since the CLR (Common Language Runtime) compiles it into Intermediate Language (IL).

    Characteristics of C#

    • C# is known for being simple, type-safe, flexible, object-oriented, compatible, consistent, interoperable, and modern.

    Inheritance of Interfaces

    • A class in C# can inherit multiple interfaces, allowing for versatile design.

    Definition of Scope

    • Scope refers to the part of the code where a variable can be accessed.

    Difference Between Public, Static, and Void

    • Public: Method accessible by anyone.
    • Static: Method can be called globally without an instance of the class.
    • Void: Indicates that a method does not return a value.

    Examples of Modifiers in C#

    • Common modifiers include abstract, sealed, virtual, const, event, extern, override, readonly, static, and new.

    Access Modifiers Available in C#

    • Available access modifiers include public, protected, private, internal, and internal protected.

    Classifications of Arrays in C#

    • Arrays can be classified as single-dimensional, multi-dimensional, or jagged arrays.

    Difference Between Object and Instance

    • An object is an instance of a user-defined type; multiple objects can be created from a single class.

    Class Destructor

    • A destructor is invoked when a class object goes out of scope or is explicitly deleted, used for resource cleanup.

    Studying That Suits You

    Use AI to generate personalized quizzes and flashcards to suit your learning preferences.

    Quiz Team

    Description

    Explore the key differences between structs and classes in C#. This quiz covers fundamental concepts like memory management and inheritance, providing clear definitions to enhance your understanding of these two essential data structures.

    More Quizzes Like This

    Struct Variables in C Programming
    5 questions

    Struct Variables in C Programming

    ThoughtfulNovaculite7717 avatar
    ThoughtfulNovaculite7717
    C Struct Quiz
    10 questions

    C Struct Quiz

    PoignantTan avatar
    PoignantTan
    C Struct and Variable Declaration Quiz
    10 questions
    Key Vocabulary Related to 'Struct'
    11 questions
    Use Quizgecko on...
    Browser
    Browser