0% found this document useful (0 votes)
51 views

C# Points

This document discusses the advantages of using Visual Studio, which include easy creation, editing, and debugging of files required for development. Visual Studio allows for easy use of source control and includes an excellent debugger. It also provides intelligence support for .NET APIs and makes it easy to incorporate web services, controls, and libraries. Nuget support allows for easy installation of third-party APIs and controls.

Uploaded by

Prashant Araya
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
51 views

C# Points

This document discusses the advantages of using Visual Studio, which include easy creation, editing, and debugging of files required for development. Visual Studio allows for easy use of source control and includes an excellent debugger. It also provides intelligence support for .NET APIs and makes it easy to incorporate web services, controls, and libraries. Nuget support allows for easy installation of third-party APIs and controls.

Uploaded by

Prashant Araya
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 20

Advantages of Visual Studio:

Easy to create, edit, search different types of files required for the
development.

Easy to use Microsoft or third party Source control.

Visual studio includes an excellent debugger that allows you to debug


code easily. Step through the application code to understand it line by
line, or identify problems in your code.

Intelligence support for MS .Net API classes.

Easy to incorporate web services, or third-party controls or libraries.

Nuget support for installing third party API/controls in the application.

Let's write first C# console program in the next section.

In order to see the output of the above C# program, we have to compile it and
run it by pressing Ctrl + F5, or clicking Run button or by clicking the "Debug"
menu and clicking "Start Without Debugging". You will see following output in
the console:
Hello World!!

So this is the basic code items that you will probably use in every C# code. Let's
learn about C# Class in the next section.

Points to Remember:
1. C# Class defines properties, fields, events, methods etc. An object is an
instance of the class.
2. Access modifiers defines the accessibility of a class e.g. public, private,
protected or internal.
3. Namespace can include one or more classes.

Points to Remember:
1. The variable is a name given to a data value.
2. A variable holds the value of specific data type e.g. string, int, float etc.
3. A variable can be declared and initialized later or declared & initialized at
the same time.
4. The value of a variable can be changed at any time throughout the
program as long as it is accessible.
5. Multiple variables can be defined separated by comma (,) in a single or
multiple line till semicolon (;).
6. A value must be assigned to a variable before using it otherwise it will
give compile time error.
Data types are further classified as value type or reference type, depending on
whether a variable of a particular type stores its own data or a pointer to the
data in the memory.

Points to Remember:
1. Value type stores the value in its memory space, whereas reference type
stores the address of the value where it is stored.
2. Primitive data types and struct are of the 'Value' type. Class objects,
string, array, delegates are reference types.
3. Value type passes by val by default. Reference type passes by ref by
default.
4. Value types and reference types stored in Stack and Heap in the memory
depends on the scope of the variable.

Points to Remember:
1. Keywords are reserved words that cannot be used as name or identifier.
2. Prefix '@' with keywords if you want to use it as identifier.

3. C# includes various categories of keywords e.g. modifier keywords,


access modifiers keywords, statement keywords, method param keywords
etc.
4. Contextual keywords can be used as identifier.

Points to Remember:
1. An Interface only contains declarations of method, events & properties.
2. An Interface can be implement implicitly or explicitly.
3. An Interface cannot include private members. All the members are
public by default.

Points to Remember:
1. if-else statement controls the flow of program based on the evaluation of
the Boolean expression.
2. It should start from the if statement followed by else or else-if
statements.
3. Only one else statement is allowed in the if-else chain.
4. Multiple else-if statements are allowed in a single if-else chain.
5. Nested if-else statement is allowed.

Points to Remember:
1. Ternary operator: Boolean expression? first statement: second statement;
2. Ternary operator returns a value; it does not execute it.
3. It can be used to replace a short if-else statement.
4. A nested ternary operator is allowed. It will be evaluated from right to
left.

Points to Remember:
1. The switch statement tests the variable against a set of constants.
2. The switch statement contains multiple case labels.
3. The switch case includes break keyword to stop the execution of switch
case.
4. The default case executes when no case satisfies the expression.
5. A nested switch statement is allowed.

Points to Remember:
1. The for loop executes the block of code repeatedly.
2. The for loop has
increment/decrement.

three

steps:

initialization,

condition

and

3. The for loop can use control variable of any numeric data type.
4. Use break keyword to stop the execution and exit from for loop.
5. Nested for loop is allowed in C#.
Please make sure that conditional expression evaluates to false at some point to
avoid infinite loop.

Points to Remember:
1. The while loop executes the block of code repeatedly.
2. The while loop includes condition expression. Increment/decrement step
should be inside the loop.
3. Use break keyword to stop the execution and exit from while loop.
4. A nested while loop is allowed.

Points to Remember:
1. The do-while loop executes the block of code repeatedly.
2. The do-while loop execute the code at least once. It includes the
conditional expression after the code block and the increment/decrement
step should be inside the loop.
3. Use the break keyword to stop the execution and exit from a do-while
loop.
4. A nested do-while loop is allowed.

Characteristics of Structure:

Structure can include constructors, constants, fields, methods, properties,


indexers, operators, events & nested types.

Structure cannot include default constructor or destructor.

Structure can implement interfaces.

A structure cannot inherit another structure or class.

Structure members cannot be specified as abstract, virtual, or protected.

Structures must be initialized with new keyword in order to use its


properties, methods or events.

Difference between struct and class:

Class is reference type whereas struct is value type

Struct cannot declare a default constructor or destructor. However, it can


have parametrized constructors.

Struct can be instantiated without the new keyword. However, you won't
be able to use any of its methods, events or properties if you do so.

Struct cannot be used as a base or cannot derive another struct or class.

Points to Remember:
1. Structure is a value type and defined with keyword struct
2. Structure can be initialized with or without new keyword.
3. Structure must be initialized with new keyword to use its properties,
method and events.

Points to Remember:
1. The enum is a set of named constant.
2. The value of enum constants starts from 0. Enum can have value of any
valid numeric type.
3. String enum is not supported in C#.
4. Use of enum makes code more readable and manageable.

Points to Remember:
1. String Builder is mutable.
2. String Builder performs faster than string when appending multiple string
values.
3. Initialize StringBuilder as class e.g. StringBuilder sb = new StringBuilder ()
4. Use StringBuilder when you need to append more than three or four
strings.

5. Use Append () method to add or append strings with StringBuilder.


6. Use ToString () method to get the string from StringBuilder.

Points to Remember:
1. An Array stores values in a series starting with a zero-based index.
2. The size of an array must be specified while initialization.
3. An Array values can be accessed using indexer.
4. An Array can be single dimensional, multi-dimensional and jagged array.
5. The Array helper class includes utility methods for arrays.
Be careful while working with jagged arrays. It will throw an IndexOutOfRange
exception if the index does not exist

Points to Remember:
1. ArrayList can store items(elements) of any datatype.
2. ArrayList resizes automatically as you add the elements.
3. ArrayList values must be cast to appropriate data types before using it.
4. ArrayList can contain multiple null and duplicate items.
5. ArrayList can be accessed using foreach or for loop or indexer.
6. Use Add (), AddRange (), Remove (), RemoveRange (), Insert (),
InsertRange (), Sort (), Reverse () methods.

Points to Remember:
1. C# has generic and non-generic SortedList.

2. SortedList stores the key-value pairs in ascending order of the key. Key
must be unique and cannot be null whereas value can be null or
duplicate.
3. Non-generic SortedList stores keys and values of any data types. So
values need to be cast to appropriate data type.
4. Key-value pair can be cast to DictionaryEntry.
5. Access individual value using indexer. SortedList indexer accepts key to
return value associated with it.

Points to Remember:
1. Stack stores the values in LIFO (Last in First out) style. The element
which is added last will be the element to come out first.
2. Use the Push () method to add elements into Stack.
3. The Pop () method returns and removes elements from the top of the
Stack. Calling the Pop () method on the empty Stack will throw an
exception.
4. The Peek () method always returns top most element in the Stack.

Points to Remember:
1. The Queue stores the values in FIFO (First in First out) style. The element
which is added first will come out First.
2. Use the Enqueue () method to add elements into Queue
3. The Dequeue () method returns and removes elements from the
beginning of the Queue. Calling the Dequeue () method on an empty
queue will throw an exception.
4. The Peek () method always returns top most element.

Points to Remember:
1. Hashtable stores key-value pairs of any datatype where the Key must be
unique.

2. The Hashtable key cannot be null whereas the value can be null.
3. Hashtable retrieves an item by comparing the hashcode of keys. So it is
slower in performance than Dictionary collection.
4. Hashtable uses the default hashcode provider which is
GetHashCode (). You can also use a custom hashcode provider.

object.

5. Use DictionaryEntry with foreach statement to iterate Hashtable.

Points to Remember:
1. An indexer is same as property except that it defined with this keyword
with square bracket that takes parameter.
2. Indexer can be override by having different types of parameters.
3. Ref and out parameter with the indexer is not supported.
4. Indexer can be included as an interface member.
5. Use code snippet to insert indexer syntax automatically in the visual
studio.

Points to Remember:
1. Stream is an abstract class for transferring bytes from different sources.
It is base class for all other class that reads\writes bytes to different
sources.
2. FileStream class provides reading and writing functionality of bytes to
physical file.
3. Reader & writer classes provide functionality to read bytes from Stream
classes (FileStream, MemoryStream etc.) and converts bytes into
appropriate encoding.
4. StreamReader provides a helper method to read string from FileStream
by converting bytes into strings. StreamWriter provides a helper method
to write string to FileStream by converting strings into bytes.

Points to Remember:
1. File is a static class to read\write from physical file with less coding.
2. Static File class provides functionalities such as create, read\write, copy,
move, delete and others for physical files.
3. Static Directory class provides functionalities such as create,
copy, move, delete etc. for physical directories with less coding.
4. FileInfo and DirectoryInfo class provides same functionality as static
File and Directory class.

Points to Remember:
1. Use FileInfo class to perform operations on physical files manually.
2. Use StreamReader & StreamWriter class with FileInfo for reading or
writing string from physical file.

Points to Remember:
1. Exception is a base class for any type of exception class in C#.
2. Exception types: SystemException and ApplicationException.
3. SystemException class is used for CLR related runtime errors.
4. Exception class includes important properties e.g. Message, StackTrack,
InnerException, data etc. to associate important information with every
exception.

Points to Remember:
1. Use the try, catch and finally blocks to handle exceptions in C#.
2. The try block must be followed by a catch or finally block or both.
3. A multiple catch block is allowed with different exception filters. General
catch{..} block must come last.

4. catch{..} and catch (Exception ex) { } both cannot be used.


5. The finally block must come after the try or catch block.
6. The finally block will always execute irrespective of whether an exception
occurred or not.
7. The finally block is appropriate place for disposing objects.
8. The finally block cannot have a return or break because it isn't allowing to
leave the control.
9. Nested try-catch blocks are allowed in C#.
10.
An Exception will be catched in the inner catch block if appropriate
filter found, otherwise will be catched by outer catch block.
Please notice that throw creates an object of any valid exception type using the
new keyword. The throw keyword cannot be used with any other type which
does not derive from the Exception class.

Points to Remember:
1. Exception is a base class for any type of exception class in C#.
2. Derive Exception class to create your own custom exception classes.

Points to Remember:
1. Delegate is a function pointer. It is reference type data type.
2. Syntax: public delegate void <function name>(<parameters>)
3. A method that is going to assign to delegate must have same signature
as delegate.
4. Delegates can be invoke like a normal function or Invoke () method.
5. Multiple methods can be assigned to the delegate using "+" operator. It is
called multicast delegate.

Points to Remember:
1. Use event keyword with delegate type to declare an event.
2. Check event is null or not before raising an event.
3. Subscribe to events using "+=" operator. Unsubscribe it using "-="
operator.
4. Function that handles the event is called event handler. Event handler
must have same signature as declared by event delegate.
5. Events can have arguments which will be passed to handler function.
6. Events can also be declared static, virtual, sealed and abstract.
7. An Interface can include event as a member.
8. Events will not be raised if there is no subscriber
9. Event handlers
subscribers

are

invoked

10.
The
.NET
framework
an EventArgs base class.

synchronously
uses

if

there

are

multiple

an EventHandler delegate

and

Points to Remember:
1. Generics denotes with angel bracket <>.
2. Compiler applies specified type for generics at compile time.
3. Generics can be applied to interface, abstract class, method, static
method, property, event, delegate and operator.
4. Generics performs faster by not doing boxing & unboxing.

Points to Remember:
1. Constraints specifies the kind of types allowed with the generics.
2. Constraints can be applied using the where keyword.

3. Six types of constraints can be applied: class, struct, new (), base class
name, interface and derived type.
4. Multiple constraints also can be applied.

Points to Remember:
1. List<T> stores elements of the specified type and it grows automatically.
2. List<T> can store multiple null and duplicate elements.
3. List<T> can be assigned to IList<T> or List<T> type of variable. It
provides more helper method When assigned to List<T> variable
4. List<T> can be access using indexer, for loop or foreach statement.
5. LINQ can be use to query List<T> collection.
6. List<T> is ideal for storing and retrieving large number of elements.

Points to Remember :
1. C# has a generic and non-generic SortedList.
2. SortedList stores the key-value pairs in ascending order of the key. The
key must be unique and cannot be null whereas value can be null or
duplicate.
3. Generic SortedList stores keys and values of specified data types. So no
need for casting.
4. Key-value pair can be cast to a KeyValuePair<TKey,TValue>.
5. An individual value can be accessed using an indexer. SortedList indexer
accepts key to return value associated with it.

Points to Remember :
1. A Dictionary stores Key-Value pairs where the key must be unique.
2. Before adding a KeyValuePair into a dictionary, check that the key does
not exist using the ContainsKey() method.

3. Use the TryGetValue() method to get the value of a key to avoid possible
runtime exceptions.
4. Use a foreach or for loop to iterate a dictionary.
5. Use dictionary indexer to access individual item.
6. Use custom class that derives IEqualityComparer to compare object of
custom class with Contains() method.

Points to Remember :
1. Use the partial keyword to split interface, class, method or structure into
multiple .cs files.
2. The partial method must be declared before implementation.
3. All the partial class, method , interface or structs must have the same
access modifiers.

Points to Remember :
1. Static classes cannot be instantiated using the new keyword
2. Static items can only access other static items. For example, a static class
can only contain static members, e.g., variables, methods, etc. A static
method can only contain static variables and can only access other static
items.
3. Static items share the resources between multiple users.
4. Static cannot be used with indexers, destructors or types other than
classes.
5. A static constructor in a non-static class runs only once when the class is
instantiated for the first time.
6. A static constructor in a static class runs only once when any of its static
members accessed for the first time.
7. Static members are allocated in high frequency heap area of the memory.

Points to Remember :
1. Anonymous method can be defined using the delegate keyword
2. Anonymous method must be assigned to a delegate.
3. Anonymous method can access outer variables or functions.
4. Anonymous method can be passed as a parameter.
5. Anonymous method can be used as event handlers.

Points to Remember :
1. Nullable<T> type allows assignment of null to value types.
2. ? operator is a shorthand syntax for Nullable types.
3. Use value property to get the value of nullable type.
4. Use HasValue property to check whether value is assigned to nullable
typeor not.
5. Static Nullable class is a helper class to compare nullable types.

Points to Remember :
1. var can only be declared and initialized in a single statement. Following is
not
valid:
var
i = 10;

i;

2. var cannot be used as a field type at the class level.


3. var cannot be used in an expression like var i += 10;
4. Multiple vars cannot be declared and initialized in a single statement. For
example, var i=10, j=20; is invalid.

Points to Remember :
1. Func is built-in delegate type.

2. Func delegate type must return a value.


3. Func delegate type can have zero to 16 input parameters.
4. Func delegate type can be used with an anonymous method or lambda
expression.

Points to Remember :
1. Action delegate is same as func delegate except that it does not return
anything. Return type must be void.
2. Action delegate can have 1 to 16 input parameters.
3. Action delegate can be used with anonymous methods or lambda
expressions.

Points to Remember :
1. Predicate delegate takes one input parameter and boolean return type.
2. Predicate delegate must contains some criateria to check whether
supplied parameter meets those criateria or not.
3. Anonymous method and Lambda expression can be assigned to the
predicate delegate
The only difference between a regular static method and an extension method is
that the first parameter of the extension method specifies the type that it is
going to operator on, preceded by the this keyword.

Points to Remember :
1. Extension methods are additional custom methods which were originally
not included with the class.
2. Extension methods can be added to custom, .NET Framework or third
party classes, structs or interfaces.

3. The first parameter of the extension method must be of the type for
which the extension method is applibable, preceded by the this keyword.
4. Extension methods can be used anywhere in the application by including
the namespace of the extension method.

Points to Remember :
1. Anonymous type can be defined using the new keyword and object
initializer syntax.
2. The implicitly typed variable- var, is used to hold an anonymous type.
3. Anonymous type is a reference type and all the properties are readonly.
4. The scope of an anonymous type is local to the method where it is
defined.

Points to Remember:
1. The dynamic types are resolved at runtime instead of compile time.
2. The compiler skips the type checking for dynamic type. So it doesn't give
any error about dynamic types at compile time.
3. The dynamic types do not have intellisense support in visual studio.
4. A method can have parameters of the dynamic type.
5. An exception is thrown at runtime if a method or property is not
compatible.

Methods, Functions, or Constructors

Variables

Classes

Interfaces

Modules

Properties and Attributes

Enumerations

Symbols

Keywords

Values

Global Identifiers

Snippet Prefixes

Words

You might also like