.NET Interview Prep

Ace your homework & exams now with Quizwiz!

When creating a new controller, what class do you inherit from?

"regular" Controller inheirts from ControllerBaseClass, ApiController inherits from BaseApiController

How do you send email on the server?

.NET and .NET Core come with built-in support for sending emails through the System.Net.Mail namespace. While it might seem like the easy choice, you will need an SMTP server for this to work. SMTP, or Simple Mail Transfer Protocol is used to send email from one server to another, but with REST APIs, it would be best to utilize a third party email service, such as SendGrid to send emails. We can use STMP to send mail across different servers

What is a Model?

A Model is a class in ASP.NET MVC Framework. The Model is used in the View to populate data, also for sending data to the Controller. ( {get; set;} ).

What are the differences between an API Controller and a "regular/view" Controller in .Net?

An API controller in .Net is a code statement or function that returns data and a RESTful API to the application while a regular or view controller is a class that returns a view.

How does one implement this? (Make sure only known users are allowed to execute code)

Authorization in ASP.NET Core is controlled with AuthorizeAttribute and its various parameters. In its most basic form, applying the [Authorize] attribute to a controller, action, or Razor Page, limits access to that component to authenticated users. ex) simple, role-based, policy-based authorizations.

Describe the main differences or considerations between the JS and C#.

C# is statically typed, while JavaScript is dynamically typed.C# is strongly typed, while JavaScript isn't.C# has LINQ, a powerful .NET component that adds native data querying capabilities, but JavaScript has separate libraries that can cover this functionality, one being Underscore.js.C# gives the programming explicit control over threading. JavaScript hides much of this with its call-and-response function structure.C# has operator and conversion overloading. JavaScript does not.

How does the webserver know who you are across different web requests?

Cookies are part of HTTP. Included in the request your computer sends to the server is a cookie. Which is basically a chunk of data that the server included in the response when you logged in. By sending the cookie with every request your computer makes, the server can identify the correct user account and act appropriately.

In an API application, what information is used to route your request?

HTTP Verb, URI Path, Action, Parameter

What are the different parts of a Connection String?

It includes parameters such as the name of the driver, server name, and the database name, as well as security information such as user name and password. name,provider name, connection string , ip address and password

What is a Null Reference Exception?

It is referencing an object that is null. Get a null reference exception when trying to access properties of an object that are not there and therefore null.

What are the fundamentals that a developer should consider when working to wire up model binding correctly?

Make sure that the model being bound reflects the data types that are in the HTTP request or else dotnet would not be able to convert the data between the frontend and backend. For example, a GET retrieves integers and hydrates the instance of the model with integers. If the data type declared in the model is a string, the method will break and say that it can't convert an integer into a string.

Can you create an instance of an abstract class?

No, you cannot create an instance of an abstract class because it does not have a complete implementation. The purpose of an abstract class is to function as a base for subclasses. It acts like a template, or an empty or partially empty structure, you should extend it and build on it before you can use it.

How can you test if an instance of an object implements a particular interface?

Object - if(myObject is IMyInterface) { // object myObject implements IMyInterface } Generic object - var myCastedObject = myObject as IMyInterface; if(myCastedObject != null) { // object myObject implements IMyInterface }

Write a C# function that accepts an array of integers, adds all the members, and returns the result.

Provided that you can use .NET 3.5 (or newer) and LINQ (public int addArr(int[] arr) {int sum = arr.Sum(); return sum; } or maybe... loop public int addArr(int[] arr) {for(int i = 0; i < arr.Length; i++) {int sum += arr[i]return sum; }})

What are the .Net objects and classes used to communicate and execute commands at the database?

SqlConnection, Sqlcommand, SqlDataReader

What is the difference between a string and a string builder and when would you use one over the other?

StringBuilder is used to represent a mutable(changeable) string of characters. String objects are immutable(not changeable) If you are concatenating a few strings, you should NOT use StringBuilder. This is is best used for large amounts of data or an unknown amount of loop iterations.

What are Nullable Types?

The Nullable type allows you to assign a null value to a variable. You can create a nullable type by putting a ? at the end of a data type

If this class was going to participate in Model binding in the MVC request life cycle, is it currently set up to report as invalid when no data is sent to the Server? (Assume FirstName & Last Name are required)

This class is currently not set up to report invalid.

What do you use to debug C#?

Use the inbuilt debugging tool with keybind F5 in Visual Studio, placing breakpoints to the line of code that you want the debugger to run to. You can then look at values if that section of code is being ran.

What would you have to do to make this model (Contact) report "Invalid" in such a case?

Would need to add validation attributes such as [Required], [StringLength(6, MinimumLength = 3)] or a RegEx like [RegularExpression(@"(\S)+", ErrorMessage = "White space is not allowed.")] 2.You would have to use data annotations to indicate which properties you would want to report as invalid when no data is sent using [Required]. Additionally you could further specify which data types and lengths you would allow it to receive and still be considered valid.

What is an abstract class and is this different than an interface?

an abstract class is a restricted class that cannot be used to create objects (to access it, it must be inherited from another class). An abstract class allows you to create functionality that subclasses can implement or override. An interface only allows you to define functionality, not implement it.

What is a static member or property in .Net?

When we declare a member of a class as static, it means no matter how many objects of the class are created, there is only one copy of the static member. Static variables are used for defining constants because their values can be retrieved by invoking the class without creating an instance of it. Static variables can be initialized outside the member function or class definition. You can also initialize static variables inside the class definition.

What does it mean to be a strongly typed language?

trongly typed is a concept used to refer to a programming language that enforces strict restrictions on intermixing of values with different data types. When such restrictions are violated, and error (exception) occurs. he behavior of operations is more predictable compared to those of weakly typed languages. The downside is having to declare and type all variables and parameters - though some would argue this is simply good coding.

How do you declare a variable in C#?

variable is nothing but a name given to a storage area that our programs can manipulate. Each variable in C# has a specific type, which determines the size and layout of the variable's memory the range of values that can be stored within that memory and the set of operations that can be applied to the variable. To create a variable, you must specify the type and assign it a value:

What are Connection Strings?

A connection string provides the information that a provider needs to communicate with a particular database.

What is the generic name of the library used in .Net that is used to communicate with Databases?

ADO.Net Two important objects of ADO.Net - DataReader and DataSet

What is Model Binding (ASP.Net) and why is it important?

ASP.NET MVC model binding allows mapping HTTP request data with a model. It is the procedure of creating . NET objects using the data sent by the browser in an HTTP request. Model binding is a well-designed bridge between the HTTP request and the C# action methods This is important because we are constantly moving data from user to database and we do that by mapping objects.

How would you declare an array in C#

An array is declared in C# by first defining the variable type followed by square brackets. For example: string[ ] fruits; This variable now holds an array of strings. You can then insert values by placing them in a comma-separated list inside curly braces. string[ ] fruits = {apple, orange, banana}

What are Interfaces?

An interface is a specification for a set of class members, not an implementation. An Interface is a reference type and it contains only abstract members such as Events, Methods, Properties, etc. It contains only declaration for its members and implementations defined as separate entities from classes. It can't contain constants, data fields, constructors, destructors and status members and all the member declarations inside interface are implicitly public. In the human world, a contract between the two or more humans binds them to act as per the contract. In the same way, an interface includes the declarations of related functionalities. The entities that implement the interface must provide the implementation of declared functionalities.

What is attribute routing?

Attribute routing is all about attaching a route, as an attribute, to a specific controller or action method. Decorating Controller and its method with [Route] attribute to define routes is called Attribute Routing. In simpler terms, using [Route] attribute with controllers and method is Attribute Routing. This allows you to easily create URIs that describe hierarchies of resources. [Route ("api/customers/{id}/orders")]

What is Authorization? At what level does it typically happen your application?

Authorization filters run before the controller action. If the request is not authorized, the filter returns an error response, and the action is not invoked. Within a controller action, you can get the current principal from the ApiController.User property. For example, you might filter a list of resources based on the user name, returning only those resources that belong to that user.

What are C# extension methods?

Extension methods are additional methods. Extension methods allow you to inject additional methods without modifying, deriving or recompiling the original class, struct or interface. Extension methods can be added to your own custom class, .NET framework classes, or third party classes or interfaces.

How do you store Flags in an Enum?

Flags allow an enum value to contain many values. An enum type with the [Flags] attribute can have multiple constant values assigned to it. With Flags, it is still possible to test enums in switches and if-statements. Flags can be removed or added. We can specify multiple flags with the "or" operator. ( [Flags] public enum UserStatus: short { NotSet = 0, [Description("Inactive")] Inactive = 1, [Description("Active")] Active = 2 } )

What are Collections in .Net?

For many applications, you want to create and manage groups of related objects. There are two ways to group objects: by creating arrays of objects, and by creating collections of objects. Arrays are most useful for creating and working with a fixed number of strongly typed objects. For information about arrays, see Arrays. Collections provide a more flexible way to work with groups of objects. Unlike arrays, the group of objects you work with can grow and shrink dynamically as the needs of the application change. For some collections, you can assign a key to any object that you put into the collection so that you can quickly retrieve the object by using the key. A collection is a class, so you must declare an instance of the class before you can add elements to that collection. If your collection contains elements of only one data type, you can use one of the classes in the System.Collections.Generic namespace. A generic collection enforces type safety so that no other data type can be added to it. When you retrieve an element from a generic collection, you do not have to determine its data type or convert it.

What are the different types of HTTP methods? When would you use these over others?

GET - when your main goal is to retrieve data from your database Post - when your main goal is to add a record to your databasw PUT - when your main goal is to update a record in your database DELETE - when your main goal is to delete something to your database

What are Generics in .Net

Generics are classes, structures, interfaces, and methods that have placeholders (type parameters) for one or more of the types that they store or use. Generic is a concept that allows us to define classes and methods with placeholders. C# compiler replaces these placeholders with the specified type at compile time. The concept of generics is used to create general-purpose classes and methods.

Why are Generics so important?

Generics introduces the concept of type parameters to .NET, which makes it possible to design classes and methods that defer the specification of one or more types until the class or method is declared and instantiated by client code. For example, by using a generic type parameter T, you can write a single class that other client codes can use without incurring the cost or risk of runtime casts or boxing operations.

What protocol is normally used in a web request?

HTTP for HyperText Transfer Protocol is used to structure request and responses over the internet. HTTP requires data to be transferred from one point to another over the networ

What is a Null Reference Exception?

Happens when you try to access a reference variable that isn't referencing any object- Can also happen when you try to set a value to a variable but have yet to instantiate it- Solution: always instantiate variables and set up null checks

What is IEnumerable and what significance does it hold?

IEnumerable is an interface that allows you to loop over an entire collection. For example, if you are writing a method that manipulates a collection of items, it would be best to declare the method's parameter by using an interface such as IEnumerable<T> rather than using a strong data type such as List<T>

What does your typical error handling code look like?

In modern languages like C#, "problems" are typically modeled using exceptions. When an exception is thrown, the current flow of the code is interrupted and handed back to a parent try catch block. C# exception handling is done with the follow keywords: try, catch, finally, and throw. try - A try block is used to encapsulate a region of code. If any code throws an exception within that try block, the exception will be handled by the corresponding catch. catch - When an exception occurs, the Catch block of code is executed. This is where you are able to handle the exception, log it, or ignore it. finally - The finally block allows you to execute certain code if an exception is thrown or not. For example, disposing of an object that must be disposed of. throw - The throw keyword is used to actually create a new exception that is the bubbled up to a try catch finally block. (https://stackify.com/csharp-exception-handling-best-practices/)

What is an MVC View?

Model View Controller (MVC) Architecture, is a software class that contains a template and data form and produces a response for the browser. It receives data from the Controller of the MVC and packages it and presents it to the browser for display

What are all the different types of validations that these attributes let you perform?

Required - Specifies that a data field value is required. - Range - Specifies the numeric range constraints for the value of a data field. - Max/Min Length - Specifies the maximum/minimum length of array or string data allowed in a property. - String Length - Specifies the minimum and maximum length of characters that are allowed in a data field. - Email - Validates an email address. - Phone - Specifies that a data field value is a well-formed phone number. - Data Type - Specifies the name of an additional type to associate with a data field. - Validation - Serves as the base class for all validation attributes

What is the Web.Config/ApplicationSettings.json? What do you use it for?

The appsettings. json file is generally used to store the application configuration settings such as database connection strings, any application scope global variables, and much other information. It is used to store the application configuration settings such as database connection strings, any application scope variables, and much other information.

Would this method work if you were to switch out browsers in the middle of a session?

The influence of cookies is limited to a single browser on a single device. You can't store the same cookie data on multiple devices. Cookies can't be read by multiple browsers. If you want to maintain state across multiple browsers or devices, then you've got to store state data on the server, not on the user's PC.

What is the best way to concatenate strings in .Net and why?

Using + operator String Interpolation String.Concatenate() method String.Join() method String.Format() method StringBuilder.Append() method (https://www.c-sharpcorner.com/article/6-effective-ways-to-concatenate-strings-in-c-sharp-and-net-core/)

What namespace do these attributes live in?

Validation attributes live in the System.ComponentModel.DataAnnotations namespace.

What are the first 4 numeric values of an Enum used to store Flags?

We use the values 0, 1, 2, 4 to indicate the underlying bits for each value—we should double each value to avoid conflicts. Operators We use bitwise operators, like OR and AND, with enum flags.

What are the principles of object-oriented programming?

What are the principles of OOP?-The four principles of object-oriented programming (abstraction, inheritance, encapsulation, and polymorphism) are features that - if used properly - can help us write more testable, flexible, and maintainable code. 1 EncapsulationEncapsulation is the mechanism of hiding of data implementation by restricting access to public methods. Instance variables are kept private and accessor methods are made public to achieve this. 2 AbstractionAbstract means a concept or an Idea which is not associated with any particular instance. Using abstract class/Interface we express the intent of the class rather than the actual implementation. In a way, one class should not know the inner details of another in order to use it, just knowing the interfaces should be good enough. 3 InheritanceInheritance expresses a relationship between two objects. Using Inheritance, In derived classes we can reuse the code of existing super classes. 4 Polymorphism means "many forms", and it occurs when we have many classes that are related to each other by inheritance.Inheritance lets us inherit fields and methods from another class. Polymorphism uses those methods to perform different tasks. This allows us to perform a single action in different ways.

In .Net how many classes can one class inherit from?

You can only inherit from a single class. It is however possible to implement multiple interfaces


Related study sets

Test 3 Valuation Intermediate corporate Finance

View Set

nursing community final exam content (week 12)

View Set

Astronomy 101 Chapter 10; Mastering Astronomy Assignment

View Set

Humanities Final Multiple Choice

View Set

Fundamentals Chapter 14 The Lathe

View Set

Ch.5 Gross Income and Exclusions

View Set

CH 1, 2, 3, 4, 5, 6 - Leadership

View Set