C#

¡Supera tus tareas y exámenes ahora con Quizwiz!

What is the difference between constant and readonly in C#?

A const keyword in C# is used to declare a constant field throughout the program. That means once a variable has been declared const, its value cannot be changed throughout the program. In C#, a constant is a number, string, null reference, or boolean values. On the other hand, with readonly keyword, you can assign the variable only when it is declared or in a constructor of the same class in which it is declared. Constants are static by default while readonly should have a value assigned when the constructor is declared. Constants can be declared within functions while readonly modifiers can be used with reference types.

Difference between the Equality Operator (==) and Equals() Method in C#?

Although both are used to compare two objects by value, still they both are used differently. Equality operator (==) is a reference type which means that if equality operator is used, it will return true only if both the references point to the same object. Equals() method: Equals method is used to compare the values carried by the objects. int x=10, int y=10. If x==y is compared then, the values carried by x and y are compared which is equal and therefore they return true. Equality operator: Compares by reference Equals(): Compares by value

What is the difference between an Array and ArrayList in C#?

An array is a collection of similar variables clubbed together under one common name. While ArrayList is a collection of objects that can be indexed individually. With ArrayList you can access a number of features like dynamic memory allocation, adding, searching, and sorting items in the ArrayList. When declaring an array the size of the items is fixed therefore, the memory allocation is fixed. But with ArrayList, it can be increased or decreased dynamically.Array belongs to system.array namespace while ArrayList belongs to the system.collection namespace.All items in an array are of the same datatype while all the items in an ArrayList can be of the same or different data types.While arrays cannot accept null, ArrayList can accept null values.

What are the differences between ref and out keywords?

C# ref keywords pass arguments by reference and not value. To use the 'ref' keyword, you need to explicitly mention 'ref'. C# out keywords pass arguments within methods and functions. 'out' keyword is used to pass arguments in a method as a reference to return multiple values. Although it is the same as the ref keyword, the ref keyword needs to be initialised before it is passed. Here, The out and ref keywords are useful when we want to return a value in the same variables that are passed as an argument.

What is Common Language Runtime (CLR)?

CLR handles program execution for various languages including C#. The architecture of CLR handles memory management, garbage collection, and security handling.

What are the types of classes in C#?

Class is an entity that encapsulates all the properties of its objects and instances as a single unit. C# has four types of such classes: Static class: Static class, defined by the keyword 'static' does not allow inheritance. Therefore, you cannot create an object for a static class. Partial class: Partial class, defined by the keyword 'partial' allows its members to partially divide or share source (.cs) files. Abstract class: Abstract classes are classes that cannot be instantiated where you cannot create objects. Abstract classes work on the OOPS concept of abstraction. Abstraction helps to extract essential details and hide the unessential ones. Sealed class: Sealed classes are classes that cannot be inherited. Use the keyword sealed to restrict access to users to inherit that class.

What are extension methods in C#?

Extension methods help to add new methods to the existing ones. The methods that are added are static. At times, when you want to add methods to an existing class but don't perceive the right to modify that class or don't hold the rights, you can create a new static class containing the new methods. Once the extended methods are declared, bind this class with the existing one and see the methods will be added to the existing one.

What is garbage collection in C#?

Garbage collection is the process of freeing up memory that is captured by unwanted objects. When you create a class object, automatically some memory space is allocated to the object in the heap memory. Now, after you perform all the actions on the object, the memory space occupied by the object becomes waste. It is necessary to free up memory. Garbage collection happens in three cases: If the occupied memory by the objects exceeds the pre-set threshold value. If the garbage collection method is called If your system has low physical memory

What are Generics in C#?

In C# collections, defining any kind of object is termed okay which compromises C#'s basic rule of type-safety. Therefore, generics were included to type-safe the code by allowing re-use of the data processing algorithms. Generics in C# mean not linked to any specific data type. Generics reduce the load of using boxing, unboxing, and typecasting objects. Generics are always defined inside angular brackets <>.

What are Indexers in C#?

Indexers are called smart arrays that allow access to a member variable. Indexers allow member variables using the features of an array. They are created using the Indexer keyword. Indexers are not static members.

What is inheritance? Does C# support multiple inheritance?

Inheritance means acquiring some of the properties from a master class. C# doesn't support multiple inheritances. Instead, you can use interfaces to inherit the properties using the class name in the signature.

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

Late binding and early binding are examples of one of the primary concepts of OOPS: Polymorphism. For ex: one function calculateBill() will calculate bills of premium customers, basic customers, and semi-premium customers based on their policies differently. The calculation for all the customer objects is done differently using the same function which is called polymorphism. When an object is assigned to an object variable in C#, the .NET framework performs the binding. When the binding function happens at compile-time, it is called early binding. It investigates and checks the methods and properties of the static objects. With early binding, the number of run-time errors decreases substantially and it executes pretty quickly. But when the binding happens at runtime, it is called late binding. Late binding happens when the objects are dynamic (decided based on the data they hold) at run-time. It is slower as it looks through during run-time.

What is the difference between an abstract class and an interface?

Let's dig into the differences between an abstract class and an interface: Abstract classes are classes that cannot be instantiated ie. that cannot create an object. The interface is like an abstract class because all the methods inside the interface are abstract methods.Surprisingly, abstract classes can have both abstract and non-abstract methods but all the methods of an interface are abstract methods.Since abstract classes can have both abstract and non-abstract methods, we need to use the Abstract keyword to declare abstract methods. But in the interface, there is no such need. An abstract class has constructors while an interface encompasses none.

What is a managed and unmanaged code?

Managed code lets you run the code on a managed CLR runtime environment in the .NET framework. Managed code runs on the managed runtime environment than the operating system itself. Benefits: Provides various services like a garbage collector, exception handling, etc. Unmanaged code is when the code doesn't run on CLR, it is an unmanaged code that works outside the .NET framework. They don't provide services of the high-level languages and therefore, run without them. Such an example is C++.

What are the different ways in which a method can be Overloaded in C#?

Overloading means when a method has the same name but carries different values to use in a different context. Only the main() method cannot be overloaded. In order to overload methods in C#, Change the number of parameters in a method, orChange the order of parameters in a method, orUse different data types for parameters In these ways, you can overload a method multiple times.

What are partial classes in C#?

Partial classes implement the functionality of a single class into multiple files. These multiple files are combined into one during compile time. The partial class can be created using the partial keyword. You can easily split the functionalities of methods, interfaces, or structures into multiple files. You can even add nested partial classes.

What are Properties in C#?

Properties in C# are public members of a class where they provide the ability to access private members of a class. The basic principle of encapsulation lets you hide some sensitive properties from the users by making the variables private. The private members are not accessible otherwise in a class. Therefore, by using properties in C# you can easily access the private members and set their values. The values can be easily assigned using get and set methods, also known as accessors. While the get method extracts the value, the set method assigns the value to the variables.

What is Reflection in C#?

Reflection in C# extracts metadata from the datatypes during runtime. To add reflection in the .NET framework, simply use System.Refelction namespace in your program to retrieve the type which can be anything from: Assembly Module Enum MethodInfo ConstructorInfo MemberInfo ParameterInfo Type FieldInfo EventInfo PropertyInfo

What is the difference between String and StringBuilder in C#?

The major difference between String and StringBuilder is that String objects are immutable while StringBuilder creates a mutable string of characters. StringBuilder will make the changes to the existing object rather than creating a new object. StringBuilder simplifies the entire process of making changes to the existing string object. Since the String class is immutable, it is costlier to create a new object every time we need to make a change. So, the StringBuilder class comes into picture which can be evoked using the System.Text namespace. In case, a string object will not change throughout the entire program, then use String class or else StringBuilder.

What is Boxing and Unboxing in C#?

The two functions are used for typecasting the data types: Boxing: Boxing converts value type (int, char, etc.) to reference type (object) which is an implicit conversion process using object value. Unboxing: Unboxing converts reference type (object) to value type (int, char, etc.) using an explicit conversion process.

What are the Arrays in C#?

When a group of similar elements is clubbed together under one name, they are called arrays. For ex. An array of tea Atea[4]: [green tea, chamomile tea, black tea, lemon tea]. The length of the array defines how many elements are present in the array. In C#, the memory allocations for the elements of the array happen dynamically. This is how values are stored in an array sequentially. A few pointers for arrays in C#: The memory allocation is DYNAMIC.Arrays in C# are treated as objects.The length of the array is easy to find by detecting the number of members in the array.The members in the array are ordered and begin with the index value=0.The array types are reference types derived from the base array type.

How is C# different from C?

You would always know C being the procedural language while C# is a more object-oriented language. The biggest difference is that C# supports automatic garbage collection by Common Language Runtime (CLR) while C does not. C# primarily needs a .NET framework to execute while C is a platform-agnostic language.


Conjuntos de estudio relacionados

Marketing Management Chapter Eleven

View Set

Chapter 3: Investment Companies and Life Insurance Products

View Set

Managerial Accounting - Chapter 2

View Set

BYU IS Physics 123 Final Exam Review

View Set

Chapter 28 --> Head and Spine Injuries

View Set