C# fundamentals Flashcards
What is the difference between C# and .dot Net?
dot Net is a framework, but C# is a programming language.
Before C#, we had C++/C, where the runtime used to convert the code to the machine’s native language. i.e. when we run the code for x86 or x64, the compiler compiles the code to x86 machines.
C# will create an IL code which is machine independent and CLR will convert it to the machine dependent based on the architecture.
Explain dot Net architecture.
Class is a basic block. We will group aLL classes under name spaces. we will group these name spaces under assembly. Many assembly forms an application.
What are naming conventions avaialble?
Camel casing: string firstName = “Bharath”
Pascal casing string FirstName = “Bharath”
Hungarian notation: string strFistName = “Bharath;
usually in C#, const are pascal casing
What is overflowing?
byte u = 255 +1 //overflow to 0
How to delete a line?
ctrl+x
How to add code snippet?
cw
How to write format string?
console.writeline(“{0} {1} “, a, b);
int.Parse()
used to convert int to string
Why main method is static?
Because we can ensure that only one instance of Main can be there.
what is static keyword?
public class Person{ static string name = "bharath"; }
Person.name;
It i snot associated with this keyword
How to create an array?
int[] numbers = new int[5];
numbers[0] = 1
int[] numbers = new int[5] {1,2,3,4,5}
What is verbatin string?
string str = @”C:\PF”
There are three strings. We have to add a seprator and coine it to one string?
STring.join(“,”)
How to create an enum in c#?
public enum name{ firstName: 1, middleName:2, lastname:3 }
to use name.FirstName;
How to make enum as byte?
By default enum is of int datatype. To change it to byte: public enum name : byte { firstName: 1, middleName:2, lastname:3 }
How to convert an object to string?
every object will have ToSTring .
Also Console.WriteLine explicitly converts to string
How to convert string to an object?
Use Parse method.
What is difference between struct and class?
struct is a value type. Since it is a value type it is stored in stack and immediatly removed from stack when it goes out of scope.
Ref type are stored in heap. GC will clear.
How to use forEach?
forEach(var i in numbers)
{
}
How to check if the string is empty?
String.IsNullOrWhiteSpace
What is the correct syntax to declare a rectangular 3x5 array?
var arr = new int[3,5]
What is the difference between arrays and lists?
Arrays are fixed in size and Lists are dynamic
What will be the output of this code?
var numbers = new List() { 1, 2, 3, 4, 5 }; var index = numbers.IndexOf(1);
Console.WriteLine(index);
0
How can access the last element in a list?
list[list.Count -1]