C# Interview prep: 50 Question and Answers

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
so you have an important job interview in c sharp ahead of you and you would like to prepare yourself in the best possible way and there are of course a bunch of things that you can do you can look into different projects that they could come up with or you can look into different questions that they can come up with for projects we have just finished a series of four different projects and we are going to add some more projects in the future so you can of course check out the link in the card where you can find the link to the whole playlist but in this video we are going to cover specific questions that you could be asked and i'm going to give you a possible answer of course there are a bunch of different ways to answer questions sometimes there is just one right answer or at least a direction of an answer but in this video we are going to check out the most important ones and then afterwards we're going to look into more and more important ones so basically we are going to cover over 50 questions that you could be asked during a job interview for c-sharp so definitely check it out even if you don't need it for a job interview if you just need it for let's say university or you just want to refresh your c-sharp knowledge this is definitely going to be the right video for you all right and also check out the other videos definitely and don't forget to subscribe and hit that like button and all of the good stuff because this is really going to help the youtube algorithm for our channel but at the same time it's definitely going to help you because you're going to see all of the good other videos that we are posting regularly and yeah enjoy the video [Music] so let's start with the first question what is a class and by the way if you want to read the whole article with all of the questions and answers definitely check out the link in the description you can find it there all of the 50 questions and answers well 52 actually you can find them directly on the website all right a class is a template to create an object it contains properties as well as methods we can create many instances of a class so here for example we have this class called student and we have a bunch of data members well in this case just two and then we have one method and this method is called print details and here we can then implement the functionality that should be run once print details is called now we can create an instance of such a class or an object of such a class by just going ahead and saying class name object name equals new class name and then the parameters in our case we don't have any parameters that we need to pass to this class because we don't have a constructor so we're going to look into constructors in this list as well of course then what are the main concepts of object-oriented programming it's encapsulation abstraction polymorphism and inheritance these are the main concepts of object-oriented programming and that is something that is used in many different programming languages c-sharp is just one of those now let's look into those individual topics so first of all encapsulation it's a process of wrapping functions and data members together in a class it's like a capsule which is a single unit encapsulation is used to prevent the unauthorized unwanted change of data from outside of the function and now let's have a look at that so in this code i'm using encapsulation so i have this class called user and i have two variables string address so you can see there is no specific access modifier and then we have a private string name in order to access those from another class such as our my program class i need to use encapsulation because otherwise i couldn't access them so i'm using getters and setters so here for example string address by using this property address i can now access the address variable this field that i created here up top and i can also set it by using that property here the same goes for name as you can see here public string name get return name set name with the value that was passed now if we look at it from our main class you can see we are creating a user we can set the name of that user and we can set the address of the user by using the set accessor so we are invoking that set accessor and when we get a value for example when we want to write the name or the location we are invoking the get accessor if you now try to access the name directly so let's say you want to access name instead of name so not the capital one and you want to set that with for example dennis in my case you can see that you get an error because that variable is hidden it's inaccessible due to its protection level and this protection here is basically encapsulation quick pause in this video you'll learn something about c sharp and if you want to learn everything there is to know that you need for the fundamentals and to become a real c-sharp developer then definitely check out my c-sharp master class in which you're going to learn all of the things you need to know about c-sharp so you're going to learn how to do the basics how to use object-oriented programming how to use wpf in order to create your own user interfaces how to use databases how to use link how to create your own games using unity and a lot more so if you want to become real c sharp developer definitely check out the link in the description below what is abstraction abstraction is the method of exposing only the required features of the class and hiding unnecessary information so let's try to understand it whether the example of a motorbike a rider knows the color name and the model of the bike still he doesn't know the internal engine and exhaust functionality he doesn't need to know that even though some people will do that because they are really into bikes and they will be opening up the bike and know everything about it so abstraction focuses on providing access to the specific functionality without exposing how that functionality works internally what is inheritance inheritance is one of the fundamental attributes of object-oriented program it allows you to define a child class that reuses or inherits or extends or even modifies the behavior of a parent class the class whose members are inherited is called the base class the class that inherits the members of the base class is called the derived class you can find a bunch of other names for that as well parent children class super base class there are a bunch of different names for that but basically you always have the inheriting as well as the inherited class so let's look at an example here so we have class a and then we have an inner class called class b which inherits from class a and in class a we have a private variable called value which is an integer and it has the value of 1337 and when we use public and get value it will return this that value so now if you look at this class b it doesn't even have a variable that is called value but as you can see it can use the variable value because it inherits from class a and because it's an inner class so now if you want to access this variable you can do that so let's create an object of b by creating a new a dot b so this creates an object of the b class and then we can go ahead and just write the get value which will just return value which in turn will return 1 300 1337 and if you run that application you will see that that's actually the case so in this case a is the base class and b is the derived class so a is the parent and b is the child class at this point the question could come up what is an object and we have used objects already in this video even but let's just quickly also cover this specific question so an object is an instance of a class through which we access the functions of that class we can use the new keyword to create an object a class that creates an object in memory holds information about the functions data members and behaviors of that class so let's look at this class called employee in which we have a private screen called fname and lname so first name and last name and then we have the methods so for example the display method which basically just displays the first name and the last name in the little text saying full name is and then first name and last name and then we have this method called set name in which we can set the name of those two variables all right so we are setting the first name as well as the last name directly by calling this method now let's go ahead and create an object of employee so this is the object itself so this is basically already the answer you are creating a new object by using the name of the class as well as a name for that object equals the new keyword plus the name of the class once again and then you pass in potential parameters here for the constructor but as i said we're going to see how constructors work in a minute and then we have to set the name at one point if we want to display it because otherwise those variables would be empty and display would run into errors because it can't find fname and lname as they would be empty so we're first of all setting those variables and then we're displaying them so you see we're setting them to john grande and then we're displaying them so now let's look at constructors a constructors like a method with the same name as the class but it is a unique method even if it's not created the compiler creates a default constructor in memory at the time of creating an object of the class okay so in this case we have a constructor even though we have not created one by ourselves so that's something that will be done by the compiler the constructor is used to initialize the object with some default values then there are different types of constructors for example the default constructor the parameterized constructor the copy constructor static constructor private constructor and that's it so these are the different types of constructors that there are so we're going to look at one of them so let's just create a constructor here real quick for our employee so that we don't have to set the name with this set name method but instead we're going to set it when creating the object itself so in this case it will be the parameterized constructor so you can see we have an employee which gets a first name and a last name when we create an object of it and then we set the variables directly so these fields that we have here we are setting them directly when creating the employee the default constructor is a constructor without any parameters as you can see here so you could add the code that should be run if an employee is created so in our case right now we are creating an employee using the default constructor and as i said this will be generated automatically even if you don't implement it by yourself then the parameterized constructor is what you can see here this is the one that you're probably going to use most of the times then you have the static constructor which just uses the static keyword a steady constructor is used to initialize any static data or to perform a particular action that needs to be performed once only it is called automatically before the first instance is created or any static members are referenced okay then we have the copy constructor which allows us to copy and other object of the same type so in this case we are just copying another employee and we're saying that this employee that we're creating this object that we're creating of this embryo class should have the same type of information in this case it's going to have the employee's first name as well as last name assigned to it as well so we're basically just copying the other object here by using the copy constructor so let's go ahead and use this parameterized constructor real quick instead of setting the name this way we are just going to do it in the constructor itself so here we are passing the values directly we don't need set name anymore and now we can run it again and you can see we still get this information full name is john grande as there is a constructor there is also a destructor and the destructor clears out the memory to free up resources it is managed automatically by the garbage collector so for example this one here system.gc.collect so this is how you can call the garbage collector manually but it is also done automatically once there is too much garbage to collect and then it starts to collect it so as i said it's done automatically internally however if required it can be done explicitly using a destructor so let's look at a destructor here for our employee so this is it you just use the utility then employee and then you can run whatever code you want to run in order to release resources so let's look at polymorphism because that could be also another question what is polymorphism well polymorphism is the ability of an object to take on many forms and there are two types of polymorphism we're going to look at both of them one of them is compile time polymorphism which is achieved by method overloading what you can see here so we have the method typing and then we have the method typing and they are overloaded so this one overloads the other one so the other one this one here and you can see it overloads it by having more information passed to it so we have the same method name but they are using different amounts of parameters so in this case typing this method here requires a boolean that we then can use inside of the function even though we don't do that specifically but we could do that all right and then we can run different code so depending on the amount of parameters or the type of parameters that you're passing you will get different results also meaning different types of code executions so different function bodies or method bodies will be executed so this here is compile time polymorphism which is achieved by method overloading now there is also runtime polymorphism which is achieved by method overriding so let's look at another example here where we are having two classes and they are inheriting from each other so one of them is inheriting from the other so we have cell phone and smartphone okay so in cell phone we have this virtual method called typing and in smartphone which inherits from cell phone we have an override void typing so we are overriding this virtual method typing from the base class with our own implementation so here we're just doing something else and this concept here is called runtime polymorphism so these are the two types compile time and runtime polymorphism and then the final question for this video is the sharp code managed or unmanaged code and the answer is c sharp is manage code because common language runtime compiles the code to intermediate language so to put it very simply manage code is just that code whose execution is managed by a runtime in this case the runtime in question is called the common language runtime or clr regardless of the implementation so for example mono or net framework or net core clr is in charge of taking the managed code compiling it into machine code and then executing it on top of that the runtime provides several important services such as automatic memory management security boundaries type safety etc quick pause in this video you'll learn something about c-sharp and if you want to learn everything there is to know that you need for the fundamentals and to become a real c sharp developer then definitely check out my c sharp master class in which you're going to learn all of the things you need to know about c sharp so you're going to learn how to do the basics how to use object-oriented programming how to use wpf in order to create your own user interfaces how to use databases how to use link how to create your own games using unity and a lot more so if you want to become a real c sharp developer definitely check out the link in the description below what are value types and what are reference types well we can categorize variables into value types and reference types variables of value type contain the value directly while variables of the reference type contain the reference of the memory address where the value is stored in the memory so simple data types such as booleans bytes integers and so forth are value types and complex data types such as strings objects delegates and so forth are reference types okay so let's look at this from with this image that you can see here so we have this integer called z which has the value of 234 so this one is going to be directly stored in the ram at that position of that variable now on the other hand if we look at a string which is a complex data type then we see that this value dennis is not directly stored at the position of the variable but the variable only has the position where the data type is stored so the address so to speak of the value where it is in our memory and that has to do with optimization and efficiency so for example if you are moving around integers bytes and those kind of data type variables they have really limited amounts of storage that they require but objects for example or strings they need significantly more space so when we move around information and when we reuse variables and so forth it's significantly more efficient to just set the location of the variable when we for example copy the value of a variable to another and it's a complex variable such as a string then it's simpler to just send the location over instead of sending the whole data over so for example let's say you go into a club and you have your jacket on and you pass the jacket over and they then store the jacket for you directly at the counter so then that would be a value type because you can directly see your jacket and now let's say it's a huge club and they need to put the jacket somewhere else they would need to put it into a different room and what they will store is your name for example because it's a fancy club and they just know you they will store your name in combination with the number where they have positioned it and they will store it at the counter and they will give you that number as well so you can now come to this counter and you can give them your number so the address and what they will do is they will go to the address meaning they will go into this other room and they will get the jacket and bring it back to you quick pause in this video you'll learn something about c sharp and if you want to learn everything there is to know that you need for the fundamentals and to become a real c sharp developer then definitely check out my c sharp master class in which you are going to learn all of the things you need to know about c-sharp so you're going to learn how to do the basics how to use object-oriented programming how to use wpf in order to create your own user interfaces how to use databases how to use link how to create your own games using unity and a lot more so if you want to become a real c sharp developer definitely check out the link in the description below alright so what are namespaces and are they even necessary well a namespace is a way of organizing classes of the same group or functionality under the same name we can call it a module although it's not compulsory to put a class in a namespace so here for example we have this demo app with a class called soundclass and there we have a method called some method so this class here is inside of the namespace demo app now we could go ahead and create a bunch of functionality that will be stored within that module or namespace that we generated and then other people could just reuse our namespace because well we just made it available as a library online for example what kind of comments are there in c sharp there are three types of comments a single line comment which starts with a double slash then a multi-line comment that you can generate with a slash asterisk and you close it with an asterisk slash so everything in between will be considered a comment and will not be any logic of your code and then finally you have an xml comment which is the summary that you can see here which you can generate by using triple slashes usually you would use those to create a little summary of what a class or a specific method is going to do you would use single line comments to describe a single line of code that comes afterwards and you can use multi-line comments if the explanation is a little more complicated what is an interface and given an example well an interface is another form of an abstract class that has only abstract public methods and these methods only have the declaration and not the definition itself a class implementing the interface must have the implementation of all the methods of that interface so for example i have this interface called eye pencil let's look at it this eye pencil interface has these two methods write and sharpen and it doesn't have a method body as you can see here but now our class pencil is going to implement this eye pencil and now we need to go ahead and write some code and implement those functions inside of this class all right the next question is how to implement multiple interfaces with the same method name in the same class if you want to implement multiple interfaces with the same method name then you cannot directly implement the body of the function you have to explicitly provide the name of the interface to implement the body of the method and this way the compiler decides which interface methods we are referring to and this resolves the issue so let's have a look at an example here so we have this student class here which implements two interfaces my interface one and my interface two and both interfaces have this method called print now let's look at those two interfaces you can see interface 1 just has this method print and interface 2 also has this method print this particular example isn't of course not going to be realistic you would have a bunch more methods in here and you would have to implement more methods and also the names would be slightly different probably but overall it's really just to bring home this point here and as you can see if you want to implement print you definitely need to also add the name of the interface that you want to implement this method from so you are implementing print from my interface one and here you are implementing the body and the same goes for interface two here you also use that same name but then you need to add this my interface to you in order to make sure that the compiler understands which print method you are referring to what is a virtual method and how is it different from an abstract method a virtual method must have a default implementation and we can then overwrite this virtual method using the override keyword in the derived class we've done that in part one but now we are specifically talking about it so we are using this virtual keyboard so this is a virtual method here and this is the cell phone class and now we have this smartphone which inherits from cell phone and here we can now override whatever the body of that method typing was so we make our own version of that method in our smartphone class so now if we call smartphone.typing it's going to print this statement instead of printing that statement so it's going to execute the typing body of our smartphone class instead of our cell phone class so if we're running this code where i'm creating an object a which is a cell phone and an object b which is a smartphone and we're using this typing method in the cell phone version is going to use the old keypad and in the smartphone version is going to be the qwerty keyboard so basically we are overriding the typing functionality of our cell phone which is this virtual void in our smartphone class by using the override keyword and how is this now different from an abstract method well the abstract method is without an implementation and it is created inside and it is created only inside an abstract class in the case of an abstract class the class derived from the abstract class must have an implementation of that abstract method and in this case it doesn't have to so here our smartphone doesn't need to overwrite the virtual method because it could just use the virtual method itself so if we run this code again we can see that our smartphone is also using the old keypad even though it's a smartphone but that's just because we haven't implemented this typing override method now a question that we almost answered already what is method overloading and method overriding method overloading is when we have a function with the same name but a different signature and method overriding is when we overwrite the virtual method of a base class in the child class using the override keyword so we have looked at this with an actual example in the last video so in part one of the series where we actually saw the difference between overloading and overriding but here at this point this is just another quick reminder so the next question is what is the static keyword so what does it do what is the impact of that static keyword well we use the static keyword to create a static class a static method or static properties what does that mean well when we create a static class then there can only be static data members and static methods in that class so if we look at our static void main so our class my program it has this static void main and when we want to create another method in here and want to call it in the main method it has to be a static method as well so here for example you can see that i cannot call my method because it is not static so it says an object reference is required for the non-static field method or property myprogram.mymethod so as this is not static i cannot call my method so if i make it static now i can call this method in a static context so in the static main method so let's look at this example here with our static class setting you can see it has this static method with the end max amount 0 and it returns the max amount but then we have our public class sales where we also want to store the max amount and we want to fetch the default data so we use setting dot fetch default you can see we don't need to create an object of setting in order to use fetch default because this is a static class and static basically means that we cannot create an instance of a class so if i now create an object of setting setting a is going to be a new setting you can see that this won't work because it says cannot create an instance of the static class setting so if you have special functions that you want to use without creating objects of a class then you can create them in a static class and just use it without having to create objects the cool thing is for example if there is the requirement to load some default application level values we can just create a static class with static functions that the class is then accessible to all other classes without creating any instance of it and it also shares the same data with all the other classes now the next question could be can we use this keyword with static classes and the answer is no we cannot with static classes you cannot do that because we can only use static variables and static methods in a static class what is the difference between constants and read only variables well constants have to be assigned a value at the time of declaration only and we cannot change the value of the variable throughout the program so here for example you can see the const myvar and const string that is a constant string and now if we try to change that in domain method here we cannot do that so i cannot go ahead and say my var equals 200 for example because it is a constant and it's unchangeable now in comparison to a read only variable so here we are using read only this keyboard with myvar1 and myvar2 and you can see we can assign a value to it in for example the constructor but then once a value is assigned to it we cannot change the value anymore so at this point i cannot go ahead and say object one dot my var 1 is going to be for example 300 that doesn't work because my var1 is a read-only field constants have to be assigned a value during declaration and read-only variables can be assigned a value later on during the run time but they cannot be changed afterwards that's the case for both of them so read only as well as constants cannot be overridden quick pause in this video you'll learn something about c-sharp and if you want to learn everything there is to know that you need for the fundamentals and to become a real c-sharp developer then definitely check out my c-sharp master class in which you're going to learn all of the things you need to know about c-sharp so you're going to learn how to do the basics how to use object-oriented programming how to use wpf in order to create your own user interfaces how to use databases how to use link how to create your own games using unity and a lot more so if you want to become a real c sharp developer definitely check out the link in the description below 21 what is the difference between string and string builder in c sharp and by the way you can check out the complete article that this video is based on on my blog you can find the link in the description below and also please don't forget to hit that like button and become a subscriber because well 70 of my viewers are not subscribed yet so maybe you're one of them and if you are then please consider subscribing all right so what is the difference well a string is an immutable object so when we have to do some actions to change a string or append a new string it clears out the old value of the string object and it creates a new instance in memory to hold the new value in a string object and this is consuming some energy or it takes quite some time to do that even though it's still super fast in comparison but what you can see here is we create this string vowel which says hello and then we add something to the well variable which is world so now it's going to say hello world but this creates a new instance of the string so if we run that it's going to say hello world without an empty space in between now let's look at the string builder in order to use the string builder you need to make sure that you're adding system.txt so this namespace is required for your string builder to work and then what you can do is you can append something to the string builder question number 22 explain the continue and break statement even though that's not the question but still they might put it in a question format so basically what we have here is a for loop which iterates from 0 to 5. so we have this if statement here inside of this for loop which checks if i is equal to 3 and if that's the case then it's going to break otherwise it's generally going to run this right line statement the number is and then it's going to request us to enter a value where it's going to read the line so we can actively test it okay so this break statement will break out of the loop which means it will jump out of this for loop and it will go all the way here so control will jump here after the break statement so this of course is obvious it's very simple we are generating this break by ourselves but maybe this if statement gets data from the database and if a certain condition is met then it will break out of the loop and it will not be as stupid loop as this one is but let's just execute this and test it so number zero i'm pressing enter number is one pressing enter numbers two and then it jumps out of the loop and as you can see we're out of the loop and the execution is done so basically it never passed this point of i being three it didn't even execute the number is three because it broke out of this for loop all right now let's do the same thing with continue so now we use continue instead of break and it will skip the single iteration which means it will not execute whatever comes after continue inside of this for loop at least for that one iteration so it will not execute the number is three but it will jump to the number is four directly let's test this the number is zero the number is one number is two and you see the number is four and then five and that's it so break jumps out of the for loop and continue jumps out of the current iteration of the for loop and this counts for loops in general it's not just the case for for loops you can also break out of the execution of the current function number 23 what are boxing and unboxing well the conversion of value type data types to reference types so object data types is called boxing so what we have here is we have this integer i being 10 and then we create an object called o and we use the i value for that object so this is the concept of boxing so basically we're boxing in our integer into this object o so we're changing it from a value type to a reference type so this is a value type and this is a reference type all right now what is unboxing well unboxing is the opposite of boxing so basically we have this object o which is an integer where which holds an integer and we convert it into an integer itself so we convert o the value that is inside of our object into an integer and that is called unboxing quick pause in this video you'll learn something about c sharp and if you want to learn everything there is to know that you need for the fundamentals and to become a real c sharp developer then definitely check out my c sharp master class in which you're going to learn all of the things you need to know about c-sharp so you're going to learn how to do the basics how to use object-oriented programming how to use wpf in order to create your own user interfaces how to use databases how to use link how to create your own games using unity and a lot more so if you want to become a real c sharp developer definitely check out the link in the description below number 24 what is a sealed class well we use the sealed keyword as you can see here before the class keyword to create a sealed class so classes are created as sealed classes when there is no need to inherit this class further or when there is a need to restrict that class from being inherited so in this case we cannot inherit from our class my class so if i now create a new class here public class my class 2 which tries to inherit from my class you will see that you get an error here my class 2 cannot derive from sealed type myclass so basically we're saying i never want to allow an inheritance of my my class number 25 what is a partial class well there is a feature in the c-sharp language to divide a single class file into multiple physical files so to achieve this we have to use the partial keyword and at compile time it is logically one file only so we cannot have a method with the same name or variable with the same name in two different partial class files and this concept is highly used when using wpf in order to create graphical user interfaces in c-sharp using xaml and well we are going to use wpf on this channel in the next few videos so definitely hit that subscribe button to not miss out on that topic number 26 what is enum well in c sharp the enum keyword is used to declare an enumeration so an enum is a value type it is a collection of related named constants referred to as enumerator list an enum type can be any of these integer float double and byte however to use it beside int explicitly casting is required so to create a numeric constant in the dotnet framework enum is used all the members of the enum are of the enum type and there must be a numeric value for each enum type so basically we have this enum here as you can see it's called day and this could be called day of the week for example so we have saturday sunday monday tuesday wednesday and so forth and we can now access them directly here so i can just say day dot and i will get those different values and you can also see that they're assigned a number so friday is for example number six which means that saturday will probably be number zero so you can see the asat is assigned zero so it starts at zero and it goes all the way up to however many entries you have here so you can also see that ind is the default type of the enumeration element and by default the first enumerator has the value of zero and well each successive enumerator is then increased by one number 27 what is dependency injection and how can it be achieved well dependency injection is a design pattern here instead of creating an object of a class in another class so independent class directly we are passing the object as an argument in the constructor of the dependent class it helps to write loosely coupled code and helps to make the code more modular and easy to test and there are three ways to achieve dependency injection so constructor injection this is the most commonly used injection type in constructor injection we can pass the dependency into the constructor we have to make sure that we do not have a default constructor here and the only constructor should be a parameterized constructor then we have property injection there are cases when we need the default constructor of a class so in that case we can use property injection and then finally method injection in method injection we need to pass the dependency in the method only when the entire class does not require the dependency there is no need to implement constructor injection when we have a dependency on multiple objects then instead of passing the dependency in the constructor we pass the dependency in the function itself where it is required number 28 the using statement the keyword using is used to define the scope of the resources used in that using statement block all the resources used inside the using code block get disposed of once the code block completes execution so i have this class called book and it inherits from the i disposable interface provides a mechanism for releasing unmanaged resources and we need that for our using keyboard that we use here so basically i have this class book which has a name and a price and i couldn't create an object of it and there i need to enter the name and price in the constructor all right and then i can just print something in this case i'm just going to print the name and the price of the object and i can dispose and as you can see i have not implemented the dispose functionality that's something that you could do then as well and then we have this class student where the students do something and we are using this using keyword here to create a new book object with a name and a price and then we just use the print statement and once that is done the this post function will be called now in order to understand this a little better i have this static void main well a starting point for my class and i create a student called class one and i do something so i call this do something method which uses this using keyword in order to print the book okay and now i have also implemented a dispose functionality which basically just says disposing of book and if i run this you will see that it executes this print statement here book name is and price is it says book name is book name and price is 12 45 and then it's disposing of book so that's basically what this dispose method now will do and it will dispose of that object all right and that's it number 29 what are the access modifiers of sharp explain each type so access modifiers are keywords used to provide accessibility to class members or functions below are its types so you can see here there is the public keyword an object or a member or a function can be accessed anywhere without any restriction with a protected keyword the access is limited up to the class which inherits this class then we have internal which can only be accessed within the current assembly and then we have private cannot be accessed outside of this class number 30 what are delegates their nuggets are like function pointers it is a reference data type that holds the reference of a method we use that against to write generic type save functions all delegates derived from the class system.delegate a delegate can be declared using the delegate keyword followed by a function signature as shown below so you can see we create a delegate here i'm going to call this one print and it requires a value you can see we create a print delegate here so this is basically our delegate declaration and then we can use delegate in order to point to functions so here we are pointing to print number so and then at this point we can just use print l as if it was print number so we can just pass an integer value here and we can pass another integer value here and call the function once again and so forth so we are calling this print delegate which in turn has print number assigned to it at that point so it points to print number at this point and then we can basically change whatever it's pointing to throughout our execution so you can see here now the print delegate will point to print money which is this function here so it's a different function it also requires an integer that's important because we defined it here we said it will work with integers and that's why we can use it for those two functions because they both require an integer as their parameter so we can just assign print money instead of print number here at this point so it now is going to point towards print money and we can use print dell again but now it's not going to execute number so and so but it's going to execute money so and so or it's going to display money so and so so let's run this just to see what it actually does and as you can see it says number number so number 100 000 number 200 and then it says money 10 000 and money 200. so let's look at some of the characteristics of delegates delegates derived from the system.delegate class as i said they have a signature as well as a return type so in our case the return type is void but a delegate needs to have a return type and a function assigned to delegates must fit with the signature so as i said it must fit this integer signature which it does in this case and in this case as well delegates can point to instance methods or even static methods delegate objects once created can dynamically invoke the methods it points to at runtime and delegates can call methods synchronously and asynchronously and delegates are quite used in wpf so for example when you're using wpf to create your user interfaces you are going to definitely have to learn more about delegates quick pause in this video you'll learn something about c-sharp and if you want to learn everything there is to know that you need for the fundamentals and to become a real c-sharp developer then definitely check out my c-sharp master class in which you are going to learn all of the things you need to know about c sharp so you're going to learn how to do the basics how to use object-oriented programming how to use wpf in order to create your own user interfaces how to use databases how to use link how to create your own games using unity and a lot more so if you want to become a real c sharp developer definitely check out the link in the description below what are the different types of delegates well there are three types of delegates single delegate multicast delegate and generic delegate and we're going to look a little more into single delegates as well as multicast delegates in this video and single delegate can invoke a single method a multi-cast delegate can invoke multiple methods and we're going to see how that works and in dotnet framework 3.5 the generic delegate was also introduced so there is no need to create an instance in a generic delegate also check out this article you can find the link in the description below but now let's get over to the code for delegates and as you see we looked into delegates in the last video already so we're going to look a little more into it therefore we worked with single cast delegates in the last video so basically you assign a method to a delegate and you create a new delegate here or you declare a delegate in our case it was called print and then i created those individual single cast delegates now the difference to a multicast delegate is that you can have multiple delegates within a delegate so here you can see this is a multicast delegate which uses the print num dell and the print mondel so this is the print number delegate which prints the number which is this method here and then we have the print mondel which prints money which is this method here our multi print delegate will now do both of them so i can so to speak call 2 methods at the same time so here multi print dell 100 will now call both methods print number as well as print money okay we can test this and as you can see here the first iteration was with one hundred thousand and then with ten thousand so print number one hundred thousand print money ten thousand and then our multi-cast delegate was called here the multi-print dell with 100 and that is what you can see here so that's the difference between single cast and multicast you can by the way also delete a delegate from our list here so erase it by using the minus and now running this will only print the number so let's run it again and there we are you see number money that was from before and then we have gotten rid of the money part so we only print the number at that point question number 32 what is an array and explain single and multi-dimensional arrays all right so this is an example of an array and this is a one-dimensional array so basically we just have three numbers 25 34 and 89 and this array has the name of marks you can see we need to use square brackets and then i'm creating a new array of integers and i say the length of this array should be three so that is part one and then what is a multi-dimensional array and how do we create one well this is a multi-dimensional array as you can see here i have this comma in between this is a two-dimensional array and then i go ahead and say i want to create a two-dimensional array with three rows and two columns so we have basically what you see here and maybe it's better to display it in a slightly different format you can see that this is our two dimensional array here and it basically is arrays within an array so we have an array which contains multiple arrays in it question number 33 what is the difference between system.ora.com and system.array.clone well let's look at the two examples here that i have prepared so we have this marks array which we had before so i'm going to get rid of this one and then i have this empty array which i call mark's copy and basically i copy it here so i'm saying marks copy two marks and then i say from which point onwards i want to copy the array so in this case i said from index 0 which means i'm going to copy the whole array but i could have also just said copy from index 1 which means it would only copy from the index 1 which would be 34 in this case and then mark's clone what that does is it clones a complete array so using the clone method we can create a new array object containing all the elements of the original array and using the copy2 method all the items of the existing array can be copied into another existing array and both ways create a shallow copy question 34 what is the difference between array and arraylist when we want to store items of the same type then we would use an array the array has a fixed size but in the case of an arraylist it's not fixed and you can also use different types so here you can see i have this array of type string and it can contain three values so at position zero i have usa at position one i have denmark and at position 2i of russia for example so only string values can be added if i try to add an integer in here instead so instead of russia i'm going to try to enter 33 this will cause an error cannot implicitly convert type int to string arraylists on the other side can store different data types so here i'm creating an arraylist and i can add an integer i can add strings i can add booleans and so forth quick pause in this video you'll learn something about c sharp and if you want to learn everything there is to know that you need for the fundamentals and to become a real c sharp developer then definitely check out my c master class in which you are going to learn all of the things you need to know about c c-sharp so you're going to learn how to do the basics how to use object-oriented programming how to use wpf in order to create your own user interfaces how to use databases how to use link how to create your own games using unity and a lot more so if you want to become a real c sharp developer definitely check out the link in the description below question number 35 what is a jacked array in c sharp a jacked array is like an esoteric each element of ejector ray is an array itself and the item of eject array can be of different dimensions and sizes so let's look at it in this example so we have this jacked array which is a two-dimensional and i said that i want to have two rows but a undefined amount of columns so i'm going to define the amount of columns per row itself so eject array at index 0 will be itself of type array which contains three values and deject array at index one will be also of type array and it will contain four values question number 36 what is the difference between struct and class well a class and a struct are both user defined but have a significant difference so the struct inherits from system.value so it is a value type so it's not a reference type such as the class it is preferred to use structs when there is a small amount of data a struct cannot be abstract there is no need to create an object with a new keyboard when working with structs and struct does not have permissions to create any default constructor so you cannot create default constructors in your structs and how is now the class different to it well a class is a reference type in c sharp and it inherits from system.object so it's a reference type and when there is a large amount of data then it would make sense to use classes we can use inheritance so one class can inherit from another class and a class can be abstract question number 37 what is the difference between throw and throw x well the throw statement holds the original error stack of the previous function or function hierarchy in contrast through x has the stack trace from the throw point so it is always advice to you throw because it provides exact error information related to the function and gives you actual trace data of the error source so you get a little more information of what caused the error and where the error comes from so question number 38 explain the difference between finally and final lies so there's two blocks that you can use so finally for example is used in exception handling so it's the code block that will be executed irrespective of whether the exception occurs or not so if the error occurred or not doesn't matter finally this final code block will always be executed while finalize is a method that is called just before garbage collection so the compiler calls this method automatically when not called explicitly in code so basically you can make sure that whatever you need to run before the garbage collector collects garbage is executed with that object before it is collected by the garbage collector and delete it question number 39 explain var and dynamic well there are different types of variables that you can create as you have seen before you can create integers doubles and so forth but there is also this keyword called var which allows you to create a variable and let's see sharp decide what kind of data type it should assign to it so i'm creating a variable here called sum variable which i assigned 15 to it so now it thinks that some variable should be an integer which is correct well but at the same time i cannot change it of course because now some variable will be of type integer so if i try to assign a string to it it won't work if i assign 25 to it however that works totally fine because 25 is an integer itself if i try to make a double out of it now you can see it doesn't work anymore so at the point of declaration the data type is determined and you cannot change the data type that is assigned to that variable afterwards when using the var keyboard however when you're using the dynamic keyboard you can make changes to it so you can see i'm using dynamic represents an object whose operations will be resolved at run time so i have this sum value variable which has the value of 21 and then at one point in this case in the constructor i assign a new value to some value which is of a different data type so in this case it's a string so now some value can hold a string and that is the dynamic keyword you have to be very careful when using dynamic however because when you do that and you assume that a variable holds an integer but it in fact doesn't hold an integer but for example a string and you want to work with an integer well then your program will run into errors during runtime and that is a hell of a challenge to figure out correctly depending on the complexity of your software so definitely be aware of that question number 40 what are anonymous types in z-sharp anonymous types allow us to create new types without defining them and it's useful when there is a need to define read-only properties in a single object without defining each type when using anonymous types the compiler generates type and it is accessible only for the current block of code so let's look at this print function here which is generating an anonymous type which in turn creates this new read-only property which has a first name and a surname and then we can use it inside of this block which means inside of this function and that's basically it so that is an anonymous type in c-sharp so if i now try to access this anonymous type outside of print it will not work anonymous as you can see it doesn't offer anonymous data at any point of our main method because well it's only available inside of this code block so now i can go ahead and access anonymousdata.firstname to print the first name for example and you can see first name is john quick pause in this video you'll learn something about c sharp and if you want to learn everything there is to know that you need for the fundamentals and to become a real c sharp developer then definitely check out my c sharp master class in which you are going to learn all of the things you need to know about c sharp so you're going to learn how to do the basics how to use object-oriented programming how to use wpf in order to create your own user interfaces how to use databases how to use link how to create your own games using unity and a lot more so if you want to become a real c sharp developer definitely check out the link in the description below alright so question number 41 what is multi-threading and what are its different states well multi-threading first of all any code block in c-sharp runs in a process called a thread and it is the execution path of the program usually an application runs in a single thread but multi-threading helps to run the application in multiple threads using multi-threading we can divide the execution of our process among different threads to execute it simultaneously so in multi-threading we can create multiple threads and assign particular tasks or put a code block to get executed in this way we can run more than one task at a time this code block executes simultaneously and can save time and in this we can make programs more efficient and fast so a thread has its life cycle which includes various states okay so it's has a state aborted a board request that's before the aborted state can start we have running which basically means the threat has been started and not yet stopped so it's currently running then there is threat stopped to stop it you can start a request you can suspend the whole thread so that it's gone and so forth so you can see a bunch of different states that a thread can have and in c sharp you can use the namespace system.threading which contains the thread class so here if you scroll down you will find this class called a thread so this creates and controls a thread and sets its priority and gets its status there is of course a lot more to know about threats but this is just a very very super high level answer for this question you could of course dig deeper into this and talk about this 10-15 minutes during the interview but i think in most cases if they don't want to know significantly more this should be enough but otherwise definitely read a lot more through the thread class and in general through the threading namespace to prepare yourself perfectly for this question number 42 how is exception handling done in c sharp well basically you can use those keyboards to do exception handling try catch throw and finally in the try block you add the code that you want to execute that could run into errors and could cause an exception then you use the catch keyword to execute what should happen in case a specific exception occurs and in our case we can just throw an exception in this case it will just stop the program there will not be any clean execution whatsoever and then you will have finally in our case we just throw the exception so basically we will display the exception in the log and then we have the finally block which is executed no matter if the try block succeeded or wasn't successful question number 43 what are custom exceptions well sometimes errors need to be called as per our requirement so as per our specific developers requirements and custom exceptions are used for those situations so we can create our own exceptions and this is a very basic one or basic approach to create such a custom exception basically we throw a new exception saying that something may not happen for example or where we express what the problem with whatever was executed is so in this case i have this function called do purchase and you can pass in a quantity that you want to purchase but if for some reason the quantity is zero we can throw the exception that the quantity cannot be zero or let's say it's even less than zero or zero then we can extend this and say quantity cannot be zero or negative all right so now let's run this and you will see that it's throwing an exception that is unhandled it says quantity cannot be zero or negative and this is our custom exception that we created there is a more complex way to create custom exceptions you can basically inherit from the exception class to create your very own exceptions but that again would be its own video to just talk about exceptions in detail but basically you can see that there is this class exception and you can go ahead and create a custom class which will inherit from exception and then use it in your try and catch block as we have seen in 43 question number 44 what is link in c sharp and by link i don't mean the guy from zelda the video game i mean l i and q so language integrated query is the full form of link it is a method of querying data using the.net capabilities and c-sharp syntax similar to sql query and the advantage of link is that we can query different sources of data the data source could be either a collection of objects xml files json files in-memory data or lists database objects you name it we can easily retrieve data from any object that implements the i innumerable interface this is an example of link syntax so we have a list here which is just one type of innumerable that you could use for link and we have mobiles in that list of strings so iphone samsung nokia mi so basically these are brands of mobile phones and then this is the link syntax so we have this var result and this is the query that we are using here we say from s in mobiles where s contains nokia select s so that is super powerful because now we can now use result to work with it in our case very simply writing onto the console so if we want to use get data here and call this method we need to make sure that it's static so public static void get data if we now run this we will see that we get something very weird we're going to get system.link innumerable where list iterator one system string so it's not very useful but what we can do with result now is we can for example get the first entry so we can call first that will return the first entry in that result so basically the first thing that was filtered in this case it contains nokia so let's run this and we can get the result is nokia so this is a very simple list but it could also be an xml file that we're working with or it could be a database that we get from the web and the cool thing is this code would work exactly the same so we wouldn't have to change any of that the only thing that we need to change is the data source where it comes from and how we handle it but that's really the power of link that it can work with different types of innumerables quick pause in this video you'll learn something about c sharp and if you want to learn everything there is to know that you need for the fundamentals and to become a real c sharp developer then definitely check out my c-sharp master class in which you're going to learn all of the things you need to know about c-sharp so you're going to learn how to do the basics how to use object-oriented programming how to use wpf in order to create your own user interfaces how to use databases how to use link how to create your own games using unity and a lot more so if you want to become a real c sharp developer definitely check out the link in the description below if you want to learn more about linkedin you should definitely check out my complete chat masterclass because there i cover how to use the link in depth question number 45 what is serialization when we want to send an object through a network then we have to convert that object into a stream of bytes serialization is a process of converting an object into a stream of bytes to facilitate the object to be serializable it should implement i serialize interface the process of the serialization is the reverse process of creating an object from a stream of bytes and where i use serialization for the first time was when i created a game in unity and there i had the situation where i wanted to store the state of the game so i wanted to save the game basically and in order to do that i needed to make sure that i know where all of the objects were and i needed to serialize the objects so that i could then save it and then i needed to deserialize the saved file to then basically load up the game so that is where you would for example use serialization to save a game but also to send data through a network question number 46 what are generics in c sharp well generics increase performance increase type safety reduce repeated code and make reusable code possible or more reusable code possible so using generics we can create collection classes it is preferred to use collection the namespace so collections.generic instead of classes such as the arraylist in systems.collections because it's just way more efficient generics encourage the usage of parameterized types and yeah let's have a look at this particular example that we have here so to specify the parameter type we use the less than and greater than sign here so we have this class gfg which has this generic type t and then we have private data members t data type this t data type here comes from our parameter type for the gfg the same goes for this property here public t value and then we have this class here vehicle with the main method so here i'm creating a gfg out of strings which i call company and the value here is tata motors so this is a indian car manufacturer and then i can also create an instance of flow type so here gfgfloat version so now new gfg is of type float and version.value is 0.6f so i can use company value and version value and you can see each time it's value but the type of the value is different each time because we made it generic so we used a generic here and that is really the power of generic so you can really reuse quite a bit using generics question number 47 what is reflection well reflection is when managed code can read its own metadata to find assemblies 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 method or access its fields and properties if you are using attributes in your code reflection enables you to access them so the following information can be retrieved using reflection assembly name the class name method name object type and it identifies properties and methods let's look at this super simple example of reflection so here i have an integer called i and it has the value of 1337 and i can use reflection to get the type and then basically print the type onto the console so this will give me information about the type saying system.int32 because this here is an int 32. question number 48 how to use nullable types in c-sharp well c-sharp also offers the nullable type even though it's not used as often and as in some other programming languages such as for example swift there it's used extensively but it's called optional so it's not nullable but optional but basically the concept is the same so you have a variable that can be null and here for example we have this question mark after the integer telling that this can now be a nullable type which means that it can hold a null value now we have this function called calculate which needs a number and we can now assign this number that was passed to it to the number variable that was a nullable and then we will only execute something in case that number has a value so we definitely need to make sure that this is the case because otherwise we could for example try to use number at that point and it might be null and if we want to access its value it could make our program crash so that's why we have to be doubly careful when using nullables all right question number 49 what is the parent class of all classes which we create in c-sharp well it is the class called object so system dot object is the class that we use for this so basically every single object that we create is inheriting from object so the system.object class even though we don't do it specifically but it's done automatically in the background question number 15 explain code compilation in c sharp so basically the c sharp compiler compiles the code into managed code also called bytecode and then the jit just in time compiler compiles the byte code into native or machine language which is directly executed by cpu alright so that's it for this series i hope you enjoyed it i hope you learned something new 50 questions with 50 answers that could help you for your job interview in c-sharp so yeah i hope you liked it if you did then please leave a like and also don't forget to hit that subscribe button because we are uploading great other videos 4c sharp unity and android regularly also see you in the next video
Info
Channel: tutorialsEU - C#
Views: 1,192
Rating: undefined out of 5
Keywords: c# interview preparation, C# interview prep, c# interview questions, c# interview questions and answers, c# interview questions with answers, c# interview questions for experienced professionals, c# interview questions with examples, csharp interview questions, c# interview questions with example, c#, step by step c#, c# interview, c# interview questions for beginners, essential c# interview questions, 50 essential c# interview questions, c# interview questions with answers pdf
Id: VCcXcwsH9G4
Channel Id: undefined
Length: 77min 24sec (4644 seconds)
Published: Thu Sep 30 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.