TGViewer
Channel Public Channel
C# 1001 notes

C# 1001 notes

@csharp_1001_notes

Регулярные короткие заметки по C# и .NET.

Просто о сложном для каждого.

admin - @haarrp
Subscribers
6.67K
Photos
416
Videos
14
Links
364

Showing posts older than #194 · Back to latest

Older Posts 20 shown
Post #193 944
What is enum in C#?

An enum is a value type with a set of related named constants often referred to as an enumerator list. The enum keyword is used to declare an enumeration. It is a primitive data type, which is user defined. An enum is used to create numeric constants in .NET framework. All the members of enum are of enum type. Their must be a numeric value for each enum type.

Some points about enum:

🔸 Enums are enumerated data type in C#.

🔸 Enums are strongly typed constant. They are strongly typed, i.e. an enum of one type may not be implicitly assigned to an enum of another type even though the underlying value of their members are the same.

🔸 Enumerations (enums) make your code much more readable and understandable.

🔸 Enum values are fixed. Enum can be displayed as a string and processed as an integer.

🔸 The default type is int, and the approved types are byte, sbyte, short, ushort, uint, long, and ulong.

🔸 Enums are value types and are created on the stack and not on the heap.
Post #192 974
What is a preprocessor directives in C#?

The preprocessor directives give instruction to the compiler to preprocess the information before actual compilation starts. Generally, the optional/conditional compilation symbols will be provided by the build script.

Not only is this cleaner and easier to read since you don't end up having #if, #else within your code. This style is less prone to errors either during normal code edits and well as logic flow errors.
Post #189 908
Can this be used within a Static method?

We can't use this in static method because keyword this returns a reference to the current instance of the class containing it.

Static methods (or any static member) do not belong to a particular instance. They exist without creating an instance of the class and call with the name of a class not by instance so we can't use this keyword in the body of static Methods, but in case of Extension Methods we can use it as the functions parameters.
Post #187 1.01K
What is the use of static constructors?

A static constructor is useful for initializing any static fields associated with a type (or any other per-type operations) - useful in particular for reading required configuration data into readonly fields, etc.

It is run automatically by the runtime the first time it is needed (the exact rules there are complicated (see "beforefieldinit"), and changed subtly between CLR2 and CLR4). Unless you abuse reflection, it is guaranteed to run at most once (even if two threads arrive at the same time).

You can't overload it.
Post #184 878
What is Managed or Unmanaged Code?

🔸 Managed Code - The code, which is developed in .NET framework is known as managed code. This code is directly executed by CLR with the help of managed code execution. Any language that is written in .NET Framework is managed code.

🔸 Unmanaged Code - The code, which is developed outside .NET framework is known as unmanaged code. Applications that do not run under the control of the CLR are said to be unmanaged, and certain languages such as C++ can be used to write such applications, which, for example, access low - level functions of the operating system. Background compatibility with the code of VB, ASP and COM are examples of unmanaged code.
Post #183 996
What is the volatile keyword used for?

In C# volatile tells the compiler that the value of a variable must never be cached as its value may change outside of the scope of the program itself (such as the operating system, the hardware, or a concurrently executing thread). The compiler will then avoid any optimisations that may result in problems if the variable changes "outside of its control".

Or in more simple terms:

Sometimes, the compiler will optimize a field and use a register to store it. If thread 1 does a write to the field and another thread accesses it, since the update was stored in a register (and not memory), the 2nd thread would get stale data.

You can think of the volatile keyword as saying to the compiler "I want you to store this value in memory". This guarantees that the 2nd thread retrieves the latest value.
Post #179 906
What are Reference Types in C#?

The reference types do not contain the actual data stored in a variable, but they contain a reference to the variables.

In other words, they refer to a memory location. Using multiple variables, the reference types can refer to a memory location. If the data in the memory location is changed by one of the variables, the other variable automatically reflects this change in value. Example of built-in reference types are: object, dynamic, and string.
Post #173 1.01K
📝 What is the difference between string and StringBuilder?

String:

🔸 It's an immutable object that hold string value.

🔸 Performance wise string is slow because its' create a new instance to override or change the previous value.

🔸 String belongs to System namespace.

StringBuilder:

🔸 StringBuilder is a mutable object.

🔸 Performance wise StringBuilder is very fast because it will use same instance of StringBuilder object to perform any operation like insert value in existing string.

🔸 StringBuilder belongs to System.Text.Stringbuilder namespace.

#post
Post #171 971
✨ Here is a solution for the #challenge above
Post #170 942
#challenge

💻 Check for Anagrams | #easy

Create a function that takes two strings and returns either true or false depending on whether they're anagrams or not.

Examples:

IsAnagram("cristian", "Cristina") ➞ true
IsAnagram("Dave Barry", "Ray Adverb") ➞ true
IsAnagram("Nope", "Note") ➞ false

🏆 Leave your solutions in the comments. The solution will be posted below in a couple of hours 👇

#interview
Older posts →
Threads Profile ViewerView any public Threads profile without an account.Open ThreadLook →Writing with AI? Make it sound human.Metric37 rewrites AI drafts so they read naturally. Free AI detector, 1,500 words free.Try Metric37 →