C-Sharp

Lakukan tugas rumah & ujian kamu dengan baik sekarang menggunakan Quizwiz!

Keyword- "As a result"

- 'as' is a keyword used for conversion from one type to another. The type can be a reference or nullable. - 'as' keyword checks the compatibility of one object type with another object type. In case of compatibility, it will return the value of the new object type otherwise, null will be returned. - Iterm-77f the conversion from one type to another type fails, then it will return a null value instead of raising an exception. So, the return value can be null also.

Keyword IS

1) 'is' keyword checks whether the conversion from one object type to another object type is compatible or not. 2) It returns true if the conversion is compatible, else returns false. 3) The syntax of 'is' keyword is,

Class in C#?

A Class is like an object constructor, or a "blueprint" for creating objects. Instances of the class are known as objects.

Sealed Class ?

A Class that cannot be Inherited

Constructor in c#

A constructor is a special method that is used to initialize objects. Constructor called when an object of a class is created. It can be used to set initial values for fields:

How are custom controls added to an application in C#?

A custom control is designed for single use in a specific application. There are three main ways to create a new custom control: Derive it from an existing user control Group existing controls together into new compiled control Create a new control by deriving from the System.Windows.Controls.Control class

Delegats

A delegate is a type that holds references to methods with a particular parameter list and return type. Invoke method at a runtime

Fields and Properties in C#?

A field is a variable of any type that is declared directly in a class or struct. A property is a class member that provides a mechanism to read, write, and compute the value of a private field.

Static keyword

A static class can only contain static data members, static methods, and a static constructor. It is not allowed to create objects of the static class. When a variable is declared as static, then a single copy of the variable is created and shared among all objects at the class level. Static variables are accessed with the name of the class, they do not require any object for access.

String and StringBuilder in C#?

A string object is immutable, meaning that it cannot be changed after it's created. Any operation that tries to modify the string object will simply create a new string object. StringBuilder class can be used when you want to modify a string without creating a new object.

Anonymous type in C#?

Allows to create new types without defining them. When reed-only property needed in a single object without defining each type. var anonymous_object = new { FirstName = "John", Age = 35 };

What are the key differences between Array and ArrayList in C#?

An ArrayList has wider usage than an Array. The key differences include: An Array is strongly-typed, meaning it only stores the same type of data. An ArrayList is a non-generic collection type, meaning it can store multiple types of data An Array stores a fixed number of elements. An ArrayList features a variable number of elements and can continually be added to An Array cannot accept null values, whereas an ArrayList can The relative simplicity of an Array means it typically provides better performance than an ArrayList

Enum in C#?

An enum is a special "class" that represents a group of constants (unchangeable/read-only variables). enum Level { Low, Medium, High }

Event handler in C#?

An event handler, in C#, is a method that contains the code that gets executed in response to a specific event that occurs in an application.

What is anonymous function

Anonymous functions in C# are often created using lambda expressions or delegate expressions, which allow you to define functions without explicitly naming them.

Code compilation in C#?

C# code is compiled into IL (Intermediate Language) when you build your project. The IL is saved in a file on disk. When you run your program, the IL is compiled again, using the Just In Time (JIT) compiler (a process often called JIT'ing). The result is machine code, executed by the machine's processor.

What is an object in C#?

In C#, Object is a real world entity, for example, chair, car, pen, mobile, laptop etc. In other words, object is an entity that has state and behavior. Here, state means data and behavior means functionality. Object is a runtime entity, it is created at runtime.

What Key principles and concepts of Object-Oriented Programming include:

Classes: - blueprint or template for creating objects. Objects: - Objects are instances of classes. They represent real-world entities Encapsulation: - bundling data (attributes) and the methods that operate on that data into a single unit (a class). Inheritance: - Inheritance allows you to create a new class (a derived or subclass) based Polymorphism: - Polymorphism allows objects of different classes to be treated as objects of a common base class. Abstraction: - Abstraction is the process of simplifying complex reality

CopyTo() vs Clone()

Clone creates new object, takes all content, faster. Copy does copy it to existing array, copying from given index to the end.

Constant in C#?

Constants are fixed values that cannot be altered during the lifetime of the program. For example, the constant 'Months' is always 12 and cannot be changed.

Continue / break statements

Continue statement breaks one iteration and Break takes out of the loop.

Control statements in C#?

Control statements are used to control the actions a program takes; this is sometimes referred to as the flow of execution. Common actions in C# include calling methods, assigning values, declaring variables, and looping through collections.

How are the different types of control statements used in C#?

Each type of control statement has its own set of syntax used to invoke the statement: Selection statements include <if>, <else>, <switch>, and <case> Iteration statements include <do>, <for>, <foreach>, and <while> Jump statements include <break>, <continue>, <return>, and <goto>

How are extension methods used in C#?

Extension methods allow developers to add a method to existing types without changing the original source code. This allows them to extend the functionality of the method. An extension method is a static method and uses the <this> keyword.

File Handling in C#.Net?

File handling is the process of saving information to the disk for external storage. The saved file contains bytes of data and is available for retrieval at a later date.

Finally vs Finalize block

Finally is a block part of exception handling. Finalize is a method called right before GC. Called automatically or explicitly in CODE.

Record in C#?

For the small amount of data and immutable. A record in C# is a class or struct that provides special syntax and behavior for working with data models.

How is HashSet used in C#?

In C#, HashSet is an unordered collection of distinct values. Generally, it is used to prevent duplicate elements from being placed in a collection, and it performs better than a list in achieving this goal. It is implemented using the HashSet class, which is derived from the System.

What is a destructor in C#?

In C#, a (~ destructor ) is a type of method. The main purpose of a destructor is to destroy instances of a class when they are no longer needed in order to free up memory. Destructors are also referred to as finalizers.

Method in C#?

In C#, a method is a block of code to perform particular operations. Methods must be declared within a class or a structure. They help save time by reusing code.

Struckt in C#?

In C#, a structure is a value type data type. It helps you to make a single variable hold related data of various data types. The struct keyword is used for creating a structure. Structures are used to represent a record.

What is an array in C#?

In C#, an array stores a fixed collection of the same data type in a single variable. Arrays can be retrieved easily for the developer's reference. - Single Dimensional Arrays. - Multidimensional Arrays. - Jagged Arrays.

Boxing and unboxing in C#?

In C#, boxing and unboxing allow developers to convert .NET data types from reference type to value type and vice versa. Unboxing is used to convert a reference type to a value type, while boxing is used to convert a value type to a reference type. These two processes underpin the unified view of C#.

How can circular references be fixed in C#?

In C#, circular references are most commonly resolved using garbage collection. The garbage collector systematically detects and collects circular references. Other solutions for circular references issues include callback methods, event handlers, and dependency injection.

How is exception handling performed in C#?

In C#, exception handling helps detect errors in code at runtime. The process is implemented using four different keywords: <Try> identifies blocks of code where exceptions are activated <Catch> catches the exceptions that have been identified by <Try> <Finally> executes a given set of statements depending on whether an exception is thrown out or not <Throw> removes the exception

Garbage collection in C#?

In C#, garbage collection is the process of managing memory in an application. The garbage collector automatically disposes of memory that is no longer used to make memory available for new allocations.

Advantages of generics in C#?

In C#, generics allow the developer to define classes and methods which can be used with any data type. This brings several benefits: Saves time by reusing code Provides type safety without unnecessary overhead Removes the need for boxing and unboxing Generic collection types generally perform better with value types because there is no need to box the values

What is an indexer in C#?

In C#, indexers are used to index instances of a class or structure. The indexed values can then be easily accessed like an array, but without explicitly specifying a type or instance member.

What is method overloading in C#?

In C#, method overloading is the process of assigning different signatures or arguments to two or more methods bearing the same name. It's an example of polymorphism in object-oriented programming. Method overloading improves the readability of the program by reducing the number of names associated with a specific action. These methods have the same name but accept different parameters. 1. By changing the Number of Parameters 2. By changing the Data types of the parameters 3. By changing the Order of the parameters

When is method overriding used in C#?

In C#, method overriding is used to invoke functions that belong to different classes. This process creates a method in the derived class with the same signature as a method in the base class without modifying the code of the base class. This helps achieve runtime polymorphism.

When should nullable types be used in C#?

In C#, nullable types are used to represent an undefined value of an underlying type. It essentially means 'no value' and is generally used when no data is available for the field.

How is serialization implemented in C#?

In C#, serialization is the process of converting an object into a stream of bytes for storage on a memory, database, or file. This allows the developer to save the state of an object for future reference. Serialization can be performed by applying <SerializableAttribute> to a type to indicate that instances of this type can be serialized. All public and private fields marked as such are then serialized by default.

How are user controls created in C#?

In C#, user controls allow developers to write code that can be used in various areas of the program. For example, if a website requires the same search control in multiple places, it can be created once as a user control and then dropped into different areas of the code. This serves the dual purposes of reusability and bug prevention.

Interface in C#?

Interface is like an abstract base class with only abstract members. A class or struct that implements the interface must implement all its members. Contract.

Partial Class

It can break the functionality of a single class into many files.

What is a Lambda Expression in C#?

It is a way to represent anonymous function. Many LINQ methods take a function (called a delegate) as a parameter. Uses for writing LINQ expressions.

LINQ (C#)

Language-Integrated Query (LINQ) LINQ in C# is used to work with data access from sources such as objects, data sets, SQL Server, and XML. Methods: Where, Select, Any, All, GroupBy, and FirstOrDefault, Last, SingleOrDefault, OrderBy

LINQ in C#?

Language-Integrated Query (LINQ) is a powerful tool using C# language similar to SQL query. (LINQ to Objects), relational databases (LINQ to SQL), and XML (LINQ to XML). LINQ is a structured to retrieve data from different types of data sources such as collections, ADO.Net DataSet, XML Docs, web service and MS SQL Server and other databases.

jagged array

Like a nested array. Each element of array contains an array itself. Could contain different sizes and dimensions.

What is the difference between managed and unmanaged code?

Managed code is executed by the Common Language Runtime (CLR) of the .NET Framework, whereas unmanaged code is executed by the Operating System (OS). CLR offers inbuilt security to managed code, whereas it's the developer's responsibility to write safe and secure code with unmanaged code.

What are the different techniques for overloading a method in C#?

Method overloading can be achieved in the three following ways: 1). By using different types of data for parameters in a method 2). By changing the number of parameters in a method 3). By changing the order of parameters in a method

When should multithreading be used and when should it be avoided in C#?

Multithreading, or threading, can be a good way to improve the performance of a program where several operations run simultaneously. It allows distinct threads to run at their own time, rather than having to wait for the previous step to be complete. This has the potential to speed up a program. However, multithreading is not advisable when much of the program's processes are interdependent. For example, if Step B was reliant on the prior completion of Step A, multithreading would lead to performance issues and create bugs in the program. As a program grows more complex, threading becomes a more delicate operation.

What is OOP?

OOP, or Object-Oriented Programming, is a programming paradigm that uses objects and classes for organizing and structuring code. It's based on the concept of "objects," which are instances of classes, and it promotes the organization of code around data (attributes) and the methods (functions) that operate on that data.

Types of Delegates

Single delegate: invoke single method Multicast delegate: invoke multiple methods using + or - operator Generic delegate: no need for the instance Action, Func, Predicate

Custom exceptions

Sometimes you want raise an exception when the business rule of your application gets violated.

Single Responsibility Principle (SRP):

Principle: A class should have only one reason to change. Explanation: This principle states that a class should have a single responsibility or job within the system. It should encapsulate one and only one part of the software's functionality. This makes classes easier to understand, maintain, and change because they have a well-defined purpose.

Interface Segregation Principle (ISP):

Principle: Clients should not be forced to depend on interfaces they do not use. Explanation: The ISP suggests that it's better to have many small, specific interfaces rather than one large, general-purpose interface. This prevents classes from being burdened with methods they don't need and promotes a more modular and focused design.

Dependency Inversion Principle (DIP):

Principle: High-level modules should not depend on low-level modules. Both should depend on abstractions. Abstractions should not depend on details; details should depend on abstractions. Explanation: The DIP encourages decoupling between high-level and low-level modules by introducing abstractions (interfaces or abstract classes) that both depend on. This allows for flexibility in changing or extending components without affecting other parts of the system. It also promotes the use of dependency injection to provide concrete implementations.

Open/Closed Principle (OCP):

Principle: Software entities (classes, modules, functions) should be open for extension but closed for modification. Explanation: The OCP encourages developers to design code in a way that allows new functionality to be added without changing existing code. This is typically achieved through interfaces, abstract classes, and polymorphism. It promotes code stability and reduces the risk of introducing bugs when making changes.

Liskov Substitution Principle (LSP)

Principle: Subtypes must be substitutable for their base types without altering the correctness of the program. Explanation: This principle ensures that derived classes (subtypes) can be used interchangeably with their base classes without causing unexpected behavior. It emphasizes that inheritance should not violate the behavior expected from the base class. Violating this principle can lead to incorrect program behavior.

How can a class be set to be inherited without overriding the method in C#?

Provided that the method isn't virtual, it won't be overridden. However, if the class is inheriting from a base class that contains a virtual member function, you can use the <sealed> modifier to avoid further overriding that member function.

Access modifiers

Public: full access Protected-internal: same assembly & derived Protected: derived classes Internal: same assembly Private-protected: derived same assembly Private: only in the class

Reflection in C#?

Reflection in C# is used to retrieve metadata on types at runtime. In other words, you can use reflection to inspect metadata of the types in your program dynamically -- you can retrieve information on the loaded assemblies and the types defined in them. Following info could be retreated: - Assembly name, - Class name, - Method name, - Object type, - Identifies properties and methods

How is reflection used in C#?

Reflection provides objects (of type Type) that describe assemblies, modules, and types. You can use reflection to dynamically create an instance of a type, bind the type to an existing object, or get the type from an existing object and invoke its methods or access its fields and properties.

Ref and out keywords in C#?

The <ref> and <out> keywords are similar in that they are both used to pass arguments in a reference or function. However, there is a subtle difference: With <ref> keywords, the value is already set, meaning the method can read and modify it With <out> keywords, the value isn't set and can't be read by the method until it is set, meaning the method must set it before it can be returned

Dependency injection in C#?

The Dependency Injection Design Pattern in C# is a process in which we are injecting the object of a class into a class that depends on that object. The Dependency Injection design pattern is the most commonly used design pattern nowadays to remove the dependencies between the objects.

Value types and reference types

The built-in reference types supported by C# include: strings, arrays, objects of classes, etc., dynamic and located in managed Heap. All numeric types, Boolean, Date, structs, and enums are examples of value types. Located in stock.

Four fundamental concepts of object-oriented programming?

The four fundamental concepts of object-oriented programming can be explained as follows: - Encapsulation is the bundling of data, including the methods that operate on that data, into a single, private unit (class, a record, or a struct) AND a mechanism of hiding the internal details (implementation) of an object from other objects and the outside world. - Polymorphism is the ability of a type to take on many forms. - Abstraction: The word abstract means a concept or an idea not associated with any specific instance. - Inheritance is the process where one class derives (or inherits) data members and methods from Parent class

What is the difference between a throw exception and a throw clause in C#?

The fundamental difference is that throw exceptions overwrite the stack trace, whereas throw clauses retain the stack information. As such, it is much harder to retrieve the original code responsible for throwing the exception with throw exceptions.

In keyword

The in keyword causes arguments to be passed by reference but ensures the argument is not modified. It makes the formal parameter an alias for the argument, which must be a variable. In other words, any operation on the parameter is made on the argument.

What is the difference between late binding and early binding in C#?

The key differences between early binding and late binding are: Early binding occurs at compile-time, whereas late binding occurs at runtime Early binding uses class information to resolve method calling, whereas late binding uses the object to resolve method calling Typically, the performance of late binding is slower than early binding because it occurs at runtime

Namespace

The namespace keyword is used to declare a scope that contains a set of related objects. And to organize them.

Read-only in C#?

The readonly keyword can be used to define a variable or an object as readable only. This means that the variable or object can be assigned a value at the class scope or in a constructor only. Can't change.

How is the singleton design pattern implemented in C#?

The singleton design pattern ensures that only one object of its kind exists, and provides global access to it for any other code. This design pattern can be implemented in a number of ways, using: Thread-safety singleton Thread-safety singleton using double-check locking No thread-safe singleton Thread-safe without a lock .NET 4's Lazy<T> type

Using statement in C#?

The using statement provides correct use of IDisposable objects. Within the using block, the object is read-only and can't be modified or reassigned.

Disadvantages of generics in C#?

There are a few limitations with generics. These include: They cannot be used with enumerations They cannot be used with lightweight dynamic methods The .NET framework doesn't support context-bound generic types

Different types of classes in C#?

There are generally considered to be four types of classes in C#. These include: 1. Abstract classes: These provide a common definition for a base class that other classes can be derived from 2. Static classes: These contain static items that can only interact with other static items 3. Partial classes: These are portions of a class that a compiler can combine to form a complete class 4. Sealed classes: These cannot be inherited by any class but can be instantiated

What is the difference between Const and ReadOnly keywords in C#?

There are several differences between Const and ReadOnly keywords in C#. These include: ReadOnly is a constant used at runtime, whereas Const is a constant used at compile-time ReadOnly values can be changed, whereas Const values cannot be changed ReadOnly cannot be declared inside the method, whereas Const can

Throw vs Throw ex?

Throw it provides information about from where the exception was thrown and also about the actual exception while throw ex provides information only about from where the exception was thrown.

What is a multicast delegate in C#?

Unlike a simple delegate, a multicast delegate in C# references multiple target methods. When a multicast delegate is used, all the functions the delegate is pointing to are invoked. They're implemented using the MulticastDelegate class, which is derived from the system.

Await and Async in c#

Use return type Task when nothing is returned. Use return type Task<T> when returning something. Avoid return type "void". (Except with event handlers). Async modifier is used to make a method able to run asynchronously. Await keyword tells a method to wait until the Async Task completes. Result will get results of an Async method synchronously (but has side effects)

Variable in C#

Variables are containers for storing data values. In C#, there are different types of variables (defined with different keywords). Name given to a storage area that our programs can manipulate. 7 categories: 1). static variables 2). instance variables 3). array elements 4). value parameters 5). reference parameters 6). output parameters 7). local variables

Virtual vs Abstract Method in C#?

Virtual method is a method that can be redefined in derived classes with override keyword. An abstract method is basically a virtual method without implementation. Implementation with override keyword. Abstract method declarations are only permitted in abstract classes.

How is code compiled in C#?

When a project is developed, C# source code is compiled into Intermediate Language (IL). IL is a set of instructions that produces a machine code for execution on the machine processor. In four steps, code moves from the preprocessor to the compiler, to the assembler, and, lastly, to the linker.

How to use nullable types C#?

You typically use a nullable value type when you need to represent the undefined value of an underlying value type. For example, a Boolean, or bool, variable can only be either true or false. However, in some applications a variable value can be undefined or missing. For example, a database field may contain true or false, or it may contain no value at all, that is, NULL. You can use the bool? type in that scenario.


Set pelajaran terkait

De 8.1 How did the geography of Africa affect its settlement patterns and commerce?

View Set

Quiz 10 Information Security Fundamentals

View Set

Basic Plans, Specifications, and Color Coding

View Set