C Sharp Fundamentals

  • Uploaded by: Keith Johnson
  • 0
  • 0
  • June 2020
  • PDF

This document was uploaded by user and they confirmed that they have the permission to share it. If you are author or own the copyright of this book, please report to us by using this DMCA report form. Report DMCA


Overview

Download & View C Sharp Fundamentals as PDF for free.

More details

  • Words: 3,839
  • Pages: 10
Microsoft Visual C# Fundamentals: 50 Questions and 50 Answers

1. Why shouldn’t all numbers be declared as the larger data types instead of the smaller data types? Although it might seem logical to use the larger data types, this would not be efficient. You should not use any more system resources (memory) than you need. 2. What happens if you assign a negative number to an unsigned variable? The compiler returns an error saying that you can’t assign a negative number to an unsigned variable if you do this with a literal. If you do a calculation that causes an unsigned variable to go below zero, you get erroneous data. 3. A decimal value is more precise than a float or a double value. What happens with rounding when you convert from these different data types? When converting from a float, double or decimal to one of the whole number variable types, the value is rounded. If a number is too big to fit into the variable, an error occurs. When a double is converted to a float that is too big or too small, the value is represented as infinity or 0, respectively. When a value is converted from a float or a double to a decimal, the value is rounded. This rounding occurs after 28 decimal places and occurs only if necessary. If the value being converted is too small to be represented as a decimal, the new value is set to 0. If the value is too large to store in the decimal, an error occurs. For conversions from decimal to float or double, the value is rounded to the nearest value that the float or double can hold. Remember, a decimal has better precision than a float or a double. This precision is lost in the conversion. 4. What other languages besides C# adhere to the Common Type System (CTS) in the Common Language Runtime (CLR)? Microsoft Visual Basic .NET (Version 7) and Microsoft Visual C++ .NET (Version 7) both support the CTS. Additionally, versions of a number of other languages are ported to the CTS as well. 5. How important is it to understand operators and operator procedures? You will use operators in almost every application you create. Operator precedence is critical to understand. If you don’t understand operator precedence, you might end up with results that are different from the ones you are trying to achieve. 6. Is it important to master the binary number system? Although it is not critical to understand binary, it is important. With computers today, information is stored in a binary format. Whether it is a positive versus negative charge, a bump versus an impression, or some other representation, all data is ultimately stored in binary. Knowing how the binary system works will make it easier for you to understand actual storage values. In addition to binary, many computer programmers work with octal and hexadecimal. Octal is a base 8 number system and hexadecimal is a base 16 number system.

The Decimal Number System The decimal number system is the base 10 system that you use every day. A number in this system, for example 342, is expressed as a power of 10. So, in the decimal number system, this number breaks down to 300 plus 40 plus 2 to give you 342. The base 10 system requires ten different digits 0 through 9. Also, a number is represented as powers of the system’s base and the system of base n requires n different digits. The Binary Number System The binary number system is base 2 and therefore requires only two digits, 0 and 1. The binary system is useful for computer programmers, because it can be used to represent the digital on/off method in which computer chips and memory work. Here is an example of a binary number and its representation in the decimal notation you’re more familiar with, writing 1011 vertically: 1 0 1 1

represents represents represents represents

eight; zero (but would be four if was 1); two; one; so 8 plus zero plus two plus one equals eleven.

Binary is a great system however with one limitation: it is cumbersome for representing large numbers. The Hexadecimal Number System This number system is base 16. Therefore, it requires 16 digits. The digits 0 through 9 are used, along with the letters A through F, which represents the decimal values 10 through 15. Here is an example of a hexadecimal number 2DA and its decimal equivalent: 2 D A

represents 2 times 256, which is 256 represents 13 times 16, which is 208 represents 10 times 1, which is 10, equaling 730.

The hexadecimal system is really useful in computing because it is based on powers of 2. Each digit in the hex system is equivalent to a four-digit binary number and each two-digit hex number is equivalent to an eight-digit binary number. Hexadecimal Digit

Decimal Equivalent

Binary Equivalent

0 1 2 3 4 5

0 1 2 3 4 5

0000 0001 0010 0011 0100 0101

6 7 8 9 A B C D E F 10 F0 FF

6 7 8 9 10 11 12 13 14 15 16 240 255

0110 0111 1000 1001 1010 1011 1100 1101 1110 1111 00010000 11110000 11111111

7. What are the essential constructs that are part of the C# language? The most essential construct is the “if else” construct. Then, there is the switch statement (a type of selection statement). Next, there are the while, do, and for statements. Finally, there is the “goto” statement and this can be used with case, default, or labels statements. 8. Are there other types of control statements? Yes, there are the throw, try, catch, and finally statements as well. 9. Can you use a text string with a switch statement? Yes. A string is a “governing type” for switch statements. This means that you can use a variable that holds a string in the switch and then use string values in the case statements. Remember, a string is simply text in quotation marks. 10. Why is a “goto” statement considered unfavorable? The goto statement has gotten a bad rap. If used cautiously and in a structured and organized manner, the goto statement can help solve a number of programming problems. The statements “goto case” and “goto default” are prime examples of good uses of goto. The statement goto has a bad rap because the goto statement is often not used cleanly; programmers use it to get from one piece of code to another quickly and in an unstructured manner. In an object oriented language like C#, the more structure you can keep in your programs, the better. They will also be more maintainable by other programmers, should that be the case. 11. What are the four pillars of object-oriented programming? The four pillars of object-oriented programming are 1. 2. 3. 4.

Encapsulation Polymorphism Inheritance Reuse

12. What is encapsulation? Encapsulation is the concept of making “packages” that contain everything you need. In object-oriented programming, this means that you can create a class that stores all the variables that you need and all of the routines to commonly manipulate this data. For example, you can create a class called “Circle” that stores all information about circles. This class can include routines, for example, that include getting a circle’s area, radius, circumference, center point, etc. By encapsulating a circle, you allow the user to be oblivious to how the circle works. He or she only need to know how to interact with the circle. 13. What is polymorphism? Polymorphism is the ability to assume many forms which means that programs can work with the information you send to them. For example, you could have a routine that gives the area of a shape. Because the area of a triangle is calculated differently than that of other shapes, the routine to calculate the area would need to adapt based on what is sent. Regardless of whether a triangle, circle, or another shape is sent, the routine would be capable of treating them all as shapes and calculating, for example, the area. 14. What is inheritance? In many object-oriented programming books, an animal analogy is used to illustrate inheritance. The analogy starts with the concept of an animal as a living being. Now consider reptiles, which are everything that an animal is, plus they are cold-blooded. A reptile contains all of the features of an animal, but it also adds its own unique features. Now consider a snake. A snake is a reptile that is long and skinny and that has no legs. It has all the characteristics of a reptile, yet has its own unique characteristics. A snake can be said to inherit the characteristics of a reptile. A reptile can be said to inherit the characteristics of an animal. 15. What is reuse? When you reuse a class, you can reuse it to create lots of objects. By using inheritance and some of the features described previously, you can create routines that can be used repeatedly in many programs and in many ways. By encapsulating functionality, you can create routines that have been tested and are proven to work. 16. Why are classes defined? Ultimately, classes are defined so that you can then create objects. A class by itself does not have the capacity to hold information or actually perform routines. Instead, a class is used to declare objects. The objects within a class can be used to hold the data and perform routines defined within the class. 17. What is instantiation? Instantiation is the declaring of an object, so this means that an object is an “instance” of a class.

18. Would you ever use a class with just data member? Generally, you would not use a class with just data members. The value of a class and of object-oriented programming is the capability to encapsulate both functionality and data into a single package. 19. Should all data members always be declared public so people can get to them? Absolutely not . Although many data members many be declared as public, sometimes you don’t want others to get to your data. One reason is to allow for the capability to change the way that the data is stored. 20. What classes already exist for C#? Microsoft has provided a bunch of classes called .NET base classes, and also has provided documentation on what each of these classes can do. The classes are organized by namespace. 21. What are other names for routines in C#? Two popular “other names” for routines in C# are functions and methods. Most developers today refer to routines as methods, but there are some developers who refer to them as functions. A method (e.g. function) is a named piece of independent code that is placed in a reusable format and can operate without interface from other parts of an application. If created correctly, it should perform a specific task that is indicated by its name. Methods can both return a value as well as have information passed along to them. 22. What does a method header tell us about a method? There are four things that we can know about a method from its header: a. The access that programs have to the method. b. The return data type of the method. c. Any values that are being sent to the method. d. The name of the method. 23. How does a method header end? A method header ends without a semicolon. If you place a semicolon at the end of the method header, you will get an error. 24. How should you name your methods? You should name methods appropriately by giving them a meaningful name. For example, if a method calculates and returns the area, the name “getArea” makes sense, as would names like “CalculateArea” and “CalcArea”. Names like “routine1” make little or no sense. 25. How do you use a method? You must call a method for it to be used. A method is called the same way that a data member is called. You enter the object name followed by a period and then the method name. The difference between calling a method and calling data members is

that you must also include parentheses and any parameters that are needed. 26. How do you pass values to methods? You must specify the header along with appropriate parameters. The format of a method header with parameters is as follows: Modifiers ReturnType Name (Parameters) The parameters are passed within the parentheses of the method. Parameters are optional so if no parameters are sent, the parentheses are empty. The basic format for each parameter that is used is as follows: {Attribute} Type Name Type is the data type of the value being passed and Name is the name of the variable being passed. Optionally, you can have an attribute as well. 27. What are the three types of “access” attributes for parameters? These “access” attributes for parameters are (1) value, (2) reference, and (3) out. Value access on a parameter refers to when a copy is made of the data being sent to the method. The method then uses a copy of the data being sent. The original values sent to the method are not impacted. In the case that you want to modify the data stored in the original variable being sent to a method, you can pass a reference to the variable instead of the variable’s value. So, a reference is a variable that has access to the original variable. If you change the reference, then you change the original variable’s value. Finally, you can add parameters to your method header specifically for returning values by adding the “out” keyword. This keyword signifies that a value is being returned out of the method but is not coming in. When you call a method that has an out parameter, you must be sure to include a variable to hold the value being returned. 28. Are there special types of methods? Yes – special types of methods include (1) property accessor methods, (2) constructors, and (3) destructors/finalizers. Examples of property accessor methods include “set” and “get”. These methods enable you to keep data members private. Constructors are used when you want some setup to occur when the object is first created. This method is used specifically for this purpose (e.g. initial set-up or construction). There are two types of constructors: (1) instance and (2) static. Instance constructors are used when each instance or object is created and static constructors are used before any objects are created for a class. Finally, you can perform some operations when an object is destroyed. These are accomplished in the destructor. From the technical side of things, a destructor is generally called by the C# runtime after an object of a class is no longer in use. The C# runtime normally calls destructors just before checking to see whether any available memory can be freed or released (a concept called garbage collection). If the C# runtime does not do any of this memory checking between the time the object is no longer

used and the time the program ends, the destructor will never happen. It is possible to force garbage collection to happen. However, it makes more sense to just limit your use of destructors. A destructor is defined by using a tilde (~) followed by the class name and empty parentheses. //----------------------//sample destructor //----------------------~xyz() { //destructor class } //-----------------------29. What is the difference between a parameter and an argument? A parameter is the definition of what will be sent to a method. A parameter occurs with the definition of a method in the method head. An argument, on the other hand, is a value that is passed to a method. You pass arguments to a method. The method matches the arguments to the parameters that were set in the method’s definition. 30. Can you create a method outside of a class? No. Because Microsoft Visual C# is object-oriented, all code must be within the framework of a class or classes. 31. If I cannot depend upon destructors, how can I do cleanup code? It is recommended that you create your own methods that perform this task. For example, if you have a class that creates a file object, you will want to close the file when you are done with it. Because a destructor might not be called, you should create your own closing method. You don’t want to leave this file sitting open longer than you need to. 32. What are a few alternative data storage methods? There are three such methods: structures, enumerators, and arrays. 33. What is the difference between a structure and a class? The primary difference between a structure and a class is centered on how a structure is stored and accessed. A structure is a value data type, while a class is a reference data type. A value data type stores the actual values at the location that points to where the information is stored. Because the overhead of reference is not included, a structure is preferred when dealing with small amounts of data or small amounts of data values. When dealing with large amounts of data, however, a class is preferred. This is especially true when passing the data to a method. So, if you need to decide between using a structure or class, determine first if the total size of data (members) is 16 bytes or less. If so, a structure is recommended. Otherwise, you should use a class.

34. Is declaring members in a structure the same as a class? Yes – however the struct keyword is used instead of class. Example: //----------------------//sample structure //----------------------struct Point { public int x; public int y: } //-----------------------35. What is a nested structure? Like classes, structures can contain any other data type, which includes other structures (e.g. nested structures). 36. What is a structure method? Like classes, structures also contain methods and properties. Methods and properties are declared exactly the same as classes. This includes using the modifiers and attributes used with classes. You can (1) overload these methods, (2) pass values, and/or (3) return values. 37. What is unique about a structure constructor? Unlike classes, if you decide to declare a constructor, you must include declarations with parameters. You cannot declare a constructor for a structure that has no parameters. 38. Can destructors be used in structures? Whereas classes can have destructors, structures cannot. If you try to add one to a structure, the compiler gives you an error. 39. What is an enumerator? Enumerators are another type that can be used in C#. Enumerators enable you to create variables that contain a limited number of values. For example, there are only seven days in the week. Instead of referring to the days of a week as 1, 2, 3, etc. it is much clearer to refer to them as Day.Monday, Day.Tuesday, Day.Wednesday, etc. Enumerators are declared with the “enum” keyword. The format for an enumerator is as follows: //-------------------------//sample enumerator //-------------------------modifiers enum enumName { enumMember1; enumMember2; }

//---------------------------40. In the above example, what does “modifiers” represent? The term “modifiers” is either the new keyword or the access modifier (public, private, protected, or internal). Note that by default, when an enumerator variable is initially declared, it is set to the value of 0. 41. How can I change the default value of an enumerator variable? The default value assigned to an enumerator variable is 0, however, you can change this default value. First, you can put a filler value in the first position of the enumerator. This is an easy option if you want that value to start at 1. The second option is to explicitly set the value of your enumerator members. These can be set with literal values, the value of other enumerator members, or calculated values. 42. How do I change the underlying type of an enumerator? If you do not specify the exact type, the default for enumerators is “int”. However, other types can be selected, such as byte, sbyte, uint, short, ushort, long, and ulong. To change the default type, use the following format: modifiers enum enumName : typeName {members(s)} 43. When should I use an array to store data? If you need to keep track of items that are of the same data type, the best solution is to use an array. An array is a single data variable that can store multiple pieces of data that are each of the same data type. Each of these elements is stored sequentially in the computer’s memory, thus making it easy to manipulate them and navigate among them. To declare an array, you must use the square brackets [] after the data type when you declare the variable. The basic format is as follows: datatype[] name; 44. How does an array work? After you have declared an initialized an array, you can begin to use it. Each item in an array is called an element. Each element within the array can be accessed by using an index. An index is a number that identifies the offset, and thus, the element within the array. Here is a sample array: decimal[] balance = new decimal[] {1000.00m, 2000.00m, 3000.00m}; 45. Where can arrays be used? An array is just another type that can be used to create variables. Arrays can be placed and created anywhere other data types can be used. This means that arrays can be used in structures, classes, and other data types. 46. How is the “foreach” keyword to be used? The “foreach” keyword can be used to simplify working with arrays, especially when

you want to loop through an entire array. Additionally, instead of using the array name with a subscript, you can use a simple variable to work with the array. The downside of the “foreach” statement is that the simple variable that you get to use is read-only, so you cannot do assignments to it. Here is the “foreach” format: foreach (datatype varname in arrayName) { statements; } 47. What are they types of arrays I can use? You can use (1) one-dimensional arrays, (2) rectangular arrays where subarrays have the same size array, or (3) arrays of different sizes (e.g. jagged arrays). 48. Is the enumerator a value type or reference type? When a variable is declared as an enumerator, it is a value type. The value is actually stored in the enumerator variable. 49. What is the foundation for C# applications? This includes storing basic data, controlling the flow of the program, repeating pieces of code, and creating classes that can store both data and methods. 50. What is the common feature of all concepts learned so far? Most of these concepts are “single” concepts only, and have not yet been integrated with other concepts. They have been explained in isolated scenarios for clarity.

Source: Teach Yourself C# in 21 Days, by Bradley Jones

Related Documents

C Sharp Fundamentals
June 2020 7
C Sharp
November 2019 24
C-sharp,
November 2019 28
C++ Fundamentals
November 2019 14
C Sharp Interview
November 2019 12

More Documents from ""