Questions and Answers
What are the parts of a C# program structure?
Namespace declaration, a class, class methods, class attributes, a Main method, statements and expressions, comments.
What is the use of the 'new' keyword in C#?
It is used to create objects and invoke constructors, as well as create instances of anonymous types.
What does the 'sealed' keyword do in C#?
It prevents other classes from inheriting from a sealed class and can also be used on methods to prevent overriding.
What does the 'this' keyword refer to in C#?
Signup and view all the answers
What does the 'static' keyword signify?
Signup and view all the answers
How do you create a string object in C#?
Signup and view all the answers
What properties are available in the String class?
Signup and view all the answers
What is the purpose of a namespace in C#?
Signup and view all the answers
What are exception handling keywords in C#?
Signup and view all the answers
What does the 'try' keyword do?
Signup and view all the answers
What does the 'catch' keyword indicate?
Signup and view all the answers
What is the function of the 'finally' keyword?
Signup and view all the answers
What is the use of the 'throw' keyword?
Signup and view all the answers
What is an abstract class in C#?
Signup and view all the answers
What is an interface in C#?
Signup and view all the answers
What is the purpose of arrays in C#?
Signup and view all the answers
What does declaring an array look like in C#?
Signup and view all the answers
How are values assigned to array elements in C#?
Signup and view all the answers
What is the purpose of collection classes in C#?
Signup and view all the answers
What is a Hashtable in C#?
Signup and view all the answers
What is the definition of a Stack in C#?
Signup and view all the answers
What is a Queue in C#?
Signup and view all the answers
Study Notes
Program Structure
- A C# program includes a namespace declaration, a class, class methods, class attributes, a Main method, statements and expressions, and comments.
"new" Keyword
- The "new" keyword is utilized to create objects, invoke constructors, and create instances of anonymous types.
- It invokes the default constructor for value types.
"sealed" Keyword
- The "sealed" modifier prevents other classes from inheriting from a sealed class.
- Can be applied to methods or properties to prevent overriding of virtual methods in derived classes.
"this" Keyword
- Refers to the current instance of the class and modifies the first parameter of an extension method.
"static" Keyword
- Declares static members that belong to the type itself, not to instances.
- Applicable to classes, fields, methods, properties, operators, events, and constructors (not indexers or finalizers).
Strings
- Strings are essentially arrays of characters but commonly declared using the string keyword, an alias for System.String.
Creating a String Object
- Can be created by assigning a string literal, using a String class constructor, concatenating strings, invoking a method that returns a string, or formatting a value/object to convert to a string.
Properties of the String Class
- Chars: Accesses the Char object at a specified position.
- Length: Returns the number of characters in the string.
Namespace
- Provides a way to separate sets of names to avoid conflicts between classes with identical names.
Using Keyword
- Indicates that the program utilizes names from the specified namespace.
"abstract" Keyword
- Creates incomplete classes or members that must be implemented in derived classes.
Abstract Classes
- Cannot be instantiated and define a common base for multiple derived classes.
- May declare abstract methods that derived classes must implement.
Virtual Method
- Facilitates overriding base class implementations in derived classes for consistent functionality across object sets.
Declaring Interfaces
- Interfaces are declared with the interface keyword and are public by default.
Interface
- Represents a contract that classes must follow, defining 'what' should be implemented, leaving 'how' to the derived classes.
- Contains member declarations without implementations.
Exception
- Represents problems during program execution, such as attempts to divide by zero.
Exception Handling Keywords
- Key terms include try, catch, finally, and throw.
try
- Identifies a block of code where exceptions may occur, followed by catch blocks for handling.
catch
- Handles specific exceptions where they occur, indicated by the catch keyword.
finally
- Executes a set of statements regardless of whether an exception was thrown, ensuring critical operations are completed.
throw
- Used to initiate an exception when an issue arises in the program.
Exception Classes
- C# exceptions are represented by classes derived from System.Exception.
- Includes System.ApplicationException and System.SystemException for application and predefined exceptions, respectively.
Handling Exceptions
- C# employs try and catch blocks for structured exception handling, separating core statements from error-handling.
Throwing Objects
- Objects derived from System.Exception can be thrown in a catch block using the throw statement.
Arrays in C#
- An array is a fixed-size collection of same-type elements, stored in contiguous memory locations, accessed via indices.
Declaring Arrays
- Declared using a datatype followed by [] to specify its rank (size) and name.
Initializing Arrays
- Arrays require the new keyword for instantiation, and are initialized to assign values.
Assigning Values to an Array
- Values can be assigned by index after declaration or during declaration with an initializer list.
Collections in C#
- Specialized classes for data storage and retrieval, supporting stacks, queues, and lists.
- Collection classes allow dynamic memory allocation and indexed access.
Classes of the System.Collection Namespace
- Key classes include ArrayList, HashTable, Stack, and Queue.
ArrayList
- An ordered, indexable collection that allows adding and removing items dynamically with automatic resizing.
HashTable
- Collection of key-value pairs, organized via hash codes for quick access based on keys.
Stack
- Represents a last-in, first-out (LIFO) collection, where adding items is "pushing" and removal is "popping".
Queue
- Represents a first-in, first-out (FIFO) collection, where adding is "enqueue" and removal is "deque".
Studying That Suits You
Use AI to generate personalized quizzes and flashcards to suit your learning preferences.
Description
Test your knowledge of C# programming concepts with these flashcards. Each card covers essential terminology and definitions that are key to understanding C#. Use these flashcards for quick revision or for learning new terms.