What is SecureString ?

In this post, we are going to discuss a class SecureString. Although this class is available since .NET 2.0, but I am sure, many of us would not be knowing or using it. Even I was not aware of this fantastic class few weeks ago. This class can be very useful for you if you are more concerned about your application security.

This class belongs to the namespace System.Security . This class should be used to store the data which is confidential. The text assigned to this string is by default encrypted. This data is also removed as soon as it is not required which in-turn reduces the chances of misuse.

Continue reading

Advertisements

Reading Event logs efficiently using C#

This post is extension is my last post on Event Viewer. Please the find the link below.

Working with EventViewer using C#

We’ll be talking about the reading event logs. In my last post I discussed about reading the event logs. But that is very slow because that loads all the event entries in memory and then iterate one by one. Say if we have around one five thousand entries in the log then it will load all the entries in memory and then allows to iterate one by one. It makes the reading of logs very slow. Also at certain point of point we might require to read the event logs on some specific criteria or want to read some specific logs then also we need to load all the events and iterate and find the required entry.

Continue reading

Learning null-coalescing operator

I am pretty sure that most of the guys has never knew this operator and its also very rarely used. Since C# provides this and this is very useful and handy at certain times.

The short from of null-coalescing operator is ??.

It actually takes two operands.  So it returns the left hand operand if it is not null else returns right operand. So this is very useful while initializing any variable. let’s see a example

string name = null;
string otherName = "Brij";
string userName = name ?? otherName;
Console.WriteLine(userName);

As you can see, here is name is null then userName is assigned to otherName. and it will print Brij.

Continue reading

Exploring Nullable types : Part 2

This is second and last part of the post on Nullable type series. You can view the first part from here

Exploring Nullable types : Part 1

In this series, I will talking certain rules that we need to take care while using Nullable type. I will be taking scenario wise.

First scenario:

Int? a=8;
object o = a;
long d = (long)c;

It will be compiled successfully but will throw and Invalid Cast Exception. Means, you cannot cast it to any other type except the underlying type which is here int although  int type can be hold by long.

Continue reading

Generics and Constraints over Generics

In this post,  I am going to discuss about Generic Classes and bit more on this.  Just for a smooth start,

“Generics were introduced in .NET 2.0, which provides us a way to create Classes/Methods/Types and many more without specifing the specific type of parameters. Parameters must be provided at the time of creating the instances. So we can say Generics are only a blueprint/templated version and actual type is defined at Runtime.”

So lets first create a simple Class say Point Continue reading…

Some basics about C#

I have seen a lot of confusion amongst a lot developers about some basic things of C#. I am going to discuss some of them here.

The very first thing, In C# every thing is a struct or a Class. And every type(Class) is a derived from System.Object, whether is of reference type or Value type.

All Value type variables are derived from System.Value type and System.Value type itself is derived from System.Object. And every value type variables is declared sealed type so no one can derive from it.

Now Lets move some basic Questions.

– What is the difference string, String and System.String?

– What is the difference amongst int, System.Int32, System.Int64.

So these are few basic things, some always have confusion. So lets try to clear it out.

int c;

System.Int32 c;

int c=0;

System.Int32 c = 0;

int c= new int();

System.Int32 c = new System.Int32();

So what is the difference in all above. or Is there any differences?

In one word answer: NO

All of the above would be compiled and will produce the same and exact IL code.

It means all of these are same. Actually C# define primitive types and every primitive is directly mapped to some Class in FCL. So whenever you are going to write the primitive type, compiler while compiling finds the exact Class from the FCL and replaces with it.

Similarly, System.String or String or string all are same. Even I had confusion earlier between String and string. Even when you type both in Visual Studio IDE, both are in different color.

One more thing, I want to say every value type is initialized to its defined default value. Whether you give its default value or not Compiler does it for you, but this is not true for Reference Type.

One already know, writing int is mapped Int32 or is of 32 bit. But some people have different view, according to them int is of 32 bit in 32 bit machines and is of 64 bit on 64 bit machines. Which is absolutely wrong and int is always of 32 bit in C#, whether is of 32 bit or 64 bit machines.

Hope it clears…

Cheers,

Bri