C# Notes

You might also like

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 2

>Here are some commonly used namespaces in the .

NET Framework:

System: Fundamental types and core functionality.


System.Collections: Collections and data structures.
System.Data: Data access and database connectivity.
System.IO: Input/output operations and file system access.
System.Net: Networking and web-related functionality.
System.Security: Security-related operations and cryptography.
System.Text: String manipulation and character encoding.
System.Threading: Multithreading and asynchronous programming.
System.Windows.Forms: Windows Forms user interface components.
System.Xml: XML processing and manipulation.

Alias .NET Type Type

byte System.Byte struct


sbyte System.SByte struct
int System.Int32 struct
uint System.UInt32 struct
short System.Int16 struct
ushort System.UInt16 struct
long System.Int64 struct
ulong System.UInt64 struct
float System.Single struct
double System.Double struct
char System.Char struct
bool System.Boolean struct
object System.Object Class
string System.String Class
decimal System.Decimal struct
DateTime System.DateTime struct

>Struct: struct is a value type, so it is faster than a class object. Use struct
whenever you want to just store the data. Generally, structs are good for game
programming. However, it is easier to transfer a class object than a struct. So do
not use struct when you are passing data across the wire or to other classes.

>class without namespace - In C#, it is possible to write a class without a


namespace, but it is not considered a recommended practice. The namespace provides
a way to organize and group related classes, and it helps prevent naming conflicts
with classes from other libraries or codebases.

However, if you choose not to include a namespace, your class will be placed in the
global namespace. The global namespace is shared across the entire application, so
if you have multiple classes without namespaces, they will all reside in the same
global namespace.

>>
StringBuilder sbAmout = new StringBuilder("Your total amount is ");
sbAmout.AppendFormat("{0:C} ", 25);

Console.WriteLine(sbAmout);//output: Your total amount is $ 25.00

Let's break down {0:C}:


{0}: This is a placeholder for the first argument passed to the AppendFormat
method. It indicates that the value of the first argument should be inserted at
this position in the formatted string.
:C: This is the format specifier. The C specifies that the value should be
formatted as a currency string.

StringBuilder is mutable.
StringBuilder performs faster than string when appending multiple string values.
Use StringBuilder when you need to append more than three or four strings.
Use the Append() method to add or append strings to the StringBuilder object.
Use the ToString() method to retrieve a string from the StringBuilder object.

>>
Nullable<T> type allows assignment of null to value types.
? operator is a shorthand syntax for Nullable types.
Use value property to get the value of nullable type.
Use HasValue property to check whether value is assigned to nullable type or not.
Static Nullable class is a helper class to compare nullable types.

>>Interface

Interface can contain declarations of method, properties, indexers, and events.


Interface cannot include private, protected, or internal members. All the members
are public by default.
Interface cannot contain fields, and auto-implemented properties.
A class or a struct can implement one or more interfaces implicitly or explicitly.
Use public modifier when implementing interface implicitly, whereas don't use it in
case of explicit implementation.
Implement interface explicitly using InterfaceName.MemberName.
An interface can inherit one or more interfaces.

>>Rules for Partial Classes


All the partial class definitions must be in the same assembly and namespace.
All the parts must have the same accessibility like public or private, etc.
If any part is declared abstract, sealed or base type then the whole class is
declared of the same type.
Different parts can have different base types and so the final class will inherit
all the base types.
The Partial modifier can only appear immediately before the keywords class, struct,
or interface.
Nested partial types are allowed.

>>Rules for Partial Methods


Partial methods must use the partial keyword and must return void.
Partial methods can have in or ref but not out parameters.
Partial methods are implicitly private methods, so cannot be virtual.
Partial methods can be static methods.
Partial methods can be generic.

You might also like